{"record":{"id":"265b89591c2e72a6","repo":"Flow-Launcher/Flow.Launcher","slug":"error-registering-for-power-notifications-marsha","errorCode":null,"errorMessage":"Error registering for power notifications: {Marshal.GetLastWin32Error()}","messagePattern":"Error registering for power notifications: (.+?)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"warning","filePath":"Flow.Launcher.Infrastructure/Win32Helper.cs","lineNumber":978,"sourceCode":"\n            _func = func;\n            _callback = new PDEVICE_NOTIFY_CALLBACK_ROUTINE(DeviceNotifyCallback);\n            _recipient = new DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS()\n            {\n                Callback = _callback,\n                Context = null\n            };\n\n            _recipientHandle = new StructSafeHandle<DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS>(_recipient);\n            _handle = PInvoke.PowerRegisterSuspendResumeNotification(\n                REGISTER_NOTIFICATION_FLAGS.DEVICE_NOTIFY_CALLBACK,\n                _recipientHandle,\n                out var handle) == WIN32_ERROR.ERROR_SUCCESS ?\n                new HPOWERNOTIFY(new IntPtr(handle)) :\n                HPOWERNOTIFY.Null;\n            if (_handle.IsNull)\n            {\n                throw new Win32Exception(\"Error registering for power notifications: \" + Marshal.GetLastWin32Error());\n            }\n        }\n\n        /// <summary>\n        /// Unregisters the sleep mode listener.\n        /// </summary>\n        public static void UnregisterSleepModeListener()\n        {\n            if (!_handle.IsNull)\n            {\n                PInvoke.PowerUnregisterSuspendResumeNotification(_handle);\n                _handle = HPOWERNOTIFY.Null;\n                _func = null;\n                _callback = null;\n                _recipientHandle = null;\n            }\n        }\n","sourceCodeStart":960,"sourceCodeEnd":996,"githubUrl":"https://github.com/Flow-Launcher/Flow.Launcher/blob/7fc63b07bbaa10a0f0b2601487913fcba9ed24b0/Flow.Launcher.Infrastructure/Win32Helper.cs#L960-L996","documentation":"Thrown as Win32Exception when PowerRegisterSuspendResumeNotification returns anything other than ERROR_SUCCESS, leaving _handle as HPOWERNOTIFY.Null. The message includes Marshal.GetLastWin32Error() to surface the OS-level reason. This API registers a callback to receive sleep/resume notifications and normally only fails under specific OS/security conditions.","triggerScenarios":"The OS rejected the callback registration due to an invalid recipient structure, a marshalling failure of the DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS, or (rarely) insufficient privilege; running on a Windows edition/configuration that restricts power notifications; the StructSafeHandle holding _recipient was disposed or invalid at call time; a P/Invoke signature mismatch producing a non-SUCCESS return.","commonSituations":"An older/locked-down Windows build (kiosk, embedded) disabling power notification registration; a bug in the StructSafeHandle marshalling causing the callback pointer to be invalid; running under a heavily restricted service account; a CsWin32 generator mismatch producing wrong binding for PowerRegisterSuspendResumeNotification.","solutions":["Decode Marshal.GetLastWin32Error() from the message (e.g. 87 ERROR_INVALID_PARAMETER, 5 ERROR_ACCESS_DENIED) to classify.","Verify the DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS and the callback delegate are kept alive (not GC'd) for the registration's lifetime — the _callback field is allocated for this reason; ensure it isn't nulled.","Confirm the OS supports PowerRegisterSuspendResumeNotification (Windows Vista+, desktop session).","Catch Win32Exception at the caller and degrade to a polling/lesser mechanism for power-state awareness if registration is unavailable.","Re-check the P/Invoke signature against the current Windows SDK if the HRESULT is unexpected."],"exampleFix":"// before\nif (_handle.IsNull)\n    throw new Win32Exception(\"Error registering for power notifications: \" + Marshal.GetLastWin32Error());\n\n// after — include the HRESULT and degrade gracefully\nif (_handle.IsNull)\n{\n    var err = Marshal.GetLastWin32Error();\n    Log.Exception(nameof(Win32Helper), $\"PowerRegisterSuspendResumeNotification failed (Win32 error {err}); power-state tracking disabled\", null);\n    // do not throw — continue without sleep/resume notifications\n}","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"try { Win32Helper.RegisterSleepModeListener(callback); }\ncatch (Win32Exception ex) when (ex.Message.Contains(\"power notifications\"))\n{ Log.Warn(...); // continue without sleep/resume notifications }","preventionTips":["Keep the callback delegate and the StructSafeHandle alive for the registration's lifetime to avoid GC invalidating the pointer.","Verify the P/Invoke signature matches the current Windows SDK / CsWin32 output.","Catch Win32Exception at the caller and degrade gracefully — power tracking is non-critical.","Confirm the OS edition supports power-notification registration before relying on it."],"tags":["win32","power","pinvoke","os"],"backgroundTag":null,"analyzedSha":"7fc63b07bbaa10a0f0b2601487913fcba9ed24b0","analyzedAt":"2026-08-13T14:54:20.914Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}