如何编组一系列结构?

时间:2014-04-22 19:02:41

标签: c# interop pinvoke

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        const int SystemPowerInformation = 11;
        const uint STATUS_SUCCESS = 0;

        [StructLayout(LayoutKind.Sequential)]
        struct PROCESSOR_POWER_INFORMATION
        {
            public uint  Number;
            public uint MaxMhz;
            public uint CurrentMhz;
            public uint MhzLimit;
            public uint MaxIdleState;
            public uint CurrentIdleState;
        }

        [DllImport("powrprof.dll")]
        static extern uint CallNtPowerInformation(
            int InformationLevel,
            IntPtr lpInputBuffer,
            int nInputBufferSize,
            [MarshalAs(UnmanagedType.LPArray)]
            out byte[]  lpOutputBuffer,
            int nOutputBufferSize
        );

        static void Main(string[] args)
        {


            byte[] buffer = new byte[4 * Marshal.SizeOf(typeof(PROCESSOR_POWER_INFORMATION))];
            uint retval = CallNtPowerInformation(
                SystemPowerInformation,
                IntPtr.Zero,
                0,
                out  buffer,
                4 * Marshal.SizeOf(typeof(PROCESSOR_POWER_INFORMATION))
            );

            if (retval == STATUS_SUCCESS)
                Console.WriteLine(buffer);
       }
    }
}

我想从CallNtPowerInformation中获取一些数据。我尝试创建一个结构并调用CallNtPowerInformation并编组来自它的数据,但这并不起作用。所以我试图看看我是否可以将数据转换为字节数组,但我得到以下内容:

  

对象引用未设置为对象的实例。

我相信我正在将内存分配给缓冲区。

我不确定为什么。任何指针都会有所帮助。

2 个答案:

答案 0 :(得分:2)

名为SystemPowerInformation的常量值为11的名称错误。它应该命名为ProcessorInformation

您应该像这样声明p / invoke:

[DllImport("powrprof.dll")]
static extern uint CallNtPowerInformation(
    int InformationLevel,
    IntPtr lpInputBuffer,
    int nInputBufferSize,
    [Out] PROCESSOR_POWER_INFORMATION[] processorPowerInformation,
    int nOutputBufferSize
);

为了调用该函数,您需要分配一个适当大小的PROCESSOR_POWER_INFORMATION结构数组。像这样:

PROCESSOR_POWER_INFORMATION[] powerInfo = 
    new PROCESSOR_POWER_INFORMATION[procCount];

CallNtPowerInformation的文档告诉您使用GetSystemInfo计算出您拥有的处理器数量。您可以使用Environment.ProcessorCount

然后你调用这样的函数:

uint retval = CallNtPowerInformation(
    ProcessorInformation,
    IntPtr.Zero,
    0,
    powerInfo,
    powerInfo.Length*Marshal.SizeOf(typeof(PROCESSOR_POWER_INFORMATION))
);

这是一个完整的计划:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        const int ProcessorInformation = 11;
        const uint STATUS_SUCCESS = 0;

        [StructLayout(LayoutKind.Sequential)]
        struct PROCESSOR_POWER_INFORMATION
        {
            public uint Number;
            public uint MaxMhz;
            public uint CurrentMhz;
            public uint MhzLimit;
            public uint MaxIdleState;
            public uint CurrentIdleState;
        }

        [DllImport("powrprof.dll")]
        static extern uint CallNtPowerInformation(
            int InformationLevel,
            IntPtr lpInputBuffer,
            int nInputBufferSize,
            [Out] PROCESSOR_POWER_INFORMATION[] lpOutputBuffer,
            int nOutputBufferSize
        );

        static void Main(string[] args)
        {
            int procCount = Environment.ProcessorCount;
            PROCESSOR_POWER_INFORMATION[] procInfo =
                new PROCESSOR_POWER_INFORMATION[procCount]; 
            uint retval = CallNtPowerInformation(
                ProcessorInformation,
                IntPtr.Zero,
                0,
                procInfo,
                procInfo.Length * Marshal.SizeOf(typeof(PROCESSOR_POWER_INFORMATION))
            );
            if (retval == STATUS_SUCCESS)
            {
                foreach (var item in procInfo)
                {
                    Console.WriteLine(item.CurrentMhz);
                }
            }
        }
    }
}

答案 1 :(得分:0)

将您的umnanaged调用的参数类型更改为IntPtr:

[DllImport("powrprof.dll")]
    static extern uint CallNtPowerInformation(
        int InformationLevel,
        IntPtr lpInputBuffer,
        int nInputBufferSize,
        IntPtr lpOutputBuffer,
        int nOutputBufferSize
    );

在调用它之前使用它:

GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();

然后调用它将IntPtr作为参数传递。

不要忘记在使用后释放!