private void SetPowerInformationBooleanValue(PowerInformationLevel level, bool value) { var inputBuffer = AllocateCoTaskBuffer <bool>(out var inputBufferSize); var byteValue = value ? (byte)1 : (byte)0; Marshal.WriteByte(inputBuffer, ofs: 0, val: byteValue); try { var returnCode = PowrprofDllImport .CallNtPowerInformation( level, inputBuffer, (ulong)inputBufferSize, IntPtr.Zero, 0); EnsureWin32CallSucceeded(returnCode); } finally { Marshal.FreeCoTaskMem(inputBuffer); } }
public void PutMachineSleep() { PowrprofDllImport.SetSuspendState( bHibernate: false, bForce: false, bWakeupEventsDisabled: false); }
private T GetPowerInformationStructure <T>(PowerInformationLevel level) where T : struct { var outputBuffer = AllocateCoTaskBuffer <T>(out var outputBufferSize); try { var returnCode = PowrprofDllImport.CallNtPowerInformation( level, (IntPtr)null, 0, outputBuffer, (ulong)outputBufferSize); EnsureWin32CallSucceeded(returnCode); return(Marshal.PtrToStructure <T>(outputBuffer)); } finally { Marshal.FreeCoTaskMem(outputBuffer); } }
private long GetPowerInformationInt64Value(PowerInformationLevel level) { var outputBuffer = AllocateCoTaskBuffer <long>(out var outputBufferSize); try { // Result returned in count of 100 nanoseconds var returnCode = PowrprofDllImport.CallNtPowerInformation( level, (IntPtr)null, 0, outputBuffer, (ulong)outputBufferSize); EnsureWin32CallSucceeded(returnCode); return(Marshal.ReadInt64(outputBuffer, 0)); } finally { Marshal.FreeCoTaskMem(outputBuffer); } }