c++ - 如何知道 Windows 本地服务是否启用了唤醒计时器?

标签 c++ winapi service power-management

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

此屏幕截图中圈出的设置:

enter image description here

最佳答案

正如我在 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.");
    }

关于c++ - 如何知道 Windows 本地服务是否启用了唤醒计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12232829/

相关文章:

C++ 成员变量

c++ - 在旧式 main() 的参数中分解 WinMain 的 cmdLine

winapi - 临界区对象可以存储在 std::vector 中吗?

symfony - Symfony2 服务的外观模式

c++ - 我可以在 CUDA 代码的内核部分使用 C++ 头文件吗?

c++ - 3D 模型格式之间的转换如何工作? (例如 Collada *.dae 到 ACIS *.sat)

c++ - 如何检查 double 是否有小数部分?

c++ - 命名管道上的 WriteFile 有时会返回 ERROR_NO_DATA

c# - 作为 LocalSystem 运行的 .NET c# 服务更改注册表中的 ProxyServer,但浏览器在(重新)启动之前不会注意到

android - 使用 NotificationListenerService 时 getActiveNotifications 始终为 null