using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { public static partial class Kernel32 { /// The AC power status. public enum AC_STATUS : byte { /// Offline AC_OFFLINE = 0, /// Online AC_ONLINE = 1, /// On backup power. AC_LINE_BACKUP_POWER = 2, /// Unknown status AC_UNKNOWN = 255 } /// The battery charge status. [Flags] public enum BATTERY_STATUS : byte { /// High—the battery capacity is at more than 66 percent BATTERY_HIGH = 1, /// Low—the battery capacity is at less than 33 percent BATTERY_LOW = 2, /// Critical—the battery capacity is at less than five percent BATTERY_CRITICAL = 4, /// Charging BATTERY_CHARGING = 8, /// No system battery BATTERY_NONE = 128, /// Unknown status—unable to read the battery flag information BATTERY_UNKNOWN = 255 } /// The latency requirement for the time is takes to wake the computer. public enum LATENCY_TIME { /// Any latency (default). LT_DONT_CARE, /// PowerSystemSleeping1 state (equivalent to ACPI state S0 and APM state Working). LT_LOWEST_LATENCY, } /// /// Retrieves the current power state of the specified device. This function cannot be used to query the power state of a display device. /// /// A handle to an object on the device, such as a file or socket, or a handle to the device itself. /// /// A pointer to the variable that receives the power state. This value is TRUE if the device is in the working state. /// Otherwise, it is FALSE. /// /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. /// // BOOL WINAPI GetDevicePowerState( _In_ HANDLE hDevice, _Out_ BOOL *pfOn); https://msdn.microsoft.com/en-us/library/windows/desktop/aa372690(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa372690")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetDevicePowerState([In] IntPtr hDevice, [Out, MarshalAs(UnmanagedType.Bool)] out bool pfOn); /// /// Retrieves the power status of the system. The status indicates whether the system is running on AC or DC power, whether the /// battery is currently charging, how much battery life remains, and if battery saver is on or off. /// /// A pointer to a SYSTEM_POWER_STATUS structure that receives status 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. /// // BOOL WINAPI GetSystemPowerStatus( _Out_ LPSYSTEM_POWER_STATUS lpSystemPowerStatus); https://msdn.microsoft.com/en-us/library/windows/desktop/aa372693(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa372693")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS lpSystemPowerStatus); /// Determines the current state of the computer. /// /// If the system was restored to the working state automatically and the user is not active, the function returns TRUE. /// Otherwise, the function returns FALSE. /// // BOOL WINAPI IsSystemResumeAutomatic(void); https://msdn.microsoft.com/en-us/library/windows/desktop/aa372708(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa372708")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsSystemResumeAutomatic(); /// /// /// [ RequestWakeupLatency is available for use in the operating systems specified in the Requirements section. It may be /// altered or unavailable in subsequent versions.] /// /// /// Has no effect and returns STATUS_NOT_SUPPORTED. This function is provided only for compatibility with earlier versions of Windows. /// /// Windows Server 2008 and Windows Vista: Has no effect and always returns success. /// /// /// The latency requirement for the time is takes to wake the computer. This parameter can be one of the following values. /// /// /// /// Value /// Meaning /// /// /// LT_LOWEST_LATENCY1 /// PowerSystemSleeping1 state (equivalent to ACPI state S0 and APM state Working). /// /// /// LT_DONT_CARE0 /// Any latency (default). /// /// /// /// /// The return value is nonzero. // BOOL WINAPI RequestWakeupLatency( _In_ LATENCY_TIME latency); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373199(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "aa373199")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool RequestWakeupLatency(LATENCY_TIME latency); /// /// /// [ SetSystemPowerState is available for use in the operating systems specified in the Requirements section. It may be /// altered or unavailable in subsequent versions. Applications written for Windows Vista and later should use SetSuspendState instead.] /// /// /// Suspends the system by shutting power down. Depending on the ForceFlag parameter, the function either suspends operation /// immediately or requests permission from all applications and device drivers before doing so. /// /// /// /// If this parameter is TRUE, the system is suspended. If the parameter is FALSE, the system hibernates. /// /// This parameter has no effect. /// /// If power has been suspended and subsequently restored, the return value is nonzero. /// If the system was not suspended, the return value is zero. To get extended error information, call GetLastError. /// /// /// /// The calling process must have the SE_SHUTDOWN_NAME privilege. To enable the SE_SHUTDOWN_NAME privilege, use the /// AdjustTokenPrivileges function. For more information, see Changing Privileges in a Token. /// /// /// If any application or driver denies permission to suspend operation, the function broadcasts a PBT_APMQUERYSUSPENDFAILED event to /// each application and driver. If power is suspended, this function returns only after system operation is resumed and related /// WM_POWERBROADCAST messages have been broadcast to all applications and drivers. /// /// This function is similar to the SetSuspendState function. /// /// To compile an application that uses this function, define the _WIN32_WINNT macro as 0x0400 or later. For more information, see /// Using the Windows Headers. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setsystempowerstate BOOL SetSystemPowerState( BOOL fSuspend, // BOOL fForce ); [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("winbase.h", MSDNShortId = "58cf4e29-2a2e-499a-85ce-0034f4323cfe")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetSystemPowerState([MarshalAs(UnmanagedType.Bool)] bool fSuspend, [MarshalAs(UnmanagedType.Bool), Optional] bool fForce); /// Contains information about the power status of the system. // typedef struct _SYSTEM_POWER_STATUS { BYTE ACLineStatus; BYTE BatteryFlag; BYTE BatteryLifePercent; BYTE SystemStatusFlag; DWORD // BatteryLifeTime; DWORD BatteryFullLifeTime;} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS; https://msdn.microsoft.com/en-us/library/windows/desktop/aa373232(v=vs.85).aspx [PInvokeData("Winbase.h", MSDNShortId = "aa373232")] [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SYSTEM_POWER_STATUS { /// /// The AC power status. This member can be one of the following values. /// /// /// /// Value /// Meaning /// /// /// 0 /// Offline /// /// /// 1 /// Online /// /// /// 255 /// Unknown status /// /// /// /// public AC_STATUS ACLineStatus; /// /// The battery charge status. This member can contain one or more of the following flags. /// /// /// /// Value /// Meaning /// /// /// 1 /// High—the battery capacity is at more than 66 percent /// /// /// 2 /// Low—the battery capacity is at less than 33 percent /// /// /// 4 /// Critical—the battery capacity is at less than five percent /// /// /// 8 /// Charging /// /// /// 128 /// No system battery /// /// /// 255 /// Unknown status—unable to read the battery flag information /// /// /// /// The value is zero if the battery is not being charged and the battery capacity is between low and high. /// public BATTERY_STATUS BatteryFlag; /// /// The percentage of full battery charge remaining. This member can be a value in the range 0 to 100, or 255 if status is unknown. /// public byte BatteryLifePercent; /// /// /// The status of battery saver. To participate in energy conservation, avoid resource intensive tasks when battery saver is on. /// To be notified when this value changes, call the RegisterPowerSettingNotification function with the power setting /// GUID, GUID_POWER_SAVING_STATUS. /// /// /// /// /// Value /// Meaning /// /// /// 0 /// Battery saver is off. /// /// /// 1 /// Battery saver on. Save energy where possible. /// /// /// /// For general information about battery saver, see battery saver (in the hardware component guidelines). /// public byte SystemStatusFlag; /// /// The number of seconds of battery life remaining, or –1 if remaining seconds are unknown or if the device is connected to AC power. /// public uint BatteryLifeTime; /// /// The number of seconds of battery life when at full charge, or –1 if full battery lifetime is unknown or if the device is /// connected to AC power. /// public uint BatteryFullLifeTime; } } }