I’m really struggling with using the CallNtPowerInformation function in C#. I need to get the Windows SystemExecutionState. (Possible values listed here).
I’ve found the appropriate C# signature:
[DllImport("powrprof.dll", SetLastError = true)]
private static extern UInt32 CallNtPowerInformation(
Int32 InformationLevel,
IntPtr lpInputBuffer,
UInt32 nInputBufferSize,
IntPtr lpOutputBuffer,
UInt32 nOutputBufferSize
);
Now I need to use information level 16 to read the “SystemExecutionState”. Here’s the code I have so far:
IntPtr status = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(ulong)));
UInt32 returnValue = CallNtPowerInformation(
16,
(IntPtr)null,
0,
status, (
UInt32)Marshal.SizeOf(typeof(ulong)));
Marshal.FreeCoTaskMem(status);
According to the Microsoft documentation:
The lpOutputBuffer buffer receives a ULONG value containing the system
execution state buffer.
How do I get the ULONG value from the IntPtr?
Call
Marshal.ReadInt32(status)to get the value.The
Marshalclass has a whole family ofReadXXXmethods that allow you to read from unmanaged memory.