I am trying to get Last Wake Time using CallNtPowerInformation
and the value that is being copied into OutputBuffer is of type ULONGLONG
in C++ that I believe is equivalent to ulong
in C#.
When trying to use Marshal
static methods, I do not see a way to get an ulong
, only long
. Is there a way to get ulong
?
My code:
[DllImport("powrprof.dll", SetLastError = true)]
public static extern uint CallNtPowerInformation(
InformationLevel informationLevel,
IntPtr lpInputBuffer,
uint nInputBufferSize,
IntPtr lpOutpuBuffer,
nOutputBuffer);
public enum InformationLevel
{
AdministratorPowerPolicy = 9,
LastSleepTime = 15,
LastWakeTime = 14,
ProcessorInformation = 11,
ProcessorPowerPolicyAc = 18,
ProcessorPowerPolicyCurrent = 22,
ProcessorPowerPolicyDc = 19,
SystemBatteryState = 5,
SystemExecutionState = 16,
SystemPowerCapabilities = 4,
SystemPowerInformation = 12,
SystemPowerPolicyAc = 0,
SystemPowerPolicyCurrent = 8,
SystemPowerPolicyDc = 1,
SystemReserveHiberFile = 10,
VerifyProcessorPowerPolicyAc = 20,
VerifyProcessorPowerPolicyDc = 21,
VerifySystemPolicyAc = 2,
VerifySystemPolicyDc = 3
}
public static ulong GetLastWakeTime()
{
var lpOutputBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ulong)));
var ntstatusResult = CallNtPowerInformation(
InformationLevel.LastWakeTime,
IntPtr.Zero,
0,
lpOuputBuffer,
(uint)Marshal.SizeOf(typeof(ulong)));
var lastWakeTime = Marshal.ReadInt64(lpOutputBuffer); // This is where ulong should get read from unmanaged memory
Marshal.FreeHGlobal(lpOutputBuffer);
return lastWakeTime;
}