From 4ac1b61907b886f241f6b18849d42365a6126871 Mon Sep 17 00:00:00 2001 From: lx-s Date: Wed, 8 Jul 2026 14:45:13 +0200 Subject: [PATCH] Correctly unregister power notification --- WinMute/WinMute.cpp | 17 +++++++++++++++-- WinMute/WinMute.h | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index feb6c6d..1eb796b 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -88,7 +88,9 @@ static bool IsCurrentSessionRemoteable() noexcept reinterpret_cast(&dwGlassSessionId), &cbGlassSessionId); - if (lResult == ERROR_SUCCESS) { + if (lResult == ERROR_SUCCESS + && dwType == REG_DWORD + && cbGlassSessionId == sizeof(dwGlassSessionId)) { DWORD dwCurrentSessionId; if (ProcessIdToSessionId(GetCurrentProcessId(), &dwCurrentSessionId)) { isRemoteable = (dwCurrentSessionId != dwGlassSessionId); @@ -234,8 +236,11 @@ bool WinMute::Init() log.LogWinError(L"WTSRegisterSessionNotification", lastError); return false; } + wtsSessionNotificationRegistered_ = true; - if (!RegisterPowerSettingNotification(hWnd_, &GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE)) { + hPowerNotify_ = RegisterPowerSettingNotification( + hWnd_, &GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE); + if (hPowerNotify_ == nullptr) { const DWORD lastError = GetLastError(); ShowWindowsError(L"RegisterPowerSettingNotification", lastError); log.LogWinError(L"RegisterPowerSettingNotification", lastError); @@ -764,6 +769,14 @@ LRESULT CALLBACK WinMute::WindowProc( void WinMute::Unload() noexcept { + if (hPowerNotify_ != nullptr) { + UnregisterPowerSettingNotification(hPowerNotify_); + hPowerNotify_ = nullptr; + } + if (wtsSessionNotificationRegistered_) { + WTSUnRegisterSessionNotification(hWnd_); + wtsSessionNotificationRegistered_ = false; + } settings_.Unload(); } diff --git a/WinMute/WinMute.h b/WinMute/WinMute.h index 10a67f2..9703e33 100644 --- a/WinMute/WinMute.h +++ b/WinMute/WinMute.h @@ -65,6 +65,9 @@ private: bool muteOnBluetooth; } muteConfig_; + bool wtsSessionNotificationRegistered_ = false; + HPOWERNOTIFY hPowerNotify_ = nullptr; + TrayIcon wmTray_; TrayIcon updateTray_; WifiDetector wifiDetector_; -- 2.43.0