Jump to content

Hibernate and wakeup the computer


bastel123
 Share

Recommended Posts

Hi,

if you want to hibernate your system and wake it up at a specific time you can try this code :

#include <date.au3>

;===============================================================================
;
; Description:    Sets a wakeup time to wake it up if the system / computer is hibernating or standby
; Parameter(s):  $Hour  - Hour Values   : 0-23
;                   $Minute - Minutes Values: 0-59
;                   $Day    - Days Values   : 1-31  (optional)
;                  $Month   - Month Values  : 1-12  (optional)
;                  $Year    - Year Values   : > 0   (optional)
;
; Requirement(s):   DllCall
; Return Value(s):  On Success - 1
;                  On Failure - 0 sets @ERROR = 1 and @EXTENDED (Windows API error code)
;
; Error code(s):    [url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp]http://msdn.microsoft.com/library/default....error_codes.asp[/url]
;
; Author(s):        Bastel123 aka Sebastian
; Note(s):        -
;
;===============================================================================
func SetWakeUpTime($Hour,$Minute,$Day=@mday,$Month=@mon,$Year=@YEAR)

$SYSTEMTIME = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
$lpSYSTEMTIME = DllStructGetPtr($SYSTEMTIME)
$LOCALFILETIME=DllStructCreate("dword;dword")
$lpLOCALFILETIME = DllStructGetPtr($LOCALFILETIME)
$DueTime=DllStructCreate("dword;dword")
$lpDueTime=DllStructGetPtr($DueTime)

DllStructSetData($SYSTEMTIME, 1, $Year)
DllStructSetData($SYSTEMTIME, 2, $Month)
DllStructSetData($SYSTEMTIME, 3, _DateToDayOfWeek($Year,$Month,$Day)-1)
DllStructSetData($SYSTEMTIME, 4, $Day)
DllStructSetData($SYSTEMTIME, 5, $Hour)
DllStructSetData($SYSTEMTIME, 6, $Minute)
DllStructSetData($SYSTEMTIME, 7, 0)
DllStructSetData($SYSTEMTIME, 8, 0)

$result = DllCall("kernel32.dll", "long", "SystemTimeToFileTime", "ptr", $lpSystemTime, "ptr", $lpLocalFileTime)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
$result = DllCall("kernel32.dll", "long", "LocalFileTimeToFileTime", "ptr", $lpLocalFileTime, "ptr", $lpLocalFileTime)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
$result = DllCall("kernel32.dll", "long", "CreateWaitableTimer", "long", 0, "long", True, "str", "")
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
DllCall("kernel32.dll", "none", "CancelWaitableTimer", "long",$result[0])

DllStructSetData($DueTime, 1, DllStructGetData($LocalFILETIME, 1))
DllStructSetData($DueTime, 2, DllStructGetData($LocalFILETIME, 2))

$result = DllCall("kernel32.dll", "long", "SetWaitableTimer", "long",$result[0], "ptr", $lpDueTime, "long", 1000, "long", 0, "long", 0, "long", true)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
return 1
EndFunc



;===============================================================================
;
; Description:    Set the computer in Hibernate or Standby Status
; Parameter(s):  $Mode  - Suspend mode  : True=Hibernate, False=Suspend
;                   $Force  - Force-Mode    : True=the system suspends operation immediately
;                                             False=FALSE, the system broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation 
;
; Requirement(s):   DllCall
;
; Author(s):        Bastel123 aka Sebastian
; Note(s):        If the system does not support hibernate use the standby mode       -
;
;===============================================================================
Func SetSuspend($mode=true,$force=true)
    $result = DllCall("PowrProf.dll", "long", "SetSuspendState", "long",$mode, "long",$force, "long", false)
EndFunc







SetWakeUpTime(@HOUR,@min+2); wakeup the system in 2 minutes from now

SetSuspend(); go to hibernate mode

I hope it works on your computer's

Sebastian

Edited by bastel123
Link to comment
Share on other sites

  • 1 month later...

Hi,

I'm trying to write a script that puts my system into stand by and then wakes it up for work purposes.

When I try to use your code, I got compile errors saying that the following functions are not defined:

DllStructCreate()

DllStructGetPtr()

DllStructSetData()

DllStructGetData()

Where do these come from? Any help would be much appreciated. Thx! :think:

Link to comment
Share on other sites

Hi,

I'm trying to write a script that puts my system into stand by and then wakes it up for work purposes.

When I try to use your code, I got compile errors saying that the following functions are not defined:

DllStructCreate()

DllStructGetPtr()

DllStructSetData()

DllStructGetData()

Where do these come from? Any help would be much appreciated. Thx! :think:

beta ^^ :(

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity
Link to comment
Share on other sites

I just tried this out, and it ran the code just fine but nothing happens. I tried both $mode=true and $mode=false (hibernate and suspend), neither did anything, but also neither returns an error.

My machine is running windows XP w/SP2, and has the PowrProf.dll (i checked to insure that I had the this dll it was calling).

Edited by Diverge
Link to comment
Share on other sites

  • Moderators

Are you running it in Beta?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Are you running it in Beta?

Yep, othewise I would have gotten the errors the others got above. I guess I can compile it and try it on some other machines at work just for the hell of it... maybe it doesn't like my machine.

Edit: Hmm, it seems it just doesn't like my work pc. I tried it on another pc in the lab and it worked just fine.

Edited by Diverge
Link to comment
Share on other sites

  • 1 year later...

Hi,

if you want to hibernate your system and wake it up at a specific time you can try this code :

#include <date.au3>

;===============================================================================
;
; Description:    Sets a wakeup time to wake it up if the system / computer is hibernating or standby
; Parameter(s):  $Hour  - Hour Values   : 0-23
;                   $Minute - Minutes Values: 0-59
;                   $Day    - Days Values   : 1-31  (optional)
;                  $Month   - Month Values  : 1-12  (optional)
;                  $Year    - Year Values   : > 0   (optional)
;
; Requirement(s):   DllCall
; Return Value(s):  On Success - 1
;                  On Failure - 0 sets @ERROR = 1 and @EXTENDED (Windows API error code)
;
; Error code(s):    [url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp]http://msdn.microsoft.com/library/default....error_codes.asp[/url]
;
; Author(s):        Bastel123 aka Sebastian
; Note(s):        -
;
;===============================================================================
func SetWakeUpTime($Hour,$Minute,$Day=@mday,$Month=@mon,$Year=@YEAR)

$SYSTEMTIME = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
$lpSYSTEMTIME = DllStructGetPtr($SYSTEMTIME)
$LOCALFILETIME=DllStructCreate("dword;dword")
$lpLOCALFILETIME = DllStructGetPtr($LOCALFILETIME)
$DueTime=DllStructCreate("dword;dword")
$lpDueTime=DllStructGetPtr($DueTime)

DllStructSetData($SYSTEMTIME, 1, $Year)
DllStructSetData($SYSTEMTIME, 2, $Month)
DllStructSetData($SYSTEMTIME, 3, _DateToDayOfWeek($Year,$Month,$Day)-1)
DllStructSetData($SYSTEMTIME, 4, $Day)
DllStructSetData($SYSTEMTIME, 5, $Hour)
DllStructSetData($SYSTEMTIME, 6, $Minute)
DllStructSetData($SYSTEMTIME, 7, 0)
DllStructSetData($SYSTEMTIME, 8, 0)

$result = DllCall("kernel32.dll", "long", "SystemTimeToFileTime", "ptr", $lpSystemTime, "ptr", $lpLocalFileTime)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
$result = DllCall("kernel32.dll", "long", "LocalFileTimeToFileTime", "ptr", $lpLocalFileTime, "ptr", $lpLocalFileTime)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
$result = DllCall("kernel32.dll", "long", "CreateWaitableTimer", "long", 0, "long", True, "str", "")
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
DllCall("kernel32.dll", "none", "CancelWaitableTimer", "long",$result[0])

DllStructSetData($DueTime, 1, DllStructGetData($LocalFILETIME, 1))
DllStructSetData($DueTime, 2, DllStructGetData($LocalFILETIME, 2))

$result = DllCall("kernel32.dll", "long", "SetWaitableTimer", "long",$result[0], "ptr", $lpDueTime, "long", 1000, "long", 0, "long", 0, "long", true)
If $result[0] = 0 Then
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    SetExtended($lastError[0])
    SetError(1)
    Return 0
EndIf
return 1
EndFunc
;===============================================================================
;
; Description:    Set the computer in Hibernate or Standby Status
; Parameter(s):  $Mode  - Suspend mode  : True=Hibernate, False=Suspend
;                   $Force  - Force-Mode    : True=the system suspends operation immediately
;                                             False=FALSE, the system broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation 
;
; Requirement(s):   DllCall
;
; Author(s):        Bastel123 aka Sebastian
; Note(s):        If the system does not support hibernate use the standby mode       -
;
;===============================================================================
Func SetSuspend($mode=true,$force=true)
    $result = DllCall("PowrProf.dll", "long", "SetSuspendState", "long",$mode, "long",$force, "long", false)
EndFunc
SetWakeUpTime(@HOUR,@min+2); wakeup the system in 2 minutes from now

SetSuspend(); go to hibernate mode

I hope it works on your computer's

Sebastian

Hi,

I want to do looping for both standby and hibernate. Can anyone help me? I am new in Auto It.

a) Standby/ Hibernate for 1 minute.

:lmao: Resume from Standby / Hibernate.

c) Wait for 1 minute then Standby / Hibernate again.

d) Repeat Step b and c for 10 times. :whistle:

Link to comment
Share on other sites

@bastel123

Is there a way to send the wakeup command immediately after hibernation occurs? See this post/thread for reasoning: http://www.autoitscript.com/forum/index.ph...st&p=357459

I've tried setting the wakeup time to 1 minute from now, but there still tends to be a lag of about 15 seconds between complete hibernation and wake up.

And I imagine that would fluctuate depending on the computer being used. Any thoughts? Thanks, great job BTW. :)

Link to comment
Share on other sites

  • 8 years later...
  • 3 months later...

This thread is 9 years old, what have YOU done to try and get this to work?

Most of the people in this thread haven't been here in years, so I don't think they'll be answering you any time soon.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 1 year later...

No, no one can help you.

This thread is now 11 years old, and as my post just above yours states, most of these posters haven't been here in nearly a decade.

Open a new thread and show what you've tried.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...