]> repos.lx-s.de Git - winmute.git/commitdiff
Correctly unregister power notification
authorlx-s <lx-s@users.noreply.github.com>
Wed, 8 Jul 2026 12:45:13 +0000 (14:45 +0200)
committerlx-s <lx-s@users.noreply.github.com>
Wed, 8 Jul 2026 12:45:13 +0000 (14:45 +0200)
WinMute/WinMute.cpp
WinMute/WinMute.h

index feb6c6d60dad5f0ebe52838640d1fd0462b9fbfe..1eb796b9ac83e414c538574701cdf257e9d2b963 100644 (file)
@@ -88,7 +88,9 @@ static bool IsCurrentSessionRemoteable() noexcept
             reinterpret_cast<BYTE*>(&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();
 }
 
index 10a67f2c54c29323fee27be39f2924dd32d97547..9703e3341511772cb93c8dd0c00ad23b72bd0369 100644 (file)
@@ -65,6 +65,9 @@ private:
       bool muteOnBluetooth;
    } muteConfig_;
 
+   bool wtsSessionNotificationRegistered_ = false;
+   HPOWERNOTIFY hPowerNotify_ = nullptr;
+
    TrayIcon wmTray_;
    TrayIcon updateTray_;
    WifiDetector wifiDetector_;