从高级电源设置中获取 Sleep After/Hibernate After 的值 [英] Get value of Sleep After/Hibernate After from advanced power settings

查看:20
本文介绍了从高级电源设置中获取 Sleep After/Hibernate After 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得睡眠/休眠超时.

I am trying to get the sleep/hibernate timeout.

我在互联网上搜索过.

已经搜索并找到注册表和cmd命令解决方法,但都失败了

Already search and found the registry and The cmd command Solution, but both failed

cmd 命令返回了很多值,但不是我需要的.

The cmd command returns a lot of values but not what I needed.

我看到注册表有一个字节数组,但不是我需要的.

The registry got a byte array as I saw but not the one I needed.

我试过这个:主题但即使这样也没有帮助我.

I tried this: Topic but even that not helped me.

在我的公司,我们使用软件设置整个公司的睡眠/休眠状态在我的代码中,我想得到那个时间(不是硬编码).

In my company, we use software that set the Sleep/hibernated all over the company And in my code, I want to get that time (not hardcode it).

推荐答案

您可以使用 Windows API 获取电源高级设置值,包括 Hibernate AfterSleep After注册表powercfgWMI.

You can get power advanced setting values including Hibernate After and Sleep After using Windows API, Registry, powercfg, and WMI.

所有解决方案最重要的通用信息是一些 GUID 值:

The most important common information for all solutions is some GUID values:

  • Active Plan 的 GUID(您应该首先找到它.)
  • 睡眠 子组的 GUID:238c9fa8-0aad-41ed-83f4-97be242c8f20
  • Hibernate After 的 GUID 设置:9d7815a6-7ee4-497e-8888-515a05f02364
  • Sleep After 的 GUID 设置:29f6c1db-86da-48c5-9fdb-f2b67b1f44da

找到该值后,该值通常是十六进制并显示.

After you found the value, usually the value is hexadecimal and shows the seconds.

您可以使用PowerGetActiveScheme 函数获取活动电源计划,然后你可以使用 PowerReadACValue 以获得您需要的值.

You can use PowerGetActiveScheme function to get the active power plan, then you can use PowerReadACValue to get the value which you need.

为此,首先声明:

private static Guid GUID_SLEEP_SUBGROUP =
    new Guid("238c9fa8-0aad-41ed-83f4-97be242c8f20");
private static Guid GUID_HIBERNATEIDLE =
    new Guid("9d7815a6-7ee4-497e-8888-515a05f02364");

[DllImport("powrprof.dll")]
static extern uint PowerGetActiveScheme(
    IntPtr UserRootPowerKey,
    ref IntPtr ActivePolicyGuid);

[DllImport("powrprof.dll")]
static extern uint PowerReadACValue(
    IntPtr RootPowerKey,
    ref Guid SchemeGuid,
    ref Guid SubGroupOfPowerSettingGuid,
    ref Guid PowerSettingGuid,
    ref int Type,
    ref int Buffer,
    ref uint BufferSize);

然后这样求值:

var activePolicyGuidPTR = IntPtr.Zero;
PowerGetActiveScheme(IntPtr.Zero, ref activePolicyGuidPTR);

var activePolicyGuid = Marshal.PtrToStructure<Guid>(activePolicyGuidPTR);
var type = 0;
var value = 0;
var valueSize = 4u;
PowerReadACValue(IntPtr.Zero, ref activePolicyGuid,
    ref GUID_SLEEP_SUBGROUP, ref GUID_HIBERNATEIDLE,
    ref type, ref value, ref valueSize);

MessageBox.Show($"Hibernate after {value} seconds.");

注意

您还可以在 一篇博文.

这篇关于从高级电源设置中获取 Sleep After/Hibernate After 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆