Some time back, I wrote a small utility for putting the system on standby mode, or hibernate it.  I created a C++ class named CPowerState, and encapsulated all the actions needed to do this inside that class.  My class is not dependent on anything else, and is not an MFC class, but you can use it in MFC applications as well as SDK applications.

Using CPowerState couldn't be any simpler.  You just construct an object of this class on the stack, and call Standby( ) or Hibernate( ) methods, and check the bool return value.  Here is how to do it:

CPowerState ps;  
bool bSuccess = ps.Standby();  
if (!bSuccess)  
	::MessageBox( NULL, TEXT("Standby operation failed."), TEXT("Error"), MB_OK | MB_ICONSTOP );

Or to hibernate the system:

CPowerState ps;  
bool bSuccess = ps.Hibernate();  
if (!bSuccess)  
	::MessageBox( NULL, TEXT("Hibernate operation failed."), TEXT("Error"), MB_OK | MB_ICONSTOP );

That's it.  Just one very important point to consider.  Both the Standby( ) and Hibernate( ) methods, if successful, will return only when the system is activated again (in case of standby) or when it's started again (in case of hibernation), so you should only check for the false return, because your operation may succeed, but you may receive no true result.  Consider this case.  You hibernate the system using this class, and the next time the user turns on the power of the system, he presses F8 at system boot screen, and chooses “Delete restoration data and proceed to system boot menu” for some reason.  This way, your process is terminated (as well as any other processes running when the system fell into hibernation mode) and the Hibernate( ) function will never return, so you will never get back the true result.

I've tested this code on Windows 2000 Professional and Windows XP Professional, and it works on both systems.  Download the sample application at the bottom of the article and use PowerState.h and PowerState.cpp files in your application.  Also, you can take a look at Power.cpp to see a sample of using this class.

 Download source code for this article

This article originally appeared on It's been republished here, and may contain modifications from the original article.