PowerWriteACValueIndex更改已应用但未使用

时间:2018-05-08 12:00:22

标签: windows pinvoke

我们使用方法PowerWriteACValueIndexPowerWriteDCValueIndex将睡眠和关机按钮的行为设置为不执行任何操作。当这些方法用于更改值时,这将反映在“控制面板”中,两个按钮行为都将更改为不执行任何操作。然而,睡眠和电源按钮仍然像以前一样起作用。手动将控制面板中的行为更改为其他值并返回后,将应用更改。我错误地使用这些方法吗?

    public static void DisableButtons()
    {
        Guid schemeId = GetActivePowerScheme();
        DisablePowerAndSleepButtons(schemeId);
    }

    private static void DisablePowerAndSleepButtons(Guid schemeId)
    {
        // Lid close action
        PowerWriteValueIndex(schemeId, ref PowrProf.SUBGROUP_BUTTONS,
            ref PowrProf.OPTION_LIDACTION, PowrProf.ACTION_DO_NOTHING);

        // Power button action
        PowerWriteValueIndex(schemeId, ref PowrProf.SUBGROUP_BUTTONS,
            ref PowrProf.OPTION_PBUTTONACTION, PowrProf.ACTION_DO_NOTHING);

        // Sleep button action
        PowerWriteValueIndex(schemeId, ref PowrProf.SUBGROUP_BUTTONS,
            ref PowrProf.OPTION_SBUTTONACTION, PowrProf.ACTION_DO_NOTHING);
    }

    // Helper method that changes both the AC power setting and the DC (battery) power
    // setting.
    private static void PowerWriteValueIndex(Guid schemeGuid,
        ref Guid subGroupOfPowerSettingsGuid, ref Guid powerSettingGuid, uint valueIndex)
    {
        // When on AC power
        var hr = NativeMethods.PowerWriteACValueIndex(IntPtr.Zero, ref schemeGuid,
            ref subGroupOfPowerSettingsGuid, ref powerSettingGuid, valueIndex);
        if (hr != 0)
        {
            Console.WriteLine("Failed to write AC value");
        }

        // When on DC power (battery)
        hr = NativeMethods.PowerWriteDCValueIndex(IntPtr.Zero, ref schemeGuid,
            ref subGroupOfPowerSettingsGuid, ref powerSettingGuid, valueIndex);
        if (hr != 0)
        {
            Console.WriteLine("Failed to write DC value");
        }
    }

    private static Guid GetActivePowerScheme()
    {
        IntPtr ptrActiveGuid = IntPtr.Zero;

        try
        {
            var hr = NativeMethods.PowerGetActiveScheme(IntPtr.Zero, ref ptrActiveGuid);
            if (hr == 0)
            {
                var activeScheme = (Guid)Marshal.PtrToStructure(ptrActiveGuid, typeof(Guid));
                return activeScheme;
            }
            return Guid.Empty;
        }
        finally
        {
            Marshal.FreeHGlobal(ptrActiveGuid);
        }
    }

    private static class NativeMethods
    {
        [DllImport("powrprof.dll")]
        public static extern uint PowerGetActiveScheme(IntPtr UserRootPowerKey, ref IntPtr ActivePolicyGuid);

        [DllImport("powrprof.dll")]
        public static extern uint PowerWriteACValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, uint AcValueIndex);

        [DllImport("powrprof.dll")]
        public static extern uint PowerWriteDCValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid, ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, uint DcValueIndex);
    }

    /// <summary>
    /// Collections of guids to identify settings in PowrProf.dll
    /// Source: http://adirondackpc.com/publicfiles/nos3s4-full.txt
    /// </summary>
    private static class PowrProf
    {
        public static Guid SUBGROUP_NONE = new Guid("fea3413e-7e05-4911-9a71-700331f1c294");
        public static Guid SUBGROUP_SLEEP = new Guid("238c9fa8-0aad-41ed-83f4-97be242c8f20");
        public static Guid SUBGROUP_VIDEO = new Guid("7516b95f-f776-4464-8c53-06167f40cc99");
        public static Guid SUBGROUP_BUTTONS = new Guid("4f971e89-eebd-4455-a8de-9e59040e7347");

        // Top level options

        public static Guid OPTION_CONSOLELOCK = new Guid("0e796bdb-100d-47d6-a2d5-f7d2daa51f51");

        // Sleep options

        public static Guid OPTION_RTCWAKE = new Guid("bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d");
        // Turn the computer to sleep after x seconds, minmum value: 0, maximum value: 4294967295
        public static Guid OPTION_STANDBYIDLE = new Guid("29f6c1db-86da-48c5-9fdb-f2b67b1f44da");
        // Hybernate the computer after x seconds, minmum value: 0, maximum value: 4294967295
        public static Guid OPTION_HYBERNATEIDLE = new Guid("9d7815a6-7ee4-497e-8888-515a05f02364");

        // Display options

        // Display brightness, minimum value: 0, maximum value: 100
        public static Guid OPTION_BRIGHTNESS = new Guid("aded5e82-b909-4619-9949-f5d71dac0bcb");
        // Dim display after x seconds, minmum value: 0, maximum value: 4294967295
        public static Guid OPTION_VIDEODIM = new Guid("3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e");
        // Turn off display after x seconds, minmum value: 0, maximum value: 4294967295
        public static Guid OPTION_VIDEOIDLE = new Guid("3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e");

        // Button options

        // Lid close action
        public static Guid OPTION_LIDACTION = new Guid("5ca83367-6e45-459f-a27b-476b1d01c936");
        // Power button action
        public static Guid OPTION_PBUTTONACTION = new Guid("7648efa3-dd9c-4e3e-b566-50f929386280");
        // Sleep button action
        public static Guid OPTION_SBUTTONACTION = new Guid("96996bc0-ad50-47ec-923b-6f41874dd9eb");

        // Button actions

        public static uint ACTION_DO_NOTHING = 0;
        public static uint ACTION_SLEEP = 1;
        public static uint ACTION_HIBERNATE = 2;
        public static uint ACTION_SHUT_DOWN = 3;

        // Universal constants

        public static uint FALSE = 0;
        public static uint TRUE = 1;

        public static uint NEVER = 0;
    }

1 个答案:

答案 0 :(得分:0)

您需要在致电PowerSetActiveSchemePowerWriteDCValueIndex之后致电PowerWriteACValueIndex

它在备注部分的documentation上:

  

只有在调用PowerSetActiveScheme函数之后,对有效电源方案的设置的更改才会生效。