2

I'm writing a Windows local service using C++. I need to know if wake timers are enabled on the system that it is running on. How can I do that from a local service?

The setting that is circled in this screenshot:

enter image description here

c00000fd
  • 20,994
  • 29
  • 177
  • 400

1 Answers1

2

As I pointed out in a previous post this setting is stored in the registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\(Current Power Scheme GUID)\(Sleep Category GUID)\(Enable AC or DC Wake Timers GUID) = 0 or 1

You may also retrieve this setting's current value from the command line:

powercfg.exe -q SCHEME_CURRENT SUB_SLEEP

I'm sure better approaches exist but this is all I've found so far. I'll update my post if I find a better technique.

EDIT

Use the following C# code as a guide. ACWakeTimerEnabled() returns 0 or 1 indicating enabled or disabled.

 [DllImport("powrprof.dll", SetLastError = true)]
    private static extern UInt32 PowerReadACValue(
        IntPtr RootPowerKey,
        ref Guid SchemeGuid,
        ref Guid SubGroupOfPowerSettingGuid,
        ref Guid PowerSettingGuid,
        IntPtr Type,
        IntPtr Buffer,
        ref UInt32 BufferSize);

    public static int ACWakeTimerEnabled()
    {
        Guid Root = new Guid("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"); // High Performance GUID
        Guid Sleep = new Guid("238c9fa8-0aad-41ed-83f4-97be242c8f20"); // Sleep Subcategory GUID
        Guid WakeTimers = new Guid("bd3b718a-0680-4d9d-8ab2-e1d2b4ac806d"); // AC Wake Timers GUID
        IntPtr ptrActiveGuid = IntPtr.Zero;
        uint buffSize = 0;
        uint res = PowerReadACValue(IntPtr.Zero, ref Root, ref Sleep, ref WakeTimers, IntPtr.Zero, IntPtr.Zero, ref buffSize);
        if (res == 0)
        {
            IntPtr ptrName = IntPtr.Zero;
            try
            {
                ptrName = Marshal.AllocHGlobal((int)buffSize);
                res = PowerReadACValue(IntPtr.Zero, ref Root, ref Sleep, ref WakeTimers, IntPtr.Zero, ptrName, ref buffSize);
                byte[] ba = new byte[buffSize];
                Marshal.Copy(ptrName, ba, 0, (int)buffSize);
                return BitConverter.ToInt32(ba, 0);
            }
            finally
            {
                if (ptrName != IntPtr.Zero) Marshal.FreeHGlobal(ptrName);
            }
        }
        throw new Win32Exception((int)res, "Error reading wake timer.");
    }
Community
  • 1
  • 1
Bosco
  • 935
  • 10
  • 18