From 4bfcf90e2a39876a1e3ac4257bcbc69830594f84 Mon Sep 17 00:00:00 2001 From: dreamforceinc Date: Tue, 28 Nov 2023 23:38:50 +0300 Subject: [PATCH] Added check for error allocating heap memory. --- wCenterWindow/resource.h | 9 +++++---- wCenterWindow/wCenterWindow.cpp | 19 ++++++++++++------- wCenterWindow/wCenterWindow.rc | Bin 13226 -> 13378 bytes 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/wCenterWindow/resource.h b/wCenterWindow/resource.h index 84b2373..1d7ad27 100644 --- a/wCenterWindow/resource.h +++ b/wCenterWindow/resource.h @@ -20,9 +20,10 @@ #define IDS_ERR_POPUP 118 #define IDS_ERR_HOOK 119 #define IDS_ERR_TIMER 120 -#define IDS_ERR_MAXMIN 121 -#define IDS_RUNNING 122 -#define IDT_TIMER 123 +#define IDS_ERR_HEAP 121 +#define IDS_ERR_MAXMIN 122 +#define IDS_RUNNING 123 +#define IDT_TIMER 124 #define IDR_MAINFRAME 128 #define IDD_MANUAL_EDITING 129 #define IDC_EDIT_X 1000 @@ -50,6 +51,6 @@ #define _APS_NEXT_RESOURCE_VALUE 130 #define _APS_NEXT_COMMAND_VALUE 32771 #define _APS_NEXT_CONTROL_VALUE 1015 -#define _APS_NEXT_SYMED_VALUE 124 +#define _APS_NEXT_SYMED_VALUE 125 #endif #endif diff --git a/wCenterWindow/wCenterWindow.cpp b/wCenterWindow/wCenterWindow.cpp index a16a082..c0bf391 100644 --- a/wCenterWindow/wCenterWindow.cpp +++ b/wCenterWindow/wCenterWindow.cpp @@ -36,7 +36,7 @@ CLogger logger(TEXT(PRODUCT_NAME), TEXT(VERSION_STR)); NOTIFYICONDATAW nid = { 0 }; MENUITEMINFO mii = { 0 }; -LPVOID szBuffer; +LPVOID szWinTitleBuffer = nullptr; // {2D7B7F30-4B5F-4380-9807-57D7A2E37F6C} // static const GUID guid = { 0x2d7b7f30, 0x4b5f, 0x4380, { 0x98, 0x7, 0x57, 0xd7, 0xa2, 0xe3, 0x7f, 0x6c } }; @@ -157,7 +157,12 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd HandlingTrayIcon(); hHeap = GetProcessHeap(); - szBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH); + szWinTitleBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, MAX_WINTITLE_BUFFER_LENGTH); + if (nullptr == szWinTitleBuffer) + { + ShowError(IDS_ERR_HEAP); + return FALSE; + } MSG msg; BOOL bRet; @@ -182,9 +187,9 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd Shell_NotifyIconW(NIM_DELETE, &nid); DestroyIcon(hIcon); - logger.Out(L"Exit from the %s() function, msg.wParam = %d", TEXT(__FUNCTION__), (int)msg.wParam); + logger.Out(L"Exit from the %s() function, msg.wParam = 0x%08X", TEXT(__FUNCTION__), (int)msg.wParam); - HeapFree(hHeap, NULL, szBuffer); + HeapFree(hHeap, NULL, szWinTitleBuffer); CloseHandle(hUpdater); return (int)msg.wParam; @@ -502,14 +507,14 @@ BOOL CALLBACK DlgProc(HWND hDlg, UINT dlgmsg, WPARAM wParam, LPARAM lParam) BOOL IsWindowApprooved(HWND hFW) { - logger.Out(L"Entering the %s() function, handle = 0x%08X", TEXT(__FUNCTION__), hFW); + logger.Out(L"Entering the %s() function", TEXT(__FUNCTION__)); bool bApprooved = FALSE; if (hFW) { - if (GetWindowTextW(hFW, (LPWSTR)szBuffer, MAX_WINTITLE_BUFFER_LENGTH)) + if (GetWindowTextW(hFW, (LPWSTR)szWinTitleBuffer, MAX_WINTITLE_BUFFER_LENGTH)) { - logger.Out(L"%s(%d): Window title: '%s'", TEXT(__FUNCTION__), __LINE__, (LPWSTR)szBuffer); + logger.Out(L"%s(%d): Window handle: 0x%08X. Title: '%s'", TEXT(__FUNCTION__), __LINE__, hFW, (LPWSTR)szWinTitleBuffer); } if (IsIconic(hFW)) diff --git a/wCenterWindow/wCenterWindow.rc b/wCenterWindow/wCenterWindow.rc index 28ec78acabea2081343c34c52fcb7e537d4c1cb4..08adaec0c6b84ee48773552997e22ddc05c044bc 100644 GIT binary patch delta 90 zcmZ3LekfyujXtXfgDZpMe>fH0pSnIVy(gdvqdfgy>ZlpzfWQyGeY jyiA5XAejN=B{CEMS-C(~E>L|DL*-;gJ;lv7`gtq>DD@SN delta 12 TcmX?C|L!6