using System; using System.Runtime.InteropServices; using Vanara.InteropServices; namespace Vanara.PInvoke { public static partial class PowrProf { /// Function class for effective power mode callback. /// Indicates the effective power mode the system is running in /// User-specified opaque context. This context would have been passed in at registration in PowerRegisterForEffectivePowerModeNotifications. /// /// Immediately after registration, this callback will be invoked with the current value of the power setting. If the registration /// occurs while the power setting is changing, you may receive multiple callbacks; the last callback is the most recent update. /// // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-effective_power_mode_callback void // EFFECTIVE_POWER_MODE_CALLBACK( EFFECTIVE_POWER_MODE Mode, VOID *Context ); [PInvokeData("powersetting.h", MSDNShortId = "47DD6801-5120-44D3-9EE4-58CCDB4B933A")] [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void EFFECTIVE_POWER_MODE_CALLBACK(EFFECTIVE_POWER_MODE Mode, IntPtr Context); /// Flags for . [PInvokeData("powersetting.h", MSDNShortId = "0fbca717-2367-4407-8094-3eb2b717b59c")] public enum DEVICE_NOTIFY { /// /// The Recipient parameter is a handle to a service.Use the CreateService or OpenService function to obtain this handle. /// DEVICE_NOTIFY_SERVICE_HANDLE = 1, /// The Recipient parameter is a pointer to a callback function to call when the power setting changes. DEVICE_NOTIFY_CALLBACK = 2, } /// Indicates the effective power mode the system is running. // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/ne-powersetting-effective_power_mode typedef enum // EFFECTIVE_POWER_MODE { EffectivePowerModeBatterySaver, EffectivePowerModeBetterBattery, EffectivePowerModeBalanced, // EffectivePowerModeHighPerformance, EffectivePowerModeMaxPerformance, EffectivePowerModeInvalid } ; [PInvokeData("powersetting.h", MSDNShortId = "8FA09CC0-99E7-4B05-88A0-2AF406C7B60C")] public enum EFFECTIVE_POWER_MODE { /// The system is in battery saver mode. EffectivePowerModeBatterySaver, /// The system is in the better battery effective power mode. EffectivePowerModeBetterBattery, /// The system is in the balanced effective power mode. EffectivePowerModeBalanced, /// The system is in the high performance effective power mode. EffectivePowerModeHighPerformance, /// The system is in the maximum performance effective power mode. EffectivePowerModeMaxPerformance, /// The system is in an invalid effective power mode due to an internal error. EffectivePowerModeInvalid, } /// Retrieves the active power scheme and returns a GUID that identifies the scheme. /// This parameter is reserved for future use and must be set to NULL. /// /// A pointer that receives a pointer to a GUID structure. Use the LocalFree function to free this memory. /// /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powergetactivescheme DWORD // PowerGetActiveScheme( HKEY UserRootPowerKey, GUID **ActivePolicyGuid ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "cd72562c-8987-40c1-89c7-04a95b5f1fd0")] public static extern Win32Error PowerGetActiveScheme([Optional] HKEY UserRootPowerKey, out SafeLocalHandle ActivePolicyGuid); /// Retrieves the active power scheme and returns a GUID that identifies the scheme. /// Receives a GUID structure. /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powergetactivescheme DWORD // PowerGetActiveScheme( HKEY UserRootPowerKey, GUID **ActivePolicyGuid ); [PInvokeData("powersetting.h", MSDNShortId = "cd72562c-8987-40c1-89c7-04a95b5f1fd0")] public static Win32Error PowerGetActiveScheme(out Guid ActivePolicyGuid) { var err = PowerGetActiveScheme(default, out var h); ActivePolicyGuid = err.Succeeded ? h.ToStructure() : default; return err; } /// Retrieves the AC power value for the specified power setting. /// This parameter is reserved for future use and must be set to NULL. /// The identifier of the power scheme. /// /// /// The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use /// NO_SUBGROUP_GUID to retrieve the setting for the default power scheme. /// /// /// /// Value /// Meaning /// /// /// NO_SUBGROUP_GUID fea3413e-7e05-4911-9a71-700331f1c294 /// Settings in this subgroup are part of the default power scheme. /// /// /// GUID_DISK_SUBGROUP 0012ee47-9041-4b5d-9b77-535fba8b1442 /// Settings in this subgroup control power management configuration of the system's hard disk drives. /// /// /// GUID_SYSTEM_BUTTON_SUBGROUP 4f971e89-eebd-4455-a8de-9e59040e7347 /// Settings in this subgroup control configuration of the system power buttons. /// /// /// GUID_PROCESSOR_SETTINGS_SUBGROUP 54533251-82be-4824-96c1-47b60b740d00 /// Settings in this subgroup control configuration of processor power management features. /// /// /// GUID_VIDEO_SUBGROUP 7516b95f-f776-4464-8c53-06167f40cc99 /// Settings in this subgroup control configuration of the video power management features. /// /// /// GUID_BATTERY_SUBGROUP e73a048d-bf27-4f12-9731-8b2076e8891f /// Settings in this subgroup control battery alarm trip points and actions. /// /// /// GUID_SLEEP_SUBGROUP 238C9FA8-0AAD-41ED-83F4-97BE242C8F20 /// Settings in this subgroup control system sleep settings. /// /// /// GUID_PCIEXPRESS_SETTINGS_SUBGROUP 501a4d13-42af-4429-9fd1-a8218c268e20 /// Settings in this subgroup control PCI Express settings. /// /// /// /// The identifier of the power setting. /// /// A pointer to a variable that receives the type of data for the value. The possible values are listed in Registry Value Types. /// This parameter can be NULL and the type of data is not returned. /// /// /// A pointer to a buffer that receives the data value. If this parameter is NULL, the BufferSize parameter receives the /// required buffer size. /// /// /// A pointer to a variable that contains the size of the buffer pointed to by the Buffer parameter. /// /// If the Buffer parameter is NULL, the function returns ERROR_SUCCESS and the variable receives the required buffer size. /// /// /// If the specified buffer size is not large enough to hold the requested data, the function returns ERROR_MORE_DATA and the /// variable receives the required buffer size. /// /// /// /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. If the buffer size /// specified by the BufferSize parameter is too small, ERROR_MORE_DATA will be returned and the DWORD pointed to by /// the BufferSize parameter will be filled in with the required buffer size. /// // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powerreadacvalue DWORD PowerReadACValue( HKEY // RootPowerKey, const GUID *SchemeGuid, const GUID *SubGroupOfPowerSettingsGuid, const GUID *PowerSettingGuid, PULONG Type, LPBYTE // Buffer, LPDWORD BufferSize ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "b0afaf75-72cc-48a3-bbf2-0000cb85f2e2")] public static extern Win32Error PowerReadACValue([Optional] HKEY RootPowerKey, in Guid SchemeGuid, in Guid SubGroupOfPowerSettingsGuid, in Guid PowerSettingGuid, out REG_VALUE_TYPE Type, IntPtr Buffer, ref uint BufferSize); /// Retrieves the DC power value for the specified power setting. /// This parameter is reserved for future use and must be set to NULL. /// The identifier of the power scheme. /// /// /// The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use /// NO_SUBGROUP_GUID to retrieve the setting for the default power scheme. /// /// /// /// Value /// Meaning /// /// /// NO_SUBGROUP_GUID fea3413e-7e05-4911-9a71-700331f1c294 /// Settings in this subgroup are part of the default power scheme. /// /// /// GUID_DISK_SUBGROUP 0012ee47-9041-4b5d-9b77-535fba8b1442 /// Settings in this subgroup control power management configuration of the system's hard disk drives. /// /// /// GUID_SYSTEM_BUTTON_SUBGROUP 4f971e89-eebd-4455-a8de-9e59040e7347 /// Settings in this subgroup control configuration of the system power buttons. /// /// /// GUID_PROCESSOR_SETTINGS_SUBGROUP 54533251-82be-4824-96c1-47b60b740d00 /// Settings in this subgroup control configuration of processor power management features. /// /// /// GUID_VIDEO_SUBGROUP 7516b95f-f776-4464-8c53-06167f40cc99 /// Settings in this subgroup control configuration of the video power management features. /// /// /// GUID_BATTERY_SUBGROUP e73a048d-bf27-4f12-9731-8b2076e8891f /// Settings in this subgroup control battery alarm trip points and actions. /// /// /// GUID_SLEEP_SUBGROUP 238C9FA8-0AAD-41ED-83F4-97BE242C8F20 /// Settings in this subgroup control system sleep settings. /// /// /// GUID_PCIEXPRESS_SETTINGS_SUBGROUP 501a4d13-42af-4429-9fd1-a8218c268e20 /// Settings in this subgroup control PCI Express settings. /// /// /// /// The identifier of the power setting. /// /// A pointer to a variable that receives the type of data for the value. The possible values are listed in Registry Value Types. /// This parameter can be NULL and the type of data is not returned. /// /// /// A pointer to a variable that receives the data value. If this parameter is NULL, the BufferSize parameter receives the /// required buffer size. /// /// /// A pointer to a variable that contains the size of the buffer pointed to by the Buffer parameter. /// /// If the Buffer parameter is NULL, the function returns ERROR_SUCCESS and the variable receives the required buffer size. /// /// /// If the specified buffer size is not large enough to hold the requested data, the function returns ERROR_MORE_DATA and the /// variable receives the required buffer size. /// /// /// /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. If the buffer size /// specified by the BufferSize parameter is too small, ERROR_MORE_DATA will be returned and the DWORD pointed to by /// the BufferSize parameter will be filled in with the required buffer size. /// // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powerreaddcvalue DWORD PowerReadDCValue( HKEY // RootPowerKey, const GUID *SchemeGuid, const GUID *SubGroupOfPowerSettingsGuid, const GUID *PowerSettingGuid, PULONG Type, PUCHAR // Buffer, LPDWORD BufferSize ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "c439c478-e882-41bf-a95a-82d36382174b")] public static extern Win32Error PowerReadDCValue([Optional] HKEY RootPowerKey, in Guid SchemeGuid, in Guid SubGroupOfPowerSettingsGuid, in Guid PowerSettingGuid, out REG_VALUE_TYPE Type, IntPtr Buffer, ref uint BufferSize); /// Registers a callback to receive effective power mode change notifications. /// /// /// Supplies the maximum effective power mode version the caller understands. If the effective power mode comes from a later /// version, it is reduced to a compatible version that is then passed to the callback. /// /// As of Windows 10, version 1809 the only understood version is EFFECTIVE_POWER_MODE_V1. /// /// /// A pointer to the callback to call when the effective power mode changes. This will also be called once upon registration to /// supply the current mode. If multiple callbacks are registered using this API, those callbacks can be called concurrently. /// /// Caller-specified opaque context. /// A handle to the registration. Use this handle to unregister for notifications. /// Returns S_OK (zero) if the call was successful, and a nonzero value if the call failed. /// /// Immediately after registration, the callback will be invoked with the current value of the power setting. If the registration /// occurs while the power mode is changing, you may receive multiple callbacks; the last callback is the most recent update. /// // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powerregisterforeffectivepowermodenotifications // HRESULT PowerRegisterForEffectivePowerModeNotifications( ULONG Version, EFFECTIVE_POWER_MODE_CALLBACK *Callback, VOID *Context, // VOID **RegistrationHandle ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "3C87643F-A8DA-4230-A216-8F46629BB6FB")] public static extern HRESULT PowerRegisterForEffectivePowerModeNotifications(uint Version, EFFECTIVE_POWER_MODE_CALLBACK Callback, IntPtr Context, out SafeEffectivePowerModeNotificationHandle RegistrationHandle); /// Sets the active power scheme for the current user. /// This parameter is reserved for future use and must be set to NULL. /// The identifier of the power scheme. /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powersetactivescheme DWORD // PowerSetActiveScheme( HKEY UserRootPowerKey, const GUID *SchemeGuid ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "e56bc3f4-2141-4be7-8479-12f8d59971af")] public static extern Win32Error PowerSetActiveScheme([Optional] HKEY UserRootPowerKey, in Guid SchemeGuid); /// Registers to receive notification when a power setting changes. /// A GUID that represents the power setting. /// /// Information about the recipient of the notification. This parameter can be one of the following values: /// /// /// Value /// Meaning /// /// /// DEVICE_NOTIFY_SERVICE_HANDLE /// The Recipient parameter is a handle to a service.Use the CreateService or OpenService function to obtain this handle. /// /// /// DEVICE_NOTIFY_CALLBACK /// The Recipient parameter is a pointer to a callback function to call when the power setting changes. /// /// /// /// A handle to the recipient of the notifications. /// A handle to the registration. Use this handle to unregister for notifications. /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. /// /// Immediately after registration, the callback will be invoked with the current value of the power setting. If the registration /// occurs while the power setting is changing, you may receive multiple callbacks; the last callback is the most recent update. /// // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powersettingregisternotification DWORD // PowerSettingRegisterNotification( LPCGUID SettingGuid, DWORD Flags, HANDLE Recipient, PHPOWERNOTIFY RegistrationHandle ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "0fbca717-2367-4407-8094-3eb2b717b59c")] public static extern Win32Error PowerSettingRegisterNotification(in Guid SettingGuid, DEVICE_NOTIFY Flags, in DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS Recipient, out SafeHPOWERNOTIFY RegistrationHandle); /// Cancels a registration to receive notification when a power setting changes. /// A handle to a registration obtained by calling the PowerSettingRegisterNotification function. /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powersettingunregisternotification DWORD // PowerSettingUnregisterNotification( HPOWERNOTIFY RegistrationHandle ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "9853c347-4528-43bb-8326-13bcd819b8a6")] public static extern Win32Error PowerSettingUnregisterNotification(HANDLE RegistrationHandle); /// /// Unregisters from effective power mode change notifications. This function is intended to be called from cleanup code and will /// wait for all callbacks to complete before unregistering. /// /// /// The handle corresponding to a single power mode registration. This handle should have been saved by the caller after the call to /// PowerRegisterForEffectivePowerModeNotifications and passed in here. /// /// Returns S_OK (zero) if the call was successful, and a nonzero value if the call failed. /// /// Immediately after registration, the callback will be invoked with the current value of the power setting. If the registration /// occurs while the power setting is changing, you may receive multiple callbacks; the last callback is the most recent update. /// // https://docs.microsoft.com/en-us/windows/desktop/api/powersetting/nf-powersetting-powerunregisterfromeffectivepowermodenotifications // HRESULT PowerUnregisterFromEffectivePowerModeNotifications( VOID *RegistrationHandle ); [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("powersetting.h", MSDNShortId = "6E9AB09B-B082-406C-8F2D-43BEA04C19E0")] public static extern HRESULT PowerUnregisterFromEffectivePowerModeNotifications(HANDLE RegistrationHandle); /// Sets the AC value index of the specified power setting. /// This parameter is reserved for future use and must be set to NULL. /// The identifier of the power scheme. /// /// /// The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use /// NO_SUBGROUP_GUID to refer to the default power scheme. /// /// /// /// /// Value /// Meaning /// /// /// NO_SUBGROUP_GUIDfea3413e-7e05-4911-9a71-700331f1c294 /// Settings in this subgroup are part of the default power scheme. /// /// /// GUID_DISK_SUBGROUP0012ee47-9041-4b5d-9b77-535fba8b1442 /// Settings in this subgroup control power management configuration of the system's hard disk drives. /// /// /// GUID_SYSTEM_BUTTON_SUBGROUP4f971e89-eebd-4455-a8de-9e59040e7347 /// Settings in this subgroup control configuration of the system power buttons. /// /// /// GUID_PROCESSOR_SETTINGS_SUBGROUP54533251-82be-4824-96c1-47b60b740d00 /// Settings in this subgroup control configuration of processor power management features. /// /// /// GUID_VIDEO_SUBGROUP7516b95f-f776-4464-8c53-06167f40cc99 /// Settings in this subgroup control configuration of the video power management features. /// /// /// GUID_BATTERY_SUBGROUPe73a048d-bf27-4f12-9731-8b2076e8891f /// Settings in this subgroup control battery alarm trip points and actions. /// /// /// GUID_SLEEP_SUBGROUP238C9FA8-0AAD-41ED-83F4-97BE242C8F20 /// Settings in this subgroup control system sleep settings. /// /// /// GUID_PCIEXPRESS_SETTINGS_SUBGROUP501a4d13-42af-4429-9fd1-a8218c268e20 /// Settings in this subgroup control PCI Express settings. /// /// /// /// /// The identifier of the power setting. /// The AC value index. /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. // DWORD WINAPI PowerWriteACValueIndex( _In_opt_ HKEY RootPowerKey, _In_ const GUID *SchemeGuid, _In_opt_ const GUID // *SubGroupOfPowerSettingsGuid, _In_opt_ const GUID *PowerSettingGuid, _In_ DWORD AcValueIndex); https://msdn.microsoft.com/en-us/library/windows/desktop/aa372765(v=vs.85).aspx [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("Powersetting.h;", MSDNShortId = "aa372765")] public static extern Win32Error PowerWriteACValueIndex([In, Optional] HKEY RootPowerKey, in Guid SchemeGuid, [Optional] in Guid SubGroupOfPowerSettingsGuid, [Optional] in Guid PowerSettingGuid, uint AcValueIndex); /// Sets the DC index of the specified power setting. /// This parameter is reserved for future use and must be set to NULL. /// The identifier of the power scheme. /// /// /// The subgroup of power settings. This parameter can be one of the following values defined in WinNT.h. Use /// NO_SUBGROUP_GUID to refer to the default power scheme. /// /// /// /// /// Value /// Meaning /// /// /// NO_SUBGROUP_GUIDfea3413e-7e05-4911-9a71-700331f1c294 /// Settings in this subgroup are part of the default power scheme. /// /// /// GUID_DISK_SUBGROUP0012ee47-9041-4b5d-9b77-535fba8b1442 /// Settings in this subgroup control power management configuration of the system's hard disk drives. /// /// /// GUID_SYSTEM_BUTTON_SUBGROUP4f971e89-eebd-4455-a8de-9e59040e7347 /// Settings in this subgroup control configuration of the system power buttons. /// /// /// GUID_PROCESSOR_SETTINGS_SUBGROUP54533251-82be-4824-96c1-47b60b740d00 /// Settings in this subgroup control configuration of processor power management features. /// /// /// GUID_VIDEO_SUBGROUP7516b95f-f776-4464-8c53-06167f40cc99 /// Settings in this subgroup control configuration of the video power management features. /// /// /// GUID_BATTERY_SUBGROUPe73a048d-bf27-4f12-9731-8b2076e8891f /// Settings in this subgroup control battery alarm trip points and actions. /// /// /// GUID_SLEEP_SUBGROUP238C9FA8-0AAD-41ED-83F4-97BE242C8F20 /// Settings in this subgroup control system sleep settings. /// /// /// GUID_PCIEXPRESS_SETTINGS_SUBGROUP501a4d13-42af-4429-9fd1-a8218c268e20 /// Settings in this subgroup control PCI Express settings. /// /// /// /// /// The identifier of the power setting. /// The DC value index. /// Returns ERROR_SUCCESS (zero) if the call was successful, and a nonzero value if the call failed. // DWORD WINAPI PowerWriteDCValueIndex( _In_opt_ HKEY RootPowerKey, _In_ const GUID *SchemeGuid, _In_opt_ const GUID // *SubGroupOfPowerSettingsGuid, _In_opt_ const GUID *PowerSettingGuid, _In_ DWORD DcValueIndex); https://msdn.microsoft.com/en-us/library/windows/desktop/aa372769(v=vs.85).aspx [DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)] [PInvokeData("Powersetting.h;", MSDNShortId = "aa372769")] public static extern Win32Error PowerWriteDCValueIndex([Optional] HKEY RootPowerKey, in Guid SchemeGuid, [Optional] in Guid SubGroupOfPowerSettingsGuid, [Optional] in Guid PowerSettingGuid, uint DcValueIndex); /// /// Provides a for that is disposed using . /// public class SafeEffectivePowerModeNotificationHandle : 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 SafeEffectivePowerModeNotificationHandle(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeEffectivePowerModeNotificationHandle() : base() { } /// protected override bool InternalReleaseHandle() => PowerUnregisterFromEffectivePowerModeNotifications(handle).Succeeded; } /// Provides a for a registered power notification that is disposed using . public class SafeHPOWERNOTIFY : 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 SafeHPOWERNOTIFY(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeHPOWERNOTIFY() : base() { } /// protected override bool InternalReleaseHandle() => PowerSettingUnregisterNotification(handle).Succeeded; } } }