# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Automatically Wake Up The Computer From Hibernation [VB6] [Win2000 or later]

## iPrank

*The Story:*

I was looking for a alarm clock software that will wake me up at 6:00am so that I can start spaming here at VBF as soon as possible.  :Big Grin:   But I don't want to keep my PC turned on all night.
Many modern motherboards supports automatic boot-up alarm. But my motherboard doesn't. 'Wake on LAN' may be an option, but I don't have a LAN. 'Wake on Modem Ring' may be another option, but I gave my dial-up modem to my friend.
So, here is my workaround: Keep the PC in Hibernate (Suspend) mode and wake it up at 6:00am.

*The Program:*

This program simply puts the PC in hibernation state and wakes it up at a pre-defined time. Optionally it can sound an alarm when it wakes up. (You must keep the power on.  :Stick Out Tongue: )

*Before We Begin:*

1.) This works only in Win2000 or later
2.) Make sure your motherboard supports ACPI S4 state.
2.) Make sure hibernation is enabled in Windows. Go to, ControlPanel>PowerOptions and in the 'Hibernation' tab, check the 'Enable hibernation' checkbox.

*Enter the Coder:*

Open the project in VB and run it. Press the 'Test' button.
If everything is ok, your system will go to Hibernation. Don't turn off power. Your system will wake up automatically after 2 minutes.
If your system doesn't wake up within 3-4 minutes, press the power button of your computer.

*Note:* Hibernation is very much OS/Motherboard/DeviceDriver dependent. Make sure Windows can hibernate your computer perfectly. Save all work before testing.

*How it works ?*

Basically I have done nothing but merging the following codes:
How To Use SetWaitableTimer With Visual Basic
SetSystemPowerState

SetSystemPowerState initiates a hibernation and just then, CreateWaitableTimer and SetWaitableTimer creates and sets a waitable timer with passing True to the fResume parameter. When the timer event fires, Windows automatically wakes-up.  :Smilie: 



> fResume 
> [in] Specifies whether to restore a system in suspended power conservation mode when the timer state is set to signaled. If fResume is TRUE on a platform that does not support a restore, the call will succeed, but GetLastError returns ERROR_NOT_SUPPORTED


In addition, I have subclassed the form. When Windows or any other program tries to suspend the system, I'm discarding the operation and initiating my own Hibernate&WakeUp routine. (Will be useful if we keep it running in background or in systray. In v2.0 may be.  :Big Grin: )

*End Sub:*
I have tested this code on Windows 2003 Server and it is working ok.
Special thanks to fahad k for testing this code.  :Thumb: 

This is my first codebank submission. Please feel free to comment or slap me.  :Big Grin:

----------


## I_Love_My_Vans

Hell yea!!! well im guessing that, but i have been looking for smmurt like this. well played

ILMV

----------


## iPrank

Thanks dude.  :Big Grin: 

I have tested this in my friends' PC, WinXP-SP2, and it works there too.

----------


## zahadumy

VB 6? Can you convert it to .NET?  :Big Grin:

----------


## thegreatone

> VB 6? Can you convert it to .NET?


 No, but you know you can if you try  :Wink:

----------


## zahadumy

> No, but you know you can if you try


I have never wrote a single code line in VB 6. I started with VB .NET 2003 and now I'm using VB 2005. So, I won't do that... Your code is really great if it works (and I'm sure it does) but I won't install VB6 just to use it...  :Smilie: 
Cheers!

----------


## iPrank

Sorry, I don't know VB.NET. 
I tried to convert it with VB2005 built-in converter, but it gave me some nasty exceptions.  :Frown:

----------


## killo

i know that the code to hibernate it is: Timer1.Enabled = True but whats the code to wake up?

----------


## iPrank

The *Wait* procedure in modTimer is used to 'wakeup'. What it basically does is, it calls the *SetWaitableTimer* API. The last parameter of the API (fResume) tells it, wheather it will wakeup the system when the timer event occurs or not. (please read 1st link in 1st post for explanation).

*SetSystemPowerState*: This API initiates a hibernation. 

The Timer1 just gives us one second so that we can have enough time to set the timer from Wait procedure before the system hibernates. This timer will wakeup our system.

Please read the *How it works ?* part in the first post and the linked pages and ask me if you need any more explanation.

----------


## Lord Orwell

I am confused.  You are using hibernate and suspend interchangeably.  Which does it do?

----------


## iPrank

Hibernate (and wakeup).

----------


## guyjasper

hi there, i know this is already an old post but this seems to be useful on the application im doing. just wanna ask, if the pc is on a "hibernate" stage, can i wake it up using Wake on Lan? because i already have the WOL function working for my client-server app. and i wanted to add the "hibernate" function on my client app.

----------


## thegreatone

Yes, you can indeed, this is the intended functionality for such a thing.

WOL will attempt to wake the PC, even when in hibernation.

----------


## guyjasper

i see. so, during hibernation, what happens to my app? i have an application that connects to a server app thru winsock. will the connection get lost when the PC hibernates?

----------


## thegreatone

he connection will most likely be lost, however, it shouldn't be that difficult to bring that connection back up when the PC is back in an "awake" state, this could be done by checking the winsocks connection status.

----------


## guyjasper

i see. okies, thanks for the reply. i'll play with this later when i get home  :Wink:  i looked at the code and i was confused on how it sets the PC to hibernate. so i researched the net and found out about SetSystemPowerState() API  :Wink:

----------


## mykewall

great job iPrank!,

I plan to use your code on my PC to see if it works.  I'm somewhat descent with .NET and will attempt to recode it.  If successful, I'll post it back on here for ya.

----------


## abi_allan

hi dude....

im very much thankful to u........

hope u il help me out soon :Alien Frog:

----------


## psenthil_17

> *The Story:*
> 
> I was looking for a alarm clock software that will wake me up at 6:00am so that I can start spaming here at VBF as soon as possible.   But I don't want to keep my PC turned on all night.
> Many modern motherboards supports automatic boot-up alarm. But my motherboard doesn't. 'Wake on LAN' may be an option, but I don't have a LAN. 'Wake on Modem Ring' may be another option, but I gave my dial-up modem to my friend.
> So, here is my workaround: Keep the PC in Hibernate (Suspend) mode and wake it up at 6:00am.
> 
> *The Program:*
> 
> This program simply puts the PC in hibernation state and wakes it up at a pre-defined time. Optionally it can sound an alarm when it wakes up. (You must keep the power on. )
> ...


Hi,

 I gone thorugh you r example. I need to wake up my machine by setting time. My scenario is, 10 am i want to switch on. 01:00 pm stand by and 2pm wake up. 4 pm stand by. 6 wake up. for this purpose we can use timer control. From ur example i tried. but its not diaplying the form whn i set time to wake up. wht to do

----------


## Lord Orwell

well i finally see how this works.  I was under the impression that hibernate shuts the system off.  Turns out it doesn't.  It does however turn off the ram.

----------


## magicsoft

> Hi,
> 
>  I gone thorugh you r example. I need to wake up my machine by setting time. My scenario is, 10 am i want to switch on. 01:00 pm stand by and 2pm wake up. 4 pm stand by. 6 wake up. for this purpose we can use timer control. From ur example i tried. but its not diaplying the form whn i set time to wake up. wht to do


I also want to know, thank you.

----------


## Chanakya69

I know its very old thread... I just want to say.. kudos to iPrank.

----------

