windows - PowerWriteACValueIndex ændringer anvendes, men ikke brugt

Indlæg af Hanne Mølgaard Plasc

Problem



Vi bruger metoderne PowerWriteACValueIndex og PowerWriteDCValueIndex til at indstille adfærd for søvn- og afbrydelsesknappen til Gør intet . Når disse metoder bruges til at ændre værdien, afspejles dette i Kontrolpanel, ændres begge knapper i Gør intet . Men søvn- og afbryderknappen fungerer stadig som før. Efter manuelt at ændre adfærd i kontrolpanelet til en anden værdi og tilbage, anvendes ændringerne. Bruger jeg disse metoder forkert?


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

Bedste reference