1

私はC#でCallNtPowerInformation関数を使用するのに本当に苦労しています。WindowsSystemExecutionStateを取得する必要があります。(ここにリストされている可能な値)。

適切なC#署名を見つけました:

    [DllImport("powrprof.dll", SetLastError = true)]

    private static extern UInt32 CallNtPowerInformation(
         Int32 InformationLevel,
         IntPtr lpInputBuffer,
         UInt32 nInputBufferSize,
         IntPtr lpOutputBuffer,
         UInt32 nOutputBufferSize
         );

次に、情報レベル16を使用して「SystemExecutionState」を読み取る必要があります。これが私がこれまでに持っているコードです:

IntPtr status = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(ulong)));
UInt32 returnValue = CallNtPowerInformation(
    16, 
    (IntPtr)null, 
    0, 
    status, (
    UInt32)Marshal.SizeOf(typeof(ulong)));
Marshal.FreeCoTaskMem(status);

Microsoftのドキュメントによると:

lpOutputBufferバッファーは、システム実行状態バッファーを含むULONG値を受け取ります。

IntPtrからULONG値を取得するにはどうすればよいですか?

4

2 に答える 2

2

out uintの代わりに使用してくださいIntPtr

[DllImport("powrprof.dll", SetLastError = true)]
private static extern UInt32 CallNtPowerInformation(
     Int32 InformationLevel,
     IntPtr lpInputBuffer,
     UInt32 nInputBufferSize,
     out uint lpOutputBuffer,
     UInt32 nOutputBufferSize
);


uint result;
CallNtPowerInformation(..., out result);
于 2012-04-22T13:41:50.590 に答える
1

値を取得するために呼び出しMarshal.ReadInt32(status)ます。

uint statusValue = (uint)Marshal.ReadInt32(status);

このクラスには、アンマネージメモリから読み取ることができるメソッドMarshalのファミリー全体があります。ReadXXX

于 2012-04-22T13:43:55.963 に答える