using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { public static partial class User32 { /// Registers the application to receive power setting notifications for the specific power setting event. /// /// Handle indicating where the power setting notifications are to be sent. For interactive applications, the Flags parameter should /// be zero, and the hRecipient parameter should be a window handle. For services, the Flags parameter should be one, and the /// hRecipient parameter should be a SERVICE_STATUS_HANDLE as returned from RegisterServiceCtrlHandlerEx. /// /// /// The GUID of the power setting for which notifications are to be sent. For more information see Registering for Power Events. /// /// /// /// /// Value /// Meaning /// /// /// DEVICE_NOTIFY_WINDOW_HANDLE 0 /// Notifications are sent using WM_POWERBROADCAST messages with a wParam parameter of PBT_POWERSETTINGCHANGE. /// /// /// DEVICE_NOTIFY_SERVICE_HANDLE 1 /// /// Notifications are sent to the HandlerEx callback function with a dwControl parameter of SERVICE_CONTROL_POWEREVENT and a /// dwEventType of PBT_POWERSETTINGCHANGE. /// /// /// /// /// /// Returns a notification handle for unregistering for power notifications. If the function fails, the return value is NULL. To get /// extended error information, call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerpowersettingnotification HPOWERNOTIFY // RegisterPowerSettingNotification( IN HANDLE hRecipient, IN LPCGUID PowerSettingGuid, IN DWORD Flags ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "e072222e-da66-4b36-a38f-f4b618eaa391")] public static extern SafeHPOWERSETTINGNOTIFY RegisterPowerSettingNotification([In] HANDLE hRecipient, in Guid PowerSettingGuid, [In] DEVICE_NOTIFY Flags); /// /// Registers to receive notification when the system is suspended or resumed. Similar to PowerRegisterSuspendResumeNotification, but /// operates in user mode and can take a window handle. /// /// /// /// This parameter contains parameters for subscribing to a power notification or a window handle representing the subscribing process. /// /// /// If Flags is DEVICE_NOTIFY_CALLBACK, hRecipient is interpreted as a pointer to a DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS /// structure. In this case, the callback function is DeviceNotifyCallbackRoutine. When the Callback function executes, the /// Type parameter is set indicating the type of event that occurred. Possible values include PBT_APMSUSPEND, /// PBT_APMRESUMESUSPEND, and PBT_APMRESUMEAUTOMATIC - see Power Management Events for more info. The Setting parameter /// is not used with suspend/resume notifications. /// /// If Flags is DEVICE_NOTIFY_WINDOW_HANDLE, hRecipient is a handle to the window to deliver events to. /// /// This parameter can be DEVICE_NOTIFY_WINDOW_HANDLE or DEVICE_NOTIFY_CALLBACK. /// /// A handle to the registration. Use this handle to unregister for notifications. /// If the function fails, the return value is NULL. To get extended error information call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registersuspendresumenotification HPOWERNOTIFY // RegisterSuspendResumeNotification( IN HANDLE hRecipient, IN DWORD Flags ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "6cd42d32-07e9-4cbd-83f9-6146b1cb54db")] public static extern SafeHSUSPRESUMENOTIFY RegisterSuspendResumeNotification([In] HANDLE hRecipient, [In] DEVICE_NOTIFY Flags); /// Unregisters the power setting notification. /// The handle returned from the RegisterPowerSettingNotification function. /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-unregisterpowersettingnotification BOOL // UnregisterPowerSettingNotification( IN HPOWERNOTIFY Handle ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "de1509f5-cf4c-448e-bb3b-08da6be53bfa")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool UnregisterPowerSettingNotification([In] HANDLE Handle); /// /// Cancels a registration to receive notification when the system is suspended or resumed. Similar to /// PowerUnregisterSuspendResumeNotification but operates in user mode. /// /// A handle to a registration obtained by calling the RegisterSuspendResumeNotification function. /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-unregistersuspendresumenotification BOOL // UnregisterSuspendResumeNotification( IN HPOWERNOTIFY Handle ); [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winuser.h", MSDNShortId = "d9307452-9670-4e9c-9df8-6a3b41d0bd2e")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool UnregisterSuspendResumeNotification([In] HANDLE Handle); /// Provides a for HPOWERNOTIFY that is disposed using . public class SafeHPOWERSETTINGNOTIFY : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeHPOWERSETTINGNOTIFY(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeHPOWERSETTINGNOTIFY() : base() { } /// protected override bool InternalReleaseHandle() => UnregisterPowerSettingNotification(handle); } /// Provides a for HPOWERNOTIFY that is disposed using . public class SafeHSUSPRESUMENOTIFY : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeHSUSPRESUMENOTIFY(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeHSUSPRESUMENOTIFY() : base() { } /// protected override bool InternalReleaseHandle() => UnregisterSuspendResumeNotification(handle); } /* CallNtPowerInformation CanUserWritePwrScheme DeletePwrScheme DeviceNotifyCallbackRoutine DevicePowerClose DevicePowerEnumDevices DevicePowerOpen DevicePowerSetDeviceState EnumPwrSchemes EFFECTIVE_POWER_MODE_CALLBACK GetActivePwrScheme GetCurrentPowerPolicies GetDevicePowerState GetPwrCapabilities GetPwrDiskSpindownRange GetSystemPowerStatus IsPwrHibernateAllowed IsPwrShutdownAllowed IsPwrSuspendAllowed IsSystemResumeAutomatic PowerCanRestoreIndividualDefaultPowerScheme PowerClearRequest PowerCreatePossibleSetting PowerCreateRequest PowerCreateSetting PowerDeleteScheme PowerDeterminePlatformRole PowerDeterminePlatformRoleEx PowerDuplicateScheme PowerEnumerate PowerGetActiveScheme PowerImportPowerScheme PowerIsSettingRangeDefined PowerReadACDefaultIndex PowerReadACValue PowerReadACValueIndex PowerReadDCDefaultIndex PowerReadDCValue PowerReadDCValueIndex PowerReadDescription PowerReadFriendlyName PowerReadIconResourceSpecifier PowerReadPossibleDescription PowerReadPossibleFriendlyName PowerReadPossibleValue PowerReadSettingAttributes PowerReadValueIncrement PowerReadValueMax PowerReadValueMin PowerReadValueUnitsSpecifier PowerRegisterForEffectivePowerModeNotifications PowerRegisterSuspendResumeNotification PowerRemovePowerSetting PowerReplaceDefaultPowerSchemes PowerReportThermalEvent PowerRestoreDefaultPowerSchemes PowerRestoreIndividualDefaultPowerScheme PowerSetActiveScheme PowerSetRequest PowerSettingAccessCheck PowerSettingAccessCheckEx PowerSettingRegisterNotification PowerSettingUnregisterNotification PowerUnregisterFromEffectivePowerModeNotifications PowerUnregisterSuspendResumeNotification PowerWriteACDefaultIndex PowerWriteACValueIndex PowerWriteDCDefaultIndex PowerWriteDCValueIndex PowerWriteDescription PowerWriteFriendlyName PowerWriteIconResourceSpecifier PowerWritePossibleDescription PowerWritePossibleFriendlyName PowerWritePossibleValue PowerWriteSettingAttributes PowerWriteValueIncrement PowerWriteValueMax PowerWriteValueMin PowerWriteValueUnitsSpecifier ReadGlobalPwrPolicy ReadProcessorPwrScheme ReadPwrScheme RequestWakeupLatency SetActivePwrScheme SetSuspendState SetSystemPowerState SetThreadExecutionState WriteGlobalPwrPolicy WriteProcessorPwrScheme WritePwrScheme */ } }