#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <wininet.h>
const char thisVersionDate[] = "2024-01-05";
bool isMostRecentVersion(HINTERNET* hInternet, HINTERNET* hConnection, HINTERNET* hData) {
char currentVersionDate[sizeof(thisVersionDate)] = {};
*hInternet = InternetOpenA(
"wininet_test",
INTERNET_OPEN_TYPE_PRECONFIG,
nullptr,
nullptr,
0
);
if (*hInternet == nullptr) {
printf("error when using InternetOpenA: %d\n", GetLastError());
return false;
}
*hConnection = InternetConnectA(
*hInternet,
"raw.githubusercontent.com",
INTERNET_DEFAULT_HTTPS_PORT,
nullptr,
nullptr,
INTERNET_SERVICE_HTTP,
0,
0
);
if (*hConnection == nullptr) {
printf("error when using InternetConnectA: %d\n", GetLastError());
return false;
}
*hData = HttpOpenRequestA(
*hConnection,
nullptr,
"/speedrun-program/amnesia_load_screen_tool/main/amnesia_settings.txt",
nullptr,
nullptr,
nullptr,
INTERNET_FLAG_SECURE,
0
);
if (*hData == nullptr) {
printf("error when using HttpOpenRequestA: %d\n", GetLastError());
return false;
}
if (!HttpSendRequestA(*hData, nullptr, 0, nullptr, 0)) {
printf("error when using HttpSendRequestA: %d\n", GetLastError());
return false;
}
// windows API documentation says to call this in a loop
DWORD bytesReadThisLoop = 0;
DWORD totalBytesRead = 0;
while (
totalBytesRead < sizeof(currentVersionDate) - 1
&& InternetReadFile(*hData, ¤tVersionDate[totalBytesRead], sizeof(currentVersionDate) - 1 - totalBytesRead, &bytesReadThisLoop)
&& bytesReadThisLoop != 0) {
totalBytesRead += bytesReadThisLoop;
}
if (totalBytesRead < sizeof(currentVersionDate) - 1) {
printf("couldn't determine the date of the most recent version using InternetReadFile: %d\n", GetLastError());
return false;
}
bool isMostRecentVersion = (strncmp(thisVersionDate, currentVersionDate, sizeof(currentVersionDate) - 1) == 0);
if (strncmp(thisVersionDate, currentVersionDate, sizeof(currentVersionDate) - 1) != 0) {
printf("a newer version from %s is available\nthis version's date: %s\n", currentVersionDate, thisVersionDate);
return false;
}
return true;
}
int main() {
HINTERNET hInternet = nullptr;
HINTERNET hConnection = nullptr;
HINTERNET hData = nullptr;
// resources acquired:
// hInternet (1)
// hConnection (2)
// hData (3)
if (isMostRecentVersion(&hInternet, &hConnection, &hData)) {
printf("this is the most recent version\n");
}
if (hData != nullptr) {
InternetCloseHandle(hData); // hData released (3)
}
if (hConnection != nullptr) {
InternetCloseHandle(hConnection); // hConnection released (2)
}
if (hInternet != nullptr) {
InternetCloseHandle(hInternet); // hInternet released (1)
}
return 0;
}
I'm writing a program in C++ for, and I'd like it to be able to check if it's fully updated. My idea is to have it read some text from a webpage on github representing the date of the most recent version. It can then compare the date on github to its own date.
bool isMostRecentVersion = (strncmp(thisVersionDate, currentVersionDate, sizeof(currentVersionDate) - 1) == 0);
\$\endgroup\$