Added check for a new version of the application.

This commit is contained in:
2023-06-04 14:23:35 +03:00
parent aadba59546
commit e74c775f90
11 changed files with 1289 additions and 19 deletions

View File

@@ -8,9 +8,8 @@
#define TBUFLEN 32
#define DBUFLEN 256
//extern WCHAR szTitle[MAX_LOADSTRING];
extern WCHAR szTitle[];
extern SYSTEMTIME lt;
extern wchar_t debugBuffer[DBUFLEN];
extern std::wofstream logfile;
extern CRITICAL_SECTION cs;

View File

@@ -1,7 +1,6 @@
// wCenterWindow
// logger.cpp
//
#include "wCenterWindow.h"
#include "globals.h"
#include "logger.h"

View File

@@ -1,14 +1,16 @@
// wCenterWindow
// logger.h
// wLogger v3.2 (Edited version from RBTray project [https://github.com/benbuck/rbtray])
// wLogger v3.3 (Edited version from RBTray project [https://github.com/benbuck/rbtray])
//
// Usage: LOG_TO_FILE(L"%s(%d): Log message", TEXT(__FUNCTION__), __LINE__);
//
#pragma once
//#include "globals.h"
//#include "wCenterWindow.h"
#define LOG_TO_FILE(fmt, ...) StringCchPrintfW(debugBuffer, DBUFLEN, fmt, ##__VA_ARGS__); logfile << GetTimeStamp() << debugBuffer << std::endl;
#define LOG_TO_FILE(fmt, ...) do {\
EnterCriticalSection(&cs); \
StringCchPrintfW(debugBuffer, DBUFLEN, fmt, ##__VA_ARGS__); \
logfile << GetTimeStamp() << debugBuffer << std::endl; \
LeaveCriticalSection(&cs); \
} while (0);
wchar_t* GetTimeStamp();
void OpenLogFile(const wchar_t[], const wchar_t[]);

1010
wCenterWindow/picojson.h Normal file

File diff suppressed because it is too large Load Diff

216
wCenterWindow/updater.cpp Normal file
View File

@@ -0,0 +1,216 @@
// wCenterWindow
// updater.h
//
#include "globals.h"
#include "logger.h"
#include "updater.h"
#include "picojson.h"
#define GITHUB_URL TEXT("api.github.com")
#define GITHUB_URI TEXT("/repos/dreamforceinc/wCenterWindow/releases/latest")
picojson::value json;
WCHAR errMessageBuffer[DBUFLEN]{ 0 };
struct Version
{
UINT Major = 0;
UINT Minor = 0;
UINT Build = 0;
UINT Revision = 0;
} verApp, verGh;
bool GetLatestRelease(const std::wstring& urn);
void FillVersionStructure(Version& ver, const std::wstring& str);
std::vector<std::wstring> Split(const std::wstring& s, wchar_t delim);
std::wstring ConvertUtf8ToWide(const std::string& str);
DWORD WINAPI Updater(LPVOID)
{
LOG_TO_FILE(L"Entering the %s() function", TEXT(__FUNCTION__));
if (!GetLatestRelease(GITHUB_URI))
{
LOG_TO_FILE(L"[UPDT] %s(%d): Failed getting releases!", TEXT(__FUNCTION__), __LINE__);
StringCchPrintfW(errMessageBuffer, DBUFLEN, L"Failed getting releases!");
MessageBoxW(NULL, errMessageBuffer, szTitle, MB_OK | MB_ICONERROR);
return 101;
}
std::wstring j_tag_name, j_file_name, j_file_ext, j_file_url, j_page_url;
int64_t j_file_size = 0;
picojson::object obj, obj2;
picojson::object::iterator it, it2;
if (json.is<picojson::object>())
{
LOG_TO_FILE(L"[UPDT] %s(%d): Parsing JSON object", TEXT(__FUNCTION__), __LINE__);
obj = json.get<picojson::object>();
it = obj.find("message"), it2;
if (it != obj.end())
{
std::string u = (*it).second.get<std::string>();
LOG_TO_FILE(L"[UPDT] %s(%d): Error! The url is %s", TEXT(__FUNCTION__), __LINE__, u);
return 102;
}
for (it = obj.begin(); it != obj.end(); it++)
{
if ((*it).first == "tag_name") j_tag_name = ConvertUtf8ToWide((*it).second.to_str());
if ((*it).first == "html_url") j_page_url = ConvertUtf8ToWide((*it).second.to_str());
if ((*it).first == "assets" && (*it).second.is<picojson::array>())
{
picojson::array a = (*it).second.get<picojson::array>();
obj2 = a[0].get<picojson::object>();
for (it2 = obj2.begin(); it2 != obj2.end(); it2++)
{
if ((*it2).first == "name") j_file_name = ConvertUtf8ToWide((*it2).second.to_str());
if ((*it2).first == "browser_download_url") j_file_url = ConvertUtf8ToWide((*it2).second.to_str());
if ((*it2).first == "size") j_file_size = static_cast<int64_t>((*it2).second.get<double>());
}
}
}
}
else
{
LOG_TO_FILE(L"[UPDT] %s(%d): Error! Cannot recognize JSON object!", TEXT(__FUNCTION__), __LINE__);
return 103;
}
std::wstring gh_version = j_tag_name.substr(1);
LOG_TO_FILE(L"[UPDT] %s(%d): AppVersion : %s", TEXT(__FUNCTION__), __LINE__, TEXT(VERSION_STR));
LOG_TO_FILE(L"[UPDT] %s(%d): GitVersion : %s", TEXT(__FUNCTION__), __LINE__, gh_version.c_str());
//LOG_TO_FILE(L"[UPDT] %s(%d): FileName : %s", TEXT(__FUNCTION__), __LINE__, j_file_name.c_str());
//LOG_TO_FILE(L"[UPDT] %s(%d): FileSize : %d", TEXT(__FUNCTION__), __LINE__, j_file_size);
//LOG_TO_FILE(L"[UPDT] %s(%d): File Url : %s", TEXT(__FUNCTION__), __LINE__, j_file_url.c_str());
//LOG_TO_FILE(L"[UPDT] %s(%d): Page Url : %s", TEXT(__FUNCTION__), __LINE__, j_page_url.c_str());
FillVersionStructure(verApp, TEXT(VERSION_STR));
FillVersionStructure(verGh, gh_version);
if ((verGh.Major > verApp.Major) || (verGh.Minor > verApp.Minor) || (verGh.Build > verApp.Build) || (verGh.Revision > verApp.Revision))
{
LOG_TO_FILE(L"[UPDT] %s(%d): An update is available!", TEXT(__FUNCTION__), __LINE__);
if (IDYES == MessageBoxW(NULL, L"An update is available!\nDo you want to open the download page?", szTitle, MB_YESNO | MB_ICONINFORMATION))
{
LOG_TO_FILE(L"[UPDT] %s(%d): Opening download page by default browser", TEXT(__FUNCTION__), __LINE__);
ShellExecuteW(NULL, L"open", j_page_url.c_str(), NULL, NULL, SW_SHOW);
}
else
{
LOG_TO_FILE(L"[UPDT] %s(%d): The user refused the update", TEXT(__FUNCTION__), __LINE__);
}
}
else
{
LOG_TO_FILE(L"[UPDT] %s(%d): No updates is available", TEXT(__FUNCTION__), __LINE__);
return 104;
}
LOG_TO_FILE(L"[UPDT] Exit from the %s() function", TEXT(__FUNCTION__));
return 0;
}
bool GetLatestRelease(const std::wstring& urn)
{
std::wstring user_agent = L"User-Agent: ";
user_agent.append(szTitle);
const std::wstring url = GITHUB_URL;
bool ret = true;
DWORD err = 0;
HINTERNET hInternet = InternetOpenW(user_agent.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet != NULL)
{
HINTERNET hConnect = InternetConnectW(hInternet, url.c_str(), INTERNET_DEFAULT_HTTPS_PORT, L"", L"", INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
if (hConnect != NULL)
{
HINTERNET hRequest = HttpOpenRequestW(hConnect, L"GET", urn.c_str(), NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_UI, 0);
if (hRequest != NULL)
{
BOOL isSend = HttpSendRequestW(hRequest, NULL, 0, 0, 0);
if (isSend)
{
char szData[1024]{ 0 };
DWORD dwBytesRead = 0;
std::string buffer;
do
{
InternetReadFile(hRequest, szData, sizeof(szData), &dwBytesRead);
buffer.append(szData, dwBytesRead);
} while (dwBytesRead != 0);
picojson::parse(json, buffer);
std::string jerr = picojson::get_last_error();
if (!jerr.empty())
{
std::cout << jerr << std::endl;
ret = false;
}
}
else
{
err = GetLastError();
LOG_TO_FILE(L"[UPDT] %s(%d): HttpSendRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
}
else
{
err = GetLastError();
LOG_TO_FILE(L"[UPDT] %s(%d): HttpOpenRequestW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
InternetCloseHandle(hRequest);
}
else
{
err = GetLastError();
LOG_TO_FILE(L"[UPDT] %s(%d): InternetConnectW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
InternetCloseHandle(hConnect);
}
else
{
err = GetLastError();
LOG_TO_FILE(L"[UPDT] %s(%d): InternetOpenW() error: %d", TEXT(__FUNCTION__), __LINE__, err);
ret = false;
}
InternetCloseHandle(hInternet);
return ret;
}
std::vector<std::wstring> Split(const std::wstring& s, wchar_t delim)
{
std::vector<std::wstring> result;
std::wstringstream ss(s);
std::wstring item;
while (getline(ss, item, delim)) result.push_back(item);
return result;
}
void FillVersionStructure(Version& ver, const std::wstring& str)
{
std::vector<std::wstring> v;
v = Split(str, '.');
if (v.size() < 4) v.push_back(L"0");
ver.Major = std::stoul(v[0]);
ver.Minor = std::stoul(v[1]);
ver.Build = std::stoul(v[2]);
ver.Revision = std::stoul(v[3]);
}
std::wstring ConvertUtf8ToWide(const std::string& str)
{
int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
std::wstring wstr(count, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &wstr[0], count);
return wstr;
}

5
wCenterWindow/updater.h Normal file
View File

@@ -0,0 +1,5 @@
// wCenterWindow
// updater.h
//
#pragma once
DWORD WINAPI Updater(LPVOID);

View File

@@ -8,8 +8,8 @@
//
#include "framework.h"
#include "globals.h"
#include "wCenterWindow.h"
#include "logger.h"
#include "updater.h"
#define NO_DONATION
#define KEY_I 0x49
@@ -25,7 +25,7 @@ WCHAR szTitle[MAX_LOADSTRING]; // wCenterWindow's title
WCHAR szClass[MAX_LOADSTRING]; // Window's class
WCHAR szWinTitle[256];
WCHAR szWinClass[256];
HANDLE hHeap = NULL;
HANDLE hHeap = NULL, hUpdater = NULL;
HHOOK hMouseHook = NULL, hKbdHook = NULL; // Hook's handles
HICON hIcon = NULL;
HMENU hMenu = NULL, hPopup = NULL;
@@ -39,6 +39,7 @@ LPKBDLLHOOKSTRUCT pkhs = { 0 };
MENUITEMINFO mii = { 0 };
LPVOID szBuffer;
CRITICAL_SECTION cs;
// {2D7B7F30-4B5F-4380-9807-57D7A2E37F6C}
// static const GUID guid = { 0x2d7b7f30, 0x4b5f, 0x4380, { 0x98, 0x7, 0x57, 0xd7, 0xa2, 0xe3, 0x7f, 0x6c } };
@@ -92,11 +93,12 @@ VOID MoveWindowToMonitorCenter(HWND hwnd, BOOL bWorkArea, BOOL bResize)
int x = (aw - nWidth) / 2;
int y = (ah - nHeight) / 2;
LOG_TO_FILE(L"%s(%d): Moving the window to %d, %d", TEXT(__FUNCTION__), __LINE__, x, y);
SendMessageW(hwnd, WM_ENTERSIZEMOVE, NULL, NULL);
MoveWindow(hwnd, x, y, nWidth, nHeight, TRUE);
SendMessageW(hwnd, WM_EXITSIZEMOVE, NULL, NULL);
LOG_TO_FILE(L"%s(%d): Moving the window to %d, %d", TEXT(__FUNCTION__), __LINE__, x, y);
LOG_TO_FILE(L"Exit from the %s() function", TEXT(__FUNCTION__));
}
@@ -113,10 +115,21 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
return FALSE;
}
InitializeCriticalSection(&cs);
OpenLogFile(szTitle, TEXT(VERSION_STR));
LOG_TO_FILE(L"Entering the %s() function", TEXT(__FUNCTION__));
hUpdater = CreateThread(NULL, 0, &Updater, nullptr, 0, nullptr);
if (NULL == hUpdater)
{
DWORD dwLastError = GetLastError();
LOG_TO_FILE(L"%s(%d): Creating Updater thread failed! Error: %d", TEXT(__FUNCTION__), __LINE__, dwLastError);
MessageBoxW(NULL, L"Creating Updater thread failed!", szTitle, MB_OK | MB_ICONERROR);
CloseLogFile();
return dwLastError;
}
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
@@ -182,8 +195,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
LOG_TO_FILE(L"Exit from the %s() function, msg.wParam = %d", TEXT(__FUNCTION__), (int)msg.wParam);
CloseLogFile();
HeapFree(hHeap, NULL, szBuffer);
CloseHandle(hUpdater);
CloseLogFile();
DeleteCriticalSection(&cs);
return (int)msg.wParam;
}
@@ -303,6 +318,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
LOG_TO_FILE(L"%s(%d): Recieved the WM_QUERYENDSESSION message, lParam = 0x%08X", TEXT(__FUNCTION__), __LINE__, (long)lParam);
CloseLogFile();
DeleteCriticalSection(&cs);
return TRUE;
break;
}

View File

@@ -6,12 +6,16 @@
// Windows Header Files
#include <fstream>
#include <sstream>
#include <filesystem>
#include <string>
#include <vector>
#include <strsafe.h>
#include <windows.h>
#include <wininet.h>
#include <shellapi.h>
#include <CommCtrl.h>
// VerionInfo header file
#include "VersionInfo.h"

View File

@@ -104,7 +104,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>comctl32.lib;wininet.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<AdditionalManifestFiles>%(AdditionalManifestFiles)</AdditionalManifestFiles>
@@ -131,7 +131,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<AdditionalDependencies>comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>comctl32.lib;wininet.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<AdditionalManifestFiles>%(AdditionalManifestFiles)</AdditionalManifestFiles>
@@ -189,12 +189,15 @@
<ClInclude Include="framework.h" />
<ClInclude Include="globals.h" />
<ClInclude Include="logger.h" />
<ClInclude Include="picojson.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="updater.h" />
<ClInclude Include="wCenterWindow.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="logger.cpp" />
<ClCompile Include="updater.cpp" />
<ClCompile Include="wCenterWindow.cpp" />
</ItemGroup>
<ItemGroup>

View File

@@ -30,11 +30,26 @@
<ClInclude Include="logger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="globals.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="picojson.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="updater.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="wCenterWindow.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="updater.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="wCenterWindow.rc">
@@ -42,9 +57,6 @@
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<Image Include="small.ico">
<Filter>Resource Files</Filter>
</Image>
<Image Include="wCenterWindow.ico">
<Filter>Resource Files</Filter>
</Image>

View File

@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>/test1 /test2</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>
</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>