Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

Hibernating...

Hi Folks,

Does anyone know how I can make a PC hibernate (I'm assuming that the system is capable of hibernating).

Cheers,

Stu
0
Stuart_Johnson
Asked:
Stuart_Johnson
  • 3
  • 3
  • 3
  • +2
1 Solution
 
simonetCommented:
there are 2 ways:

1) Win 9x

SetSuspendState
The SetSuspendState function suspends the system by shutting power down. Depending on the Hibernate parameter, the system either enters a suspend (sleep) state or hibernation (S4). If the ForceFlag parameter is TRUE, the system suspends operation immediately; if it is FALSE, the system requests permission from all applications and device drivers before doing so.

BOOL SetSuspendState (
  BOOL Hibernate,
  BOOL ForceCritical,
  BOOL DisableWakeEvent
);
Parameters
Hibernate
[in] Specifies the state of the system. If TRUE, the system hibernates. If FALSE, the system is suspended.
ForceCritical
[in] Forced suspension. If TRUE, the function broadcasts a PBT_APMSUSPEND event to each application and driver, then immediately suspends operation. If FALSE, the function broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation.
DisableWakeEvent
[in] If TRUE, the system disables all wake events. If FALSE, any system wake events remain enabled.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
An application may use SetSuspendState to transition the system from the working state to the standby (sleep), or optionally, hibernate (S4) state. This function is similar to the SetSystemPowerState function.


2) Win NT/2000/XP

SetSystemPowerState
The SetSystemPowerState function suspends the system by shutting power down. Depending on the ForceFlag parameter, the function either suspends operation immediately or requests permission from all applications and device drivers before doing so.

The calling process must have the SE_SHUTDOWN_NAME privilege. To enable the SE_SHUTDOWN_NAME privilege, use the AdjustTokenPrivileges function. For more information, see Privileges.

BOOL SetSystemPowerState(
  BOOL fSuspend,
  BOOL fForce
);
Parameters
fSuspend
Windows NT/2000/XP: [in] Specifies the state of the system. If TRUE, the system is suspended. If FALSE, the system hibernates.
Windows 95/98/Me: Ignored.

fForce
[in] Forced suspension. If TRUE, the function broadcasts a PBT_APMSUSPEND event to each application and driver, then immediately suspends operation. If FALSE, the function broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation.
Return Values
If power has been suspended and subsequently restored, the return value is nonzero.

If the system was not suspended, the return value is zero. To get extended error information, call GetLastError.

Remarks
If any application or driver denies permission to suspend operation, the function broadcasts a PBT_APMQUERYSUSPENDFAILED event to each application and driver. If power is suspended, this function returns only after system operation is resumes and related WM_POWERBROADCAST messages have been broadcast to all applications and drivers.


To use the later function, just adapt the code in http://www.bhnet.com.br/~simonet/tipstricks/exitwindowsnt.htm so that it calls SetSystemPowerState instead of ExitWindowsEx.

Yours,

Alex
0
 
ginsonicCommented:
listening
0
 
Stuart_JohnsonAuthor Commented:
Hi Alex,

Wow!  That's one impressive post :)  Thank you very much.  I'll have a look at this when I get home, and I'll let you know straight away if it worked or not (it looks good!).

Thanks once again,

Stu.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rwilson032697Commented:
Come on Alex - you should know that to make your computer hibernate you toss it in the freezer for a while ;-)

Cheers,

Raymond.
0
 
DelFreakCommented:
Listening...
0
 
simonetCommented:
Hey Ray,

that wouldn't leave too much space for the ice cream!

Besides, don't electrons move faster on lower temperatures?! If so, that would make them move faster, rather than hibernate!

0
 
rwilson032697Commented:
:-)
0
 
Stuart_JohnsonAuthor Commented:
Hi Ray,

You twit :)  And here I was thinking it was really hard to hibernate a system!  I knew I should have asked you first :)

Alex,

That code worked beautifully!  Thank you very much!

For those interested, here's the final solution:

var
  hToken : THANDLE;
  tkp, tkDumb : TTokenPrivileges;
  DumbInt : Cardinal;
begin
  FillChar(tkp, sizeof(tkp), 0);
  // Get a token for this process
  if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
          or TOKEN_QUERY, hToken)) then
     raise Exception.create('OpenProcessToken failed with code '
               + inttostr(GetLastError));

  // Get the LUID for the Shutdown privilege
  LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'),
                       tkp.Privileges[0].Luid);

  tkp.PrivilegeCount := 1; // one privilege to set
  tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

  // Get the shutdown provolege for this process
  AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

  // Cannot test the return value of AdjustTokenPrivileges
  if GetLastError <> ERROR_SUCCESS then
     Raise Exception.create('AdjustTokenPrivileges failed with code '
               + inttostr(GetLastError));

  // shut down the system and for all applications to close
  if not SetSystemPowerState(false, true) then
     Raise Exception.create('ExitWindowsEx failed with code '
               + inttostr(GetLastError));
end;

Many thank!


Stu
0
 
DelFreakCommented:
Is this for all OS's?
0
 
Stuart_JohnsonAuthor Commented:
Hi Del,

I'd imagine it will work with Me/2K/NT4/XP, but I'd be surprised if it works for 95/98.  98 is a possibility as it did support hibernate (although it only worked through use of third-party software).

The system MUST support Hibernation, and it must be enabled in the Power Options in the Control Panel.

Stu
0
 
simonetCommented:
Thanks a lot, Stu. Good to help ya!

For Win9x it's best to simply call SetSuspendState directly. Obviously, as Stu as already pointed out, it must be supported and explicitely enabled in the Control Panel.

Yours,

Alex
0
 
DelFreakCommented:
Oh okay. Thanks! Good work.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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