1

I am trying to get the sleep/hibernate timeout.

I searched over the Internet.

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

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).

Asaf Shazar
  • 1,065
  • 1
  • 11
  • 33
  • It may help if you referenced, or explained the "registry" and "cmd" solutions, and why they haven't worked. It may also help if you post what you currently have, and say why that doesn't work for you. – Paul Michaels Apr 09 '18 at 07:58
  • 1
    This isn't wrapped by the .net framework, [pinvoke is required](https://stackoverflow.com/questions/16023978/how-to-use-power-management-functions-powerenuimerate-to-get-power-settings). – Hans Passant Apr 09 '18 at 08:00
  • Possible duplicate of [How to use Power Management Functions (PowerEnuimerate) to get power settings](https://stackoverflow.com/questions/16023978/how-to-use-power-management-functions-powerenuimerate-to-get-power-settings) – Ian H. Apr 09 '18 at 08:26
  • the cmd command is powercfg. there no option to get the sleep timeout, that return all the data about sleep (long text) and no direct the timeout. about the registry: there no only 1 place I saw. I found a couple guides that mentions different area in the registry that can be found the timeout. none of them helped me. @lan H I already saw that, but as you see he not returned the Sleep/hibernate timeout... Only the display – Asaf Shazar Apr 09 '18 at 12:46

1 Answers1

2

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

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

  • GUID of Active Plan (You should first find it.)
  • GUID of Sleep Sub Group : 238c9fa8-0aad-41ed-83f4-97be242c8f20
  • GUID of Hibernate After Setting: 9d7815a6-7ee4-497e-8888-515a05f02364
  • GUID of Sleep After Setting: 29f6c1db-86da-48c5-9fdb-f2b67b1f44da

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

Using Windows API

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

To do so, first declare:

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

Then find the value this way:

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

Note

You can also find example of using Registry or using WMI in a blog post.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • [How to get value of advanced power settings](http://www.reza-aghaei.com/how-to-get-value-of-advanced-power-settings/) – Reza Aghaei Apr 10 '18 at 04:11
  • Your are my savior. Now i understand these guid And with that function i succeded to get the value. You gave me the hibrade guid but with powercfg i took the sleep guid.. Thank you very much.. that work perfect Btw the registry didnt work( i mean even if i change the number it not update there) – Asaf Shazar Apr 10 '18 at 12:05
  • By the way, thanks for checking the registry solution, I fixed it and updated the blog post and removed it from here, because it was making the answer unnecessarily too long. Future readers can find it in the blog post. – Reza Aghaei Apr 10 '18 at 16:08