【问题标题】:How to know if wake timers are enabled from a Windows local service?如何知道是否从 Windows 本地服务启用了唤醒计时器?
【发布时间】:2012-09-02 01:42:23
【问题描述】:

我正在使用 C++ 编写 Windows 本地服务。我需要知道是否在运行它的系统上启用了唤醒计时器。如何通过本地服务做到这一点?

此截图中圈出的设置:

【问题讨论】:

    标签: c++ winapi service power-management


    【解决方案1】:

    正如我在 previous 帖子中指出的,此设置存储在注册表中:

    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

    您也可以从命令行检索此设置的当前值:

    powercfg.exe -q SCHEME_CURRENT SUB_SLEEP
    

    我确信存在更好的方法,但这就是我迄今为止发现的全部。如果我找到更好的技术,我会更新我的帖子。

    编辑

    使用以下 C# 代码作为指导。 ACWakeTimerEnabled() 返回 0 或 1 表示启用或禁用。

     [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.");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 2017-03-24
      • 1970-01-01
      相关资源
      最近更新 更多