我想问一个关于P / Invoke方法的问题。我正在使用C#进行编码,并希望访问Power Management 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上运行
答案 0 :(得分: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;
}