using System;
using System.Runtime.InteropServices;
using Vanara.InteropServices;
namespace Vanara.PInvoke
{
/// Functions, structures and constants from powrprof.dll.
public static partial class PowrProf
{
/// Indicates power level information.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/ne-wdm-power_information_level typedef enum {
// SystemPowerPolicyAc, SystemPowerPolicyDc, VerifySystemPolicyAc, VerifySystemPolicyDc, SystemPowerCapabilities, SystemBatteryState,
// SystemPowerStateHandler, ProcessorStateHandler, SystemPowerPolicyCurrent, AdministratorPowerPolicy, SystemReserveHiberFile,
// ProcessorInformation, SystemPowerInformation, ProcessorStateHandler2, LastWakeTime, LastSleepTime, SystemExecutionState,
// SystemPowerStateNotifyHandler, ProcessorPowerPolicyAc, ProcessorPowerPolicyDc, VerifyProcessorPowerPolicyAc,
// VerifyProcessorPowerPolicyDc, ProcessorPowerPolicyCurrent, SystemPowerStateLogging, SystemPowerLoggingEntry, SetPowerSettingValue,
// NotifyUserPowerSetting, PowerInformationLevelUnused0, SystemMonitorHiberBootPowerOff, SystemVideoState,
// TraceApplicationPowerMessage, TraceApplicationPowerMessageEnd, ProcessorPerfStates, ProcessorIdleStates, ProcessorCap,
// SystemWakeSource, SystemHiberFileInformation, TraceServicePowerMessage, ProcessorLoad, PowerShutdownNotification,
// MonitorCapabilities, SessionPowerInit, SessionDisplayState, PowerRequestCreate, PowerRequestAction, GetPowerRequestList,
// ProcessorInformationEx, NotifyUserModeLegacyPowerEvent, GroupPark, ProcessorIdleDomains, WakeTimerList, SystemHiberFileSize,
// ProcessorIdleStatesHv, ProcessorPerfStatesHv, ProcessorPerfCapHv, ProcessorSetIdle, LogicalProcessorIdling, UserPresence,
// PowerSettingNotificationName, GetPowerSettingValue, IdleResiliency, SessionRITState, SessionConnectNotification,
// SessionPowerCleanup, SessionLockState, SystemHiberbootState, PlatformInformation, PdcInvocation, MonitorInvocation,
// FirmwareTableInformationRegistered, SetShutdownSelectedTime, SuspendResumeInvocation, PlmPowerRequestCreate, ScreenOff,
// CsDeviceNotification, PlatformRole, LastResumePerformance, DisplayBurst, ExitLatencySamplingPercentage, RegisterSpmPowerSettings,
// PlatformIdleStates, ProcessorIdleVeto, PlatformIdleVeto, SystemBatteryStatePrecise, ThermalEvent, PowerRequestActionInternal,
// BatteryDeviceState, PowerInformationInternal, ThermalStandby, SystemHiberFileType, PhysicalPowerButtonPress,
// QueryPotentialDripsConstraint, EnergyTrackerCreate, EnergyTrackerQuery, UpdateBlackBoxRecorder, PowerInformationLevelMaximum } POWER_INFORMATION_LEVEL;
[PInvokeData("wdm.h", MSDNShortId = "DCAB0482-C0E3-4F75-B5A7-FB8DFFA89D6F")]
public enum POWER_INFORMATION_LEVEL
{
/// Indicates SystemPowerPolicyAc.
SystemPowerPolicyAc,
/// Indicates SystemPowerPolicyDc.
SystemPowerPolicyDc,
/// Indicates VerifySystemPolicyAc.
VerifySystemPolicyAc,
/// Indicates VerifySystemPolicyDc.
VerifySystemPolicyDc,
/// Indicates the power capabilities of the system.
SystemPowerCapabilities,
/// Indicates the system's battery state.
SystemBatteryState,
/// Indicates the system's power state handler.
SystemPowerStateHandler,
/// Indicates the processor state handler.
ProcessorStateHandler,
/// Indicates the system's current power policy.
SystemPowerPolicyCurrent,
/// Indicates the administrator's power policy.
AdministratorPowerPolicy,
/// Indicates the SystemReserveHiberFile.
SystemReserveHiberFile,
/// Indicates the processor information.
ProcessorInformation,
/// Indicates the system power information.
SystemPowerInformation,
/// Indicates the processor state handler.
ProcessorStateHandler2,
/// Indicates the last wake time.
LastWakeTime,
/// Indicates the last sleep time.
LastSleepTime,
/// Indicates the system execution state.
SystemExecutionState,
/// Indicates the system power state notify handler.
SystemPowerStateNotifyHandler,
/// Indicates ProcessorPowerPolicyAc.
ProcessorPowerPolicyAc,
/// Indicates ProcessorPowerPolicyDc.
ProcessorPowerPolicyDc,
/// Indicates VerifyProcessorPowerPolicyAc.
VerifyProcessorPowerPolicyAc,
/// Indicates VerifyProcessorPowerPolicyDc.
VerifyProcessorPowerPolicyDc,
/// Indicates the current processor power policy.
ProcessorPowerPolicyCurrent,
/// Indicates SystemPowerStateLogging.
SystemPowerStateLogging,
/// Indicates SystemPowerLoggingEntry.
SystemPowerLoggingEntry,
/// Indicates that the power setting value is set.
SetPowerSettingValue,
/// Indicates that the user should be notified of the power setting.
NotifyUserPowerSetting,
/// Indicates that the power information level is unused.
PowerInformationLevelUnused0,
/// Indicates that the system monitor boot power is off.
SystemMonitorHiberBootPowerOff,
/// Indicates the system video state.
SystemVideoState,
/// Indicates the trace application power message.
TraceApplicationPowerMessage,
/// Indicates the end of the trace application power message.
TraceApplicationPowerMessageEnd,
/// Indicates the processor performance states.
ProcessorPerfStates,
/// Indicates the processor idle states.
ProcessorIdleStates,
/// Indicates the processor cap.
ProcessorCap,
/// Indicates the system wake source.
SystemWakeSource,
/// Indicates the system's hibernation file information.
SystemHiberFileInformation,
/// Indicates the trace service power message.
TraceServicePowerMessage,
/// Indicates the processor load.
ProcessorLoad,
/// Indicates the power shutdown notification.
PowerShutdownNotification,
/// Indicates the monitor's capabilities.
MonitorCapabilities,
/// Indicates the session power has been initialized.
SessionPowerInit,
/// Indicates the session display state.
SessionDisplayState,
/// Indicates that a power request has been created.
PowerRequestCreate,
/// Indicates the action of the power request.
PowerRequestAction,
/// Indicates that the power request list should be queued.
GetPowerRequestList,
/// Indicates ProcessorInformationEx.
ProcessorInformationEx,
/// Indicates that a notification should be created for the user mode legacy power event.
NotifyUserModeLegacyPowerEvent,
/// Indicates the group park.
GroupPark,
/// Indicates the processor's idle domains.
ProcessorIdleDomains,
/// Indicates the wake timer list.
WakeTimerList,
/// Indicates the system's hibernation file size.
SystemHiberFileSize,
/// Indicates the processor's idle states.
ProcessorIdleStatesHv,
/// Indicates the processor's performance states.
ProcessorPerfStatesHv,
/// Indicates the processor's performance capabilities.
ProcessorPerfCapHv,
/// Indicates that the processor has been set to idle.
ProcessorSetIdle,
/// Indicates that the processor is idling.
LogicalProcessorIdling,
/// Indicates the user presence.
UserPresence,
/// Indicates the power setting notification name.
PowerSettingNotificationName,
/// Indicates that the power setting value should be queued.
GetPowerSettingValue,
/// Indicates the idle resiliency.
IdleResiliency,
/// Indicates the session's RIT state.
SessionRITState,
/// Indicates the session's connect notification.
SessionConnectNotification,
/// Indicates the session's power cleanup.
SessionPowerCleanup,
/// Indicates the session's lock state.
SessionLockState,
/// Indicates the system's hibernation boot state.
SystemHiberbootState,
/// Indicates the platform information.
PlatformInformation,
/// Indicates the pdc invocation.
PdcInvocation,
/// Indicates the monitor invocation.
MonitorInvocation,
/// Indicates the registered firmware table information.
FirmwareTableInformationRegistered,
/// Indicates that the shutdown time should be set.
SetShutdownSelectedTime,
/// Indicates SuspendResumeInvocation.
SuspendResumeInvocation,
/// Indicates that the power request has been created.
PlmPowerRequestCreate,
/// Indicates that the screen is off.
ScreenOff,
/// Indicates the device notification.
CsDeviceNotification,
/// Indicates the platform role.
PlatformRole,
/// Indicates the last time performance was resumed.
LastResumePerformance,
/// Indicates display burst.
DisplayBurst,
/// Indicates the latency sampling percentage.
ExitLatencySamplingPercentage,
/// Indicates that the power settings are registered.
RegisterSpmPowerSettings,
/// Indicates the platform's idle states.
PlatformIdleStates,
/// Indicates the processor's idle veto.
ProcessorIdleVeto,
/// Indicates the platform's idle veto.
PlatformIdleVeto,
/// Indicates the system's battery state.
SystemBatteryStatePrecise,
/// Indicates the thermal event.
ThermalEvent,
/// Indicates the internal power request action.
PowerRequestActionInternal,
/// Indicates the battery's device state.
BatteryDeviceState,
/// Indicates the internal power information.
PowerInformationInternal,
/// Indicates thermal standby.
ThermalStandby,
/// Indicates the system's hibernation file type.
SystemHiberFileType,
/// Indicates a physical power button press.
PhysicalPowerButtonPress,
/// Indicates the potential drips constraint.
QueryPotentialDripsConstraint,
/// Indicates that the energy tracker is created.
EnergyTrackerCreate,
/// Indicates that the energy tracker is queried.
EnergyTrackerQuery,
/// Indicates that the black box recorder is updated.
UpdateBlackBoxRecorder,
/// Indicates the maximum power level.
PowerInformationLevelMaximum,
}
///
/// The version of the POWER_PLATFORM_ROLE enumeration for the platform used by .
///
[PInvokeData("powerbase.h", MSDNShortId = "64b597d3-ca7a-4ff7-8527-72c3625147cd")]
public enum PowerPlatformRoleVersion
{
/// The version of the POWER_PLATFORM_ROLE enumeration for Windows 7, Windows Server 2008 R2, Windows Vista or Windows Server 2008.
/// Calling PowerDeterminePlatformRoleEx with this value returns the same result as calling PowerDeterminePlatformRole on Windows 7, Windows Server 2008 R2, Windows Vista or Windows Server 2008.
POWER_PLATFORM_ROLE_V1 = 1,
/// The version of the POWER_PLATFORM_ROLE enumeration for Windows 8 and Windows Server 2012.
POWER_PLATFORM_ROLE_V2 = 2
}
/// Flags for .
[PInvokeData("powerbase.h", MSDNShortId = "3b39ec3a-417c-4ce4-a581-ed967f1baec9")]
public enum RegisterSuspendResumeNotificationFlags
{
/// The Recipient parameter is a pointer to a callback function to call when the power setting changes.
DEVICE_NOTIFY_CALLBACK = 2
}
/// Sets or retrieves power information.
///
///
/// The information level requested. This value indicates the specific power information to be set or retrieved. This parameter must
/// be one of the following POWER_INFORMATION_LEVEL enumeration type values.
///
///
///
/// Value
/// Meaning
///
/// -
/// AdministratorPowerPolicy 9
/// This information level is not supported.
///
/// -
/// LastSleepTime 15
///
/// The lpInBuffer parameter must be NULL; otherwise, the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer
/// receives a ULONGLONG that specifies the interrupt-time count, in 100-nanosecond units, at the last system sleep time.
///
///
/// -
/// LastWakeTime 14
///
/// The lpInBuffer parameter must be NULL; otherwise, the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer
/// receives a ULONGLONG that specifies the interrupt-time count, in 100-nanosecond units, at the last system wake time.
///
///
/// -
/// ProcessorInformation 11
///
/// The lpInBuffer parameter must be NULL; otherwise the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer receives
/// one PROCESSOR_POWER_INFORMATION structure for each processor that is installed on the system. Use the GetSystemInfo function to
/// retrieve the number of processors.
///
///
/// -
/// ProcessorPowerPolicyAc 18
/// This information level is not supported.
///
/// -
/// ProcessorPowerPolicyCurrent 22
/// This information level is not supported.
///
/// -
/// ProcessorPowerPolicyDc 19
/// This information level is not supported.
///
/// -
/// SystemBatteryState 5
///
/// The lpInBuffer parameter must be NULL; otherwise, the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer
/// receives a SYSTEM_BATTERY_STATE structure containing information about the current system battery.
///
///
/// -
/// SystemExecutionState 16
///
/// The lpInBuffer parameter must be NULL; otherwise the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer receives
/// a ULONG value containing the system execution state buffer. This value may contain any combination of the following values:
/// ES_SYSTEM_REQUIRED, ES_DISPLAY_REQUIRED, or ES_USER_PRESENT. For more information, see the SetThreadExecutionState function.
///
///
/// -
/// SystemPowerCapabilities 4
///
/// The lpInBuffer parameter must be NULL, otherwise, the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer
/// receives a SYSTEM_POWER_CAPABILITIES structure containing the current system power capabilities. This information represents the
/// currently supported power capabilities. It may change as drivers are installed in the system. For example, installation of legacy
/// device drivers that do not support power management disables all system sleep states.
///
///
/// -
/// SystemPowerInformation 12
///
/// The lpInBuffer parameter must be NULL; otherwise, the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer
/// receives a SYSTEM_POWER_INFORMATION structure. Applications can use this level to retrieve information about the idleness of the system.
///
///
/// -
/// SystemPowerPolicyAc 0
///
/// If lpInBuffer is not NULL, the function applies the SYSTEM_POWER_POLICY values passed in lpInBuffer to the current system power
/// policy used while the system is running on AC (utility) power. The lpOutputBuffer buffer receives a SYSTEM_POWER_POLICY structure
/// containing the current system power policy used while the system is running on AC (utility) power.
///
///
/// -
/// SystemPowerPolicyCurrent 8
///
/// The lpInBuffer parameter must be NULL; otherwise, the function returns ERROR_INVALID_PARAMETER. The lpOutputBuffer buffer
/// receives a SYSTEM_POWER_POLICY structure containing the current system power policy used while the system is running on AC
/// (utility) power.
///
///
/// -
/// SystemPowerPolicyDc 1
///
/// If lpInBuffer is not NULL, the function applies the SYSTEM_POWER_POLICY values passed in lpInBuffer to the current system power
/// policy used while the system is running on battery power. The lpOutputBuffer buffer receives a SYSTEM_POWER_POLICY structure
/// containing the current system power policy used while the system is running on battery power.
///
///
/// -
/// SystemReserveHiberFile 10
///
/// If lpInBuffer is not NULL and the current user has sufficient privileges, the function commits or decommits the storage required
/// to hold the hibernation image on the boot volume. The lpInBuffer parameter must point to a BOOLEAN value indicating the desired
/// request. If the value is TRUE, the hibernation file is reserved; if the value is FALSE, the hibernation file is removed.
///
///
/// -
/// VerifyProcessorPowerPolicyAc 20
/// This information level is not supported.
///
/// -
/// VerifyProcessorPowerPolicyDc 21
/// This information level is not supported.
///
/// -
/// VerifySystemPolicyAc 2
/// This information level is not supported.
///
/// -
/// VerifySystemPolicyDc 3
/// This information level is not supported.
///
///
///
///
/// A pointer to an optional input buffer. The data type of this buffer depends on the information level requested in the
/// InformationLevel parameter.
///
/// The size of the input buffer, in bytes.
///
/// A pointer to an optional output buffer. The data type of this buffer depends on the information level requested in the
/// InformationLevel parameter. If the buffer is too small to contain the information, the function returns STATUS_BUFFER_TOO_SMALL.
///
///
/// The size of the output buffer, in bytes. Depending on the information level requested, this may be a variably sized buffer.
///
///
/// If the function succeeds, the return value is STATUS_SUCCESS.
/// If the function fails, the return value can be one the following status codes.
///
///
/// Status
/// Meaning
///
/// -
/// STATUS_BUFFER_TOO_SMALL
/// The output buffer is of insufficient size to contain the data to be returned.
///
/// -
/// STATUS_ACCESS_DENIED
/// The caller had insufficient access rights to perform the requested action.
///
///
///
///
///
/// Changes made to the current system power policy using CallNtPowerInformation are immediate, but they are not persistent;
/// that is, the changes are not stored as part of a power scheme. Any changes to system power policy made with
/// CallNtPowerInformation may be overwritten by changes to a policy scheme made by the user in the Power Options control
/// panel program, or by subsequent calls to WritePwrScheme, SetActivePwrScheme, or other power scheme functions.
///
/// For more information on using PowrProf.h, see Power Schemes.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/powerbase/nf-powerbase-callntpowerinformation NTSTATUS
// CallNtPowerInformation( POWER_INFORMATION_LEVEL InformationLevel, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer,
// ULONG OutputBufferLength );
[DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)]
[PInvokeData("powerbase.h", MSDNShortId = "adc0052d-e2dd-4c55-996c-6af8f5987d79")]
public static extern NTStatus CallNtPowerInformation(POWER_INFORMATION_LEVEL InformationLevel, IntPtr InputBuffer, uint InputBufferLength, IntPtr OutputBuffer, uint OutputBufferLength);
/// Retrieves information about the system power capabilities.
/// A pointer to a SYSTEM_POWER_CAPABILITIES structure that receives the information.
///
/// 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.
///
///
///
/// This function retrieves detailed information about the current system power management hardware resources and capabilities. This
/// includes information about the presence of hardware features such as power buttons, lid switches, and batteries. Other details
/// returned include information about current power management capabilities and configurations that can change dynamically, such as
/// the minimum sleep state currently supported, which may change as new drivers are introduced into the system, or the presence of
/// the system hibernation file.
///
/// This information is also available through the CallNtPowerInformation function, using the SystemPowerCapabilities level.
/// For more information on using PowrProf.h, see Power Schemes.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/powerbase/nf-powerbase-getpwrcapabilities BOOLEAN GetPwrCapabilities(
// PSYSTEM_POWER_CAPABILITIES lpspc );
[DllImport(Lib.PowrProf, SetLastError = true, ExactSpelling = true)]
[PInvokeData("powerbase.h", MSDNShortId = "bb5cec5f-8d45-4158-824a-023f92af9b69")]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool GetPwrCapabilities(out SYSTEM_POWER_CAPABILITIES lpspc);
/// Determines the computer role for the specified platform.
///
/// The version of the POWER_PLATFORM_ROLE enumeration for the platform. This parameter can be one of the following values.
///
///
/// Value
/// Meaning
///
/// -
/// POWER_PLATFORM_ROLE_VERSION
/// The version of the POWER_PLATFORM_ROLE enumeration for the current build target.
///
/// -
/// POWER_PLATFORM_ROLE_V1
///
/// The version of the POWER_PLATFORM_ROLE enumeration for Windows 7, Windows Server 2008 R2, Windows Vista or Windows Server 2008.
/// Calling PowerDeterminePlatformRoleEx with this value returns the same result as calling PowerDeterminePlatformRole on Windows 7,
/// Windows Server 2008 R2, Windows Vista or Windows Server 2008.
///
///
/// -
/// POWER_PLATFORM_ROLE_V2
/// The version of the POWER_PLATFORM_ROLE enumeration for Windows 8 and Windows Server 2012.
///
///
///
/// The return value is one of the values from the specified version of the POWER_PLATFORM_ROLE enumeration.
///
///
/// This function reads the ACPI Fixed ACPI Description Table (FADT) to determine the OEM preferred computer role. If that
/// information is not available, the function looks for a battery. If at least one battery is available, the function returns
/// PlatformRoleMobile. If no batteries are available, the function returns PlatformRoleDesktop.
///
///
/// If the OEM preferred computer role is not supported on the platform specified by the caller, the function returns the closest
/// supported value. For example, calling the PowerDeterminePlatformRoleEx function with a Version of
/// POWER_PLATFORM_ROLE_V1 on a tablet device returns PlatformRoleMobile.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/powerbase/nf-powerbase-powerdetermineplatformroleex POWER_PLATFORM_ROLE
// PowerDeterminePlatformRoleEx( ULONG Version );
[DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)]
[PInvokeData("powerbase.h", MSDNShortId = "64b597d3-ca7a-4ff7-8527-72c3625147cd")]
public static extern POWER_PLATFORM_ROLE PowerDeterminePlatformRoleEx(PowerPlatformRoleVersion Version);
/// Registers to receive notification when the system is suspended or resumed.
/// This parameter must be DEVICE_NOTIFY_CALLBACK.
///
/// This parameter is 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.
///
/// 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.
// https://docs.microsoft.com/en-us/windows/desktop/api/powerbase/nf-powerbase-powerregistersuspendresumenotification DWORD
// PowerRegisterSuspendResumeNotification( DWORD Flags, HANDLE Recipient, PHPOWERNOTIFY RegistrationHandle );
[DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)]
[PInvokeData("powerbase.h", MSDNShortId = "3b39ec3a-417c-4ce4-a581-ed967f1baec9")]
public static extern Win32Error PowerRegisterSuspendResumeNotification(RegisterSuspendResumeNotificationFlags Flags, in DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS Recipient, out SafeHPOWERNOTIFY RegistrationHandle);
/// Cancels a registration to receive notification when the system is suspended or resumed.
///
/// A handle to a registration obtained by calling the PowerRegisterSuspendResumeNotification 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/powerbase/nf-powerbase-powerunregistersuspendresumenotification DWORD
// PowerUnregisterSuspendResumeNotification( HPOWERNOTIFY RegistrationHandle );
[DllImport(Lib.PowrProf, SetLastError = false, ExactSpelling = true)]
[PInvokeData("powerbase.h", MSDNShortId = "5680e6bd-1694-4d5f-94ea-41b24149c741")]
public static extern Win32Error PowerUnregisterSuspendResumeNotification(HANDLE RegistrationHandle);
/// Provides a for a registered power suspend/resume notification that is disposed using .
public class SafeHPOWERSRNOTIFY : 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 SafeHPOWERSRNOTIFY(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }
/// Initializes a new instance of the class.
private SafeHPOWERSRNOTIFY() : base() { }
///
protected override bool InternalReleaseHandle() => PowerUnregisterSuspendResumeNotification(handle).Succeeded;
}
}
}