From b9099d01c20cc1b95e4237a2499828aee34221ac Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Sun, 28 Jun 2020 00:13:14 +0200 Subject: Windows: Fix failure to detect suspension of machine that caused mounted volume not be dismount even if "Entering power saving mode" is checked in Preferences. This fix implements compatibility with Windows 10 Modern Standby and Windows 8.1 Connected Standby power model. --- src/Mount/Mount.c | 59 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/Mount/Mount.c b/src/Mount/Mount.c index e73859f5..ece39512 100644 --- a/src/Mount/Mount.c +++ b/src/Mount/Mount.c @@ -64,6 +64,17 @@ typedef BOOL (WINAPI *WTSREGISTERSESSIONNOTIFICATION)(HWND, DWORD); typedef BOOL (WINAPI *WTSUNREGISTERSESSIONNOTIFICATION)(HWND); +#ifndef _HPOWERNOTIFY_DEF_ +#define _HPOWERNOTIFY_DEF_ + +typedef PVOID HPOWERNOTIFY; +typedef HPOWERNOTIFY *PHPOWERNOTIFY; + +#endif + +typedef HPOWERNOTIFY (WINAPI *REGISTERSUSPENDRESUMENOTIFICATION)(HANDLE hRecipient, DWORD Flags); +typedef BOOL (WINAPI *UNREGISTERSUSPENDRESUMENOTIFICATION) (HPOWERNOTIFY Handle); + using namespace VeraCrypt; enum timer_ids @@ -188,7 +199,13 @@ static HMODULE hWtsLib = NULL; static WTSREGISTERSESSIONNOTIFICATION fnWtsRegisterSessionNotification = NULL; static WTSUNREGISTERSESSIONNOTIFICATION fnWtsUnRegisterSessionNotification = NULL; -static void RegisterWtsNotification(HWND hWnd) +// Used to opt-in to receive notification about power events. +// This is mandatory to support Windows 10 Modern Standby and Windows 8.1 Connected Standby power model. +// https://docs.microsoft.com/en-us/windows-hardware/design/device-experiences/prepare-software-for-modern-standby +// https://docs.microsoft.com/en-us/windows/win32/w8cookbook/desktop-activity-moderator?redirectedfrom=MSDN +static HPOWERNOTIFY g_hPowerNotify = NULL; + +static void RegisterWtsAndPowerNotification(HWND hWnd) { if (!hWtsLib) { @@ -215,9 +232,19 @@ static void RegisterWtsNotification(HWND hWnd) } } } + + if (IsOSAtLeast (WIN_8)) + { + REGISTERSUSPENDRESUMENOTIFICATION fnRegisterSuspendResumeNotification = (REGISTERSUSPENDRESUMENOTIFICATION) GetProcAddress (GetModuleHandle (L"user32.dll"), "RegisterSuspendResumeNotification"); + if (fnRegisterSuspendResumeNotification) + { + g_hPowerNotify = fnRegisterSuspendResumeNotification ((HANDLE) hWnd, DEVICE_NOTIFY_WINDOW_HANDLE); + } + + } } -static void UnregisterWtsNotification(HWND hWnd) +static void UnregisterWtsAndPowerNotification(HWND hWnd) { if (hWtsLib && fnWtsUnRegisterSessionNotification) { @@ -227,6 +254,14 @@ static void UnregisterWtsNotification(HWND hWnd) fnWtsRegisterSessionNotification = NULL; fnWtsUnRegisterSessionNotification = NULL; } + + if (IsOSAtLeast (WIN_8) && g_hPowerNotify) + { + UNREGISTERSUSPENDRESUMENOTIFICATION fnUnregisterSuspendResumeNotification = (UNREGISTERSUSPENDRESUMENOTIFICATION) GetProcAddress (GetModuleHandle (L"user32.dll"), "UnregisterSuspendResumeNotification"); + if (fnUnregisterSuspendResumeNotification) + fnUnregisterSuspendResumeNotification (g_hPowerNotify); + g_hPowerNotify = NULL; + } } static std::vector GetReadChildNodes (MSXML2::IXMLDOMNodeListPtr childs) @@ -435,7 +470,7 @@ void EndMainDlg (HWND hwndDlg) KillTimer (hwndDlg, TIMER_ID_MAIN); KillTimer (hwndDlg, TIMER_ID_UPDATE_DEVICE_LIST); TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); EndDialog (hwndDlg, 0); } } @@ -7284,7 +7319,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa } if (TaskBarIconMutex != NULL) - RegisterWtsNotification(hwndDlg); + RegisterWtsAndPowerNotification(hwndDlg); DoPostInstallTasks (hwndDlg); ResetCurrentDirectory (); } @@ -7369,7 +7404,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa } TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); } EndMainDlg (hwndDlg); localcleanup (); @@ -7587,7 +7622,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa && GetDriverRefCount () < 2) { TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); EndMainDlg (hwndDlg); } } @@ -7714,7 +7749,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa EnumWindows (CloseTCWindowsEnum, 0); TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); SendMessage (hwndDlg, WM_COMMAND, sel, 0); } } @@ -7735,7 +7770,7 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa case TC_APPMSG_CLOSE_BKG_TASK: if (TaskBarIconMutex != NULL) TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); return 1; @@ -8411,12 +8446,12 @@ BOOL CALLBACK MainDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lPa if (bEnableBkgTask) { TaskBarIconAdd (hwndDlg); - RegisterWtsNotification(hwndDlg); + RegisterWtsAndPowerNotification(hwndDlg); } else { TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); if (MainWindowHidden) EndMainDlg (hwndDlg); } @@ -9870,7 +9905,7 @@ BOOL TaskBarIconChange (HWND hwnd, int iconId) ScreenDPI >= 120 ? 0 : 16, (ScreenDPI >= 120 ? LR_DEFAULTSIZE : 0) | LR_SHARED - | LR_DEFAULTCOLOR : LR_VGACOLOR); + | LR_DEFAULTCOLOR); return Shell_NotifyIcon (NIM_MODIFY, &tnid); } @@ -10415,7 +10450,7 @@ static void HandleHotKey (HWND hwndDlg, WPARAM wParam) MessageBeep (0xFFFFFFFF); } TaskBarIconRemove (hwndDlg); - UnregisterWtsNotification(hwndDlg); + UnregisterWtsAndPowerNotification(hwndDlg); EndMainDlg (hwndDlg); break; -- cgit v1.2.3