我可以获得最小(最大)处理器状态的值

时间:2012-11-16 10:57:15

标签: c# windows-7

在Win7下,打开Control Panel -> Power Options -> Advanced Settings->Processor power management

您可以看到最低处理器状态,最大处理器状态 我想通过C#得到处理器状态的值。例如5%,100%。 我在c#中使用命令“powercfg”,我不能得到“monitor-timeout-ac”的值等等。
我无法得到处理器的价值 状态。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您需要dllimport powrprof.dll,并使用ReadProcessorPwrScheme API,然后访问processorPolicyInfoAc.DemotePercent和ProcessorPolicyInfoAc.PromotePercent

请参阅下面的代码。没有经过测试但它会告诉你的方式(我使用这段代码来获取DynamicThrottle信息并且它可以工作)。


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

        struct PROCESSOR_POWER_POLICY
        {
            public uint Revision;
            public byte DynamicThrottle;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
            public byte[] Spare;
            public uint Reserved;
            public uint PolicyCount;
            [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 machinep = new MACHINE_PROCESSOR_POWER_POLICY();
            uint scheme = 0;

            if (ReadProcessorPwrScheme(scheme, out machinep))
            {

                //Then loop through machinep.ProcessorPolicyAc.Policy[]; array
                //Use:  PROCESSOR_POWER_POLICY_INFO processorPolicyInfoAc = mppp.ProcessorPolicyAc.Policy[i];
                //Use: processorPolicyInfoAc.DemotePercent;
                //Use: processorPolicyInfoAc.PromotePercent;

                //And don't forget to do the same for Dc (Dc is battery)
            }
        }

Edgar Rocha Carvalho

答案 1 :(得分:0)

使用

PowerReadACValueIndex()/PowerReadDCValueIndex()
  1. 第一个参数是NULL
  2. 第二个参数 - GUID用于所需的电源方案(GUID_MAX_POWER_SAVINGS / GUID_MIN_POWER_SAVINGS / GUID_TYPICAL_POWER_SAVINGS
  3. 第三个参数 - GUID_PROCESSOR_SETTINGS_SUBGROUP
  4. 第4个参数 - GUID_PROCESSOR_THROTTLE_MAXIMUMGUID_PROCESSOR_THROTTLE_MINIMUM
  5. 第五个参数是值接收器。
  6. 所有GUID说明都可以在 winnt.h

    中找到