Skip to content

Commit

Permalink
Addedd UI callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaimoga committed Sep 2, 2024
1 parent e7bc22b commit 72a6b1e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
18 changes: 15 additions & 3 deletions DemoApp/DemoApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

const DWORD nLength = _MAX_PATH;
TCHAR lpszFilePath[nLength] = { 0, };
GetModuleFileName(nullptr, lpszFilePath, nLength);
WriteConfigFile(lpszFilePath, _T("https://www.moga.doctor/freeware/IntelliEditSetup.msi"));

// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_DEMOAPP, szWindowClass, MAX_LOADSTRING);
Expand Down Expand Up @@ -243,6 +248,13 @@ INT_PTR CALLBACK AboutCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lP
return (INT_PTR)FALSE;
}

HWND hwndDialog = nullptr;
void UI_Callback(int, const std::wstring& strMessage)
{
SetWindowText(GetDlgItem(hwndDialog, IDC_STATUS), strMessage.c_str());
UpdateWindow(GetDlgItem(hwndDialog, IDC_STATUS));
}

bool g_bThreadRunning = false;
bool g_bNewUpdateFound = false;
DWORD WINAPI UpdateThreadProc(LPVOID lpParam)
Expand All @@ -253,8 +265,7 @@ DWORD WINAPI UpdateThreadProc(LPVOID lpParam)
const DWORD nLength = _MAX_PATH;
TCHAR lpszFilePath[nLength] = { 0, };
GetModuleFileName(nullptr, lpszFilePath, nLength);
// WriteConfigFile(lpszFilePath, _T("https://www.moga.doctor/freeware/IntelliEditSetup.msi"));
g_bNewUpdateFound = CheckForUpdates(lpszFilePath, _T("https://www.moga.doctor/freeware/genUp4win.xml"));
g_bNewUpdateFound = CheckForUpdates(lpszFilePath, _T("https://www.moga.doctor/freeware/genUp4win.xml"), UI_Callback);
g_bThreadRunning = false;

::ExitThread(0);
Expand All @@ -271,8 +282,9 @@ INT_PTR CALLBACK UpdateCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM l
switch (message)
{
case WM_INITDIALOG:
hwndDialog = hDlg; // used in UI_Callback
CenterWindow(hDlg);
SendMessage(GetDlgItem(hDlg, IDC_PROGRESS), (UINT)PBM_SETMARQUEE, (WPARAM)TRUE, (LPARAM)30);
// SendMessage(GetDlgItem(hDlg, IDC_PROGRESS), (UINT)PBM_SETMARQUEE, (WPARAM)TRUE, (LPARAM)30);
m_hUpdateThread = ::CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)UpdateThreadProc, hDlg, 0, &m_nUpdateThreadID);
m_nTimerID = SetTimer(hDlg, 0x1234, 100, nullptr);
return (INT_PTR)TRUE;
Expand Down
Binary file modified DemoApp/DemoApp.rc
Binary file not shown.
24 changes: 11 additions & 13 deletions genUp4win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ const std::wstring GetAppSettingsFilePath(const std::wstring& strFilePath, const
return lpszFullPath;
}

bool WriteConfigFile(const std::wstring& strFilePath, const std::wstring& strDownloadURL)
bool WriteConfigFile(const std::wstring& strFilePath, const std::wstring& strDownloadURL, fnCallback ParentCallback)
{
bool retVal = false;
CVersionInfo pVersionInfo;

if (pVersionInfo.Load(strFilePath.c_str()))
{
const std::wstring& strProductName = pVersionInfo.GetProductName();
OutputDebugString(strProductName.c_str());
try {
const HRESULT hr{ CoInitialize(nullptr) };
if (FAILED(hr))
Expand All @@ -83,13 +82,13 @@ bool WriteConfigFile(const std::wstring& strFilePath, const std::wstring& strDow
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException.GetErrorMessage(lpszErrorMessage, nErrorLength);
OutputDebugString(lpszErrorMessage);
ParentCallback(GENUP4WIN_ERROR, lpszErrorMessage);
}
}
return retVal;
}

bool ReadConfigFile(const std::wstring& strConfigURL, const std::wstring& strProductName, std::wstring& strLatestVersion, std::wstring& strDownloadURL)
bool ReadConfigFile(const std::wstring& strConfigURL, const std::wstring& strProductName, std::wstring& strLatestVersion, std::wstring& strDownloadURL, fnCallback ParentCallback)
{
HRESULT hResult = S_OK;
bool retVal = false;
Expand All @@ -103,7 +102,7 @@ bool ReadConfigFile(const std::wstring& strConfigURL, const std::wstring& strPro
{
CString strFileName = lpszFilePath;
strFileName.Replace(_T(".tmp"), _T(".xml"));
OutputDebugString(_T("Connecting..."));
ParentCallback(GENUP4WIN_INPROGRESS, _T("Connecting..."));
if ((hResult = URLDownloadToFile(nullptr, strConfigURL.c_str(), strFileName, 0, nullptr)) == S_OK)
{
try {
Expand All @@ -121,21 +120,21 @@ bool ReadConfigFile(const std::wstring& strConfigURL, const std::wstring& strPro
const int nErrorLength = 0x100;
TCHAR lpszErrorMessage[nErrorLength] = { 0, };
pException.GetErrorMessage(lpszErrorMessage, nErrorLength);
OutputDebugString(lpszErrorMessage);
ParentCallback(GENUP4WIN_ERROR, lpszErrorMessage);
}
}
else
{
_com_error pError(hResult);
LPCTSTR lpszErrorMessage = pError.ErrorMessage();
OutputDebugString(lpszErrorMessage);
ParentCallback(GENUP4WIN_ERROR, lpszErrorMessage);
}
}
}
return retVal;
}

bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strConfigURL)
bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strConfigURL, fnCallback ParentCallback)
{
HRESULT hResult = S_OK;
bool retVal = false;
Expand All @@ -146,7 +145,7 @@ bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strCon
{
const std::wstring& strProductName = pVersionInfo.GetProductName();
OutputDebugString(strProductName.c_str());
if (ReadConfigFile(strConfigURL, strProductName, strLatestVersion, strDownloadURL))
if (ReadConfigFile(strConfigURL, strProductName, strLatestVersion, strDownloadURL, ParentCallback))
{
bool bNewUpdateFound = (strLatestVersion.compare(pVersionInfo.GetProductVersionAsString()) != 0);
if (bNewUpdateFound)
Expand All @@ -161,7 +160,7 @@ bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strCon
{
CString strFileName = lpszFilePath;
strFileName.Replace(_T(".tmp"), DEFAULT_EXTENSION);
OutputDebugString(_T("Downloading..."));
ParentCallback(GENUP4WIN_INPROGRESS, _T("Downloading..."));
if ((hResult = URLDownloadToFile(nullptr, strDownloadURL.c_str(), strFileName, 0, nullptr)) == S_OK)
{
SHELLEXECUTEINFO pShellExecuteInfo;
Expand All @@ -174,20 +173,19 @@ bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strCon
pShellExecuteInfo.lpDirectory = nullptr;
pShellExecuteInfo.nShow = SW_SHOWNORMAL;
const bool bLauched = ShellExecuteEx(&pShellExecuteInfo);
OutputDebugString(bLauched ? _T("The installation started successfully") : _T("Installation failed"));
ParentCallback(GENUP4WIN_INPROGRESS, bLauched ? _T("The installation started successfully") : _T("Installation failed"));
retVal = true; // Download was successful
}
else
{
_com_error pError(hResult);
LPCTSTR lpszErrorMessage = pError.ErrorMessage();
OutputDebugString(lpszErrorMessage);
ParentCallback(GENUP4WIN_ERROR, lpszErrorMessage);
}
}
}
}
}
}

return retVal;
}
17 changes: 14 additions & 3 deletions genUp4win.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,34 @@ SOFTWARE. */
#ifdef __cplusplus

#include <string>
#include <functional>

#ifdef GENUP4WIN_EXPORTS
#define GENUP4WIN __declspec(dllexport)
#else
#define GENUP4WIN __declspec(dllimport)
#endif

typedef enum {
GENUP4WIN_ERROR = -1,
GENUP4WIN_OK,
GENUP4WIN_INPROGRESS
} GENUP4WIN_STATUS;

void StatusCallback(int, const std::wstring& strMessage) { OutputDebugString(strMessage.c_str()); };

typedef std::function<void(int, const std::wstring& strMessage)> fnCallback;

// Generates the configuration XML file path
GENUP4WIN const std::wstring GetAppSettingsFilePath(const std::wstring& strFilePath, const std::wstring& strProductName);

// Writes the configuration XML file; the result should be install on Web for futher access
GENUP4WIN bool WriteConfigFile(const std::wstring& strFilePath, const std::wstring& strDownloadURL);
GENUP4WIN bool WriteConfigFile(const std::wstring& strFilePath, const std::wstring& strDownloadURL, fnCallback = StatusCallback);

// Reads the config XML file (i.e. downloads it on local machine); the result is stored in `strLatestVersion` and `strDownloadURL`
GENUP4WIN bool ReadConfigFile(const std::wstring& strConfigURL, const std::wstring& strProductName, std::wstring& strLatestVersion, std::wstring& strDownloadURL);
GENUP4WIN bool ReadConfigFile(const std::wstring& strConfigURL, const std::wstring& strProductName, std::wstring& strLatestVersion, std::wstring& strDownloadURL, fnCallback = StatusCallback);

// Checks for updates; first, it downloads the config XML file; then, it actual downloads the installer
GENUP4WIN bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strConfigURL);
GENUP4WIN bool CheckForUpdates(const std::wstring& strFilePath, const std::wstring& strConfigURL, fnCallback = StatusCallback);

#endif
Binary file modified genUp4win.rc
Binary file not shown.
Binary file modified x64/Release/DemoApp.exe
Binary file not shown.
Binary file modified x64/Release/genUp4win.dll
Binary file not shown.

0 comments on commit 72a6b1e

Please sign in to comment.