From: lx-s Date: Wed, 8 Jul 2026 12:45:13 +0000 (+0200) Subject: Correctly unregister power notification X-Git-Tag: 2.6.0.0~75 X-Git-Url: https://repos.lx-s.de/gitweb/?a=commitdiff_plain;h=4ac1b61907b886f241f6b18849d42365a6126871;p=winmute.git Correctly unregister power notification --- 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_;