1

我想问一个关于 P/Invoke 方法的问题。我正在使用 C# 进行编码,并希望访问电源管理 API (powrprof.dll) 方法“ReadProcessorPwrScheme”。不幸的是,我没有得到正确的值,我怀疑我可能是编组的问题。任何帮助深表感谢。我拥有的一些代码:

struct PROCESSOR_POWER_POLICY_INFO
{
    public uint TimeCheck;                  // ULONG
    public uint DemoteLimit;                // ULONG
    public uint PromoteLimit;               // ULONG
    public byte DemotePercent;
    public byte PromotePercent;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public byte[] Spare;
    public uint AllowDemotion;
    public uint AllowPromotion;
    public uint Reserved;
}

struct PROCESSOR_POWER_POLICY
{
    public uint Revision;                   // DWORD
    public byte DynamicThrottle;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
    public byte[] Spare;
    public uint DisableCStates;             // DWORD
    public uint Reserved;                   // DWORD
    public uint PolicyCount;                // DWORD
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
    public PROCESSOR_POWER_POLICY_INFO[] Policy;
}

struct MACHINE_PROCESSOR_POWER_POLICY
{
    public uint Revision;                   // ULONG
    public PROCESSOR_POWER_POLICY ProcessorPolicyAc;
    public PROCESSOR_POWER_POLICY ProcessorPolicyDc;
}


[DllImport("powrprof.dll", SetLastError = true)]
static extern bool ReadProcessorPwrScheme(uint uiID, out MACHINE_PROCESSOR_POWER_POLICY pMachineProcessorPowerPolicy);
public void ReadProcessorPowerScheme()
{
    MACHINE_PROCESSOR_POWER_POLICY mppp = new MACHINE_PROCESSOR_POWER_POLICY();
    uint schemeID0 = 0;

    if (ReadProcessorPwrScheme(schemeID0, out mppp))
    {
        Console.WriteLine(mppp.ProcessorPolicyAc.DynamicThrottle);
        Console.WriteLine(mppp.ProcessorPolicyDc.DynamicThrottle);
    }
}

mppp.ProcessorPolicyAc.DynamicThrottle显示正确的值,但mppp.ProcessorPolicyDc.DynamicThrottle未显示正确的值。有人有任何提示或想法有什么问题吗?

附加信息:这是在 Win XP 上运行的

4

1 回答 1

1

我找到了我的问题的解决方案。这是我对导致读取错误字节的结构定义的误解。正确的结构定义如下。

struct PROCESSOR_POWER_POLICY_INFO 
{ 
    public uint TimeCheck;                  // ULONG 
    public uint DemoteLimit;                // ULONG 
    public uint PromoteLimit;               // ULONG 
    public byte DemotePercent; 
    public byte PromotePercent; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] 
    public byte[] Spare; 
    //public uint AllowDemotion; 
    //public uint AllowPromotion; 
    //public uint Reserved; 
    public uint AllowBits;
} 

struct PROCESSOR_POWER_POLICY 
{ 
    public uint Revision;                   // DWORD 
    public byte DynamicThrottle; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] 
    public byte[] Spare; 
    //public uint DisableCStates;             // DWORD 
    public uint Reserved;                   // DWORD 
    public uint PolicyCount;                // DWORD 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] 
    public PROCESSOR_POWER_POLICY_INFO[] Policy; 
} 
于 2010-07-23T01:28:50.887 回答