Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hibernating...

Posted on 2002-03-17
12
Medium Priority
?
703 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 200 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
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.

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

705 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