Solved

Hibernating...

Posted on 2002-03-17
12
684 Views
Last Modified: 2012-05-04
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
Comment
Question by:Stuart_Johnson
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 50 total points
ID: 6875516
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
 
LVL 9

Expert Comment

by:ginsonic
ID: 6875995
listening
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 6876011
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
 
LVL 12

Expert Comment

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

Cheers,

Raymond.
0
 

Expert Comment

by:DelFreak
ID: 6877497
Listening...
0
 
LVL 15

Expert Comment

by:simonet
ID: 6878068
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:rwilson032697
ID: 6878228
:-)
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 6881110
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
 

Expert Comment

by:DelFreak
ID: 6883170
Is this for all OS's?
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 6884320
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
 
LVL 15

Expert Comment

by:simonet
ID: 6885554
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
 

Expert Comment

by:DelFreak
ID: 6892436
Oh okay. Thanks! Good work.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now