开发者

Calling ReadProcessorPwrScheme() does not return correct values

开发者 https://www.devze.com 2023-01-08 11:57 出处:网络
I would like to ask a question regarding P/Invoke method. I am coding in C# and would like to access the Power Management API (powrprof.dll) method \"ReadProcessorPwrScheme\". Unfortunately, I am not

I would like to ask a question regarding P/Invoke method. I am coding in C# and would like to access the Power Management API (powrprof.dll) method "ReadProcessorPwrScheme". Unfortunately, I am not getting the correct values and I suspect that I may be a problem in marshaling. Any help is much appreciated. Some of the codes that I have:

struct PROCESSOR_POWER_POLICY_INFO
{
    public uint TimeCheck;                  // ULONG
    public uint 开发者_StackOverflowDemoteLimit;                // 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 is showing the correct value but mppp.ProcessorPolicyDc.DynamicThrottle is not showing the correct value. Anyone has any hint or idea whats wrong?

Additional info: This is run on Win XP


I have found the solution to my question. It was my misunderstanding about the struct definition which causes the wrong bytes to be read. The correct struct definition is as below.

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; 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号