• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 676
  • Last Modified:

URGENT - Detecting current power scheme Hibernation settings?

My application needs the computer to NEVER go in Hibernation mode so I need to be able to detect if the "current power scheme" hibernates after a few hours or "never" so that if it's not "never", I can warn the user.

None of the API calls I checked seem to address that question, such as:
GetCurrentPowerPolicies
and structures such as POWER_POLICY, etc... seem to have all the settings about power management except anything mentioning hibernation.

What can I do to check for that information? Either by API or by looking up the registry.

Please keep in mind that I'm using VB4 so any API call that uses an ENUM data type won't work for me, unless it can be adapted.

Thanks,

GB
0
GizmoBlaster
Asked:
GizmoBlaster
  • 6
  • 6
  • 2
2 Solutions
 
zzzzzoocCommented:
>>or by looking up the registry
I'm not sure about older versions of Windows, but for Win2k/XP, the location below should have a "Heuristics" entry with the value in binary. The 7th byte will either be 00 or 01 for whether it's disabled or not.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Power

If you don't know how to read values from the registry, http://www.pscode.com/vb/ should have plenty of examples submitted.
0
 
Erick37Commented:
Here is a way that your app can prevent the system fro going into suspend mode:

Prevent Windows Suspend Mode
http://www.freevbcode.com/ShowCode.Asp?ID=2596

0
 
zzzzzoocCommented:
I don't think he'll be able to subclass too easily in VB4.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Erick37Commented:
Oops!
0
 
Erick37Commented:
There's always:

Call Shell("powercfg /hibernate off", vbHide)
0
 
zzzzzoocCommented:
Just to annoy you.. he wants to detect, not disable. :P I wouldn't enjoy a program that disables features without me knowing!
0
 
GizmoBlasterAuthor Commented:
Ok Guys, we're getting somewhere, if nothing better comes up, I'll mix zzzzzooc and Erick's solutions by detecting whether its disabled or not and if not, by offering the user the possibility to let me disable it -- since of course I wouldnt want my software to mess up in such fundamental settings.

But one last thing I need to know is about zzzzzooc's solution:
what if hibernate is NOT disabled on the machine BUT the user has a setting of "never" in the place where he specifies the time to elapse before hibernation. It's a subtility but it kind of matters. If the user has "never hibernate" as a setting, I don't wanna bug him with disabling hibernation.

Unless you tell me that "never" is the same as disabling hibernation and that therefore, a setting of never would put the byte to "00".

How about the other values in the key you suggested? They all seem to be related to hiberation, such as:
HiberCopyTime
etc...

Is there someplace where I can have a documentation on these values?

Gizmo
0
 
zzzzzoocCommented:
HKEY_CURRENT_USER\Control Panel\PowerCfg\PowerPolicies\0\Policies - the 29th/30th (this count may be off by 1-2.. my eyes playing tricks!) bytes will be 00 00 if it's set to "never". This is for the "0" policy which my be different. To get the current policy, HKEY_CURRENT_USER\Control Panel\PowerCfg\CurrentPowerPolicy (that key's value will be the number of the policy).

This is from monitoring the registry in Win2k Pro. It may vary between versions of Windows so don't think it'll be compatible with every one. You may want to download software to monitor the registry yourself when changing values (to make it compatible for other Windows versions).

http://www.sysinternals.com/ntw2k/source/regmon.shtml
0
 
Erick37Commented:
Here is a bit of code which uses WMI to read the registry values provided by zzzzzooc:

This seems to work in XP Pro.

Const HKEY_CURRENT_USER = &H80000001

Dim strPath As String, regSZ As String
Dim objRegistry As Object
Dim regBinary As Variant
Dim i As Long, Ret As Long, lSecs As Long

'Get the WMI Registry Object
Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")

'Get the current policy
strPath = "Control Panel\PowerCfg"

Ret = objRegistry.GetStringValue(HKEY_CURRENT_USER, strPath, "CurrentPowerPolicy", regSZ)

If Ret = 0 Then 'success
    'Get the correct policy from the list
    strPath = "Control Panel\PowerCfg\PowerPolicies\" & regSZ
    'Fill the variant array with the binary values
    Ret = objRegistry.GetBinaryValue(HKEY_CURRENT_USER, strPath, "Policies", regBinary)
End If

If Ret = 0 Then 'success

    'regBinary should contain an array of bytes
    If (regBinary(28) = 0) And (regBinary(29) = 0) Then
        'Hibernation not enabled
        MsgBox "Hibernation Disabled"
    Else
        lSecs = (regBinary(29) * 256) + regBinary(28)
        MsgBox "Hibernation set to " & CStr(lSecs) & " secs."
    End If
   
End If

Set objRegistry = Nothing
0
 
zzzzzoocCommented:
Kudos, Erick. Will make the PAQ more worthwhile for others. Glad it works on XP or it might've been difficult for Gizmo.
0
 
Erick37Commented:
Another check required:

If running on batteries, the values in regBinary(33) and regBinary(32) will be used.  Need to detect when the computer is running on AC or Batteries...
0
 
GizmoBlasterAuthor Commented:
Hi Erick37,

Sorry for not getting back earlier. I tried your code with great enthusiasm... until I realized that it detects sleep mode, NOT hibernation, he he. Other than that, it works SUPER well, great!

If there a way to make it detect actual "hibernation"? I'd like to stress again that what I need to detect is hibernation, not sleep mode. If so, I'd consider my problem mostly solved except for a small detail: it works in XP but not in Win9x (tried it on 98). On win98, I get error 432: "File name or class name not found during OLE Automation operation" on this line of code:

Set objRegistry = GetObject("Winmgmts:root\default:StdRegProv")

Would there be a workaround for win98? I won't cry if it doesn't work on win98 but that would make the solution even better if it could.

Eagerly awaiting your solution(s). So far, you code is the most promising solution to that problem. Thanks a lot for your efforts.

Gizmo
0
 
Erick37Commented:
Well zzzzzooc's first comment tells you how to detect if Hibernation is enabled or not.  I suppose the values I gave code for are the "Suspend Mode" times.

I do not see a setting that allows me to set the time before hibernation, only an option to hibernate when the power button is pressed.

Although the WMI object should be compatible with Win9x, it may not be installed.  I'll see if I can get an API sample going that can read the Hibernate enable bit and post it here.
0
 
zzzzzoocCommented:
>>I do not see a setting that allows me to set the time before hibernation
I ran into that problem too. Enable hibernation, click apply, and the new time option should appear (did in Win2k Pro).

>>until I realized that it detects sleep mode
It detects the hibernation time for me in Win2k. It just reads the bytes from the registry that detail the # of seconds (kudos, Erick) to wait until hibernation. If they're 0, it's disabled (set to "Never"). If that worked for Erick on WinXP, then it'll work for you on Win2k/XP.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now