hoppfrosch
1/6/2014 - 12:26 PM

AHK wake-up-timer #ahk #script #function

AHK wake-up-timer #ahk #script #function

; AHK wake-up-timer
; scheduler provides some of the functions of Window's Task Scheduler or the command line tool "schtasks" as an AHK-script. 
; Unlike Windows' Scheduled Tasks this function works with accounts without passwords
;
; Author: boskoop (http://http://www.autohotkey.com/board/topic/10587-wake-up-timer-scheduler-function)

; CONFIGURATION 
Year=2006 
Month=8   ;1-12 
Day=30      ;1-31 
Hour=21   ;0-23 
Minute=16 ;1-59 
Hibernate=0 ;0, 1, 2
Resume=1 ;0,1
Name=%A_Now%

; AUTOEXECUTE 
WakeUp(Year, Month, Day, Hour, Minute, Hibernate, Resume, Name) 

msgbox, I'm awake and working! 


; FUNCTIONS 
WakeUp(Year, Month, Day, Hour, Minute, Hibernate, Resume, Name) 
;Awaits duetime, then returns to the caller (like some sort of "sleep until duetime"). 
;If the computer is in hibernate or suspend mode 
;at duetime, it will be reactivated (hardware support provided) 
;Parameters: Year, Month, Day, Hour, Minute together produce duetime 
;Hibernate: If Hibernate=1, the function hibernates the computer. If Hibernate=2 the computer is set to 
;			suspend-mode
;Resume: If Resume=1, the system is restored from power save mode at due time
;Name: Arbitrary name for the timer
{ 
    duetime:=GetUTCFileTime(Year, Month, Day, Hour, Minute) 

    Handle:=DLLCall("CreateWaitableTimer" 
            ,"char *", 0 
            ,"Int",0 
            ,"Str",name, "UInt") 

    DLLCall("CancelWaitableTimer","UInt",handle) 

    DLLCall("SetWaitableTimer" 
          ,"Uint", handle 
          ,"Int64*", duetime        ;duetime must be in UTC-file-time format! 
          ,"Int", 1000 
          ,"uint",0 
          ,"uint",0 
          ,"int",resume) 
    

    ;Hibernates the computer, depending on variable "Hibernate": 
    If Hibernate=1 		;Hibernate
        { 
        DllCall("PowrProf\SetSuspendState", "int", 1, "int", 0, "int", 0) 
        } 
        
    If Hibernate=2		;Suspend
    	{
    	DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0) 
    	}
    Signal:=DLLCall("WaitForSingleObject" 
            ,"Uint", handle 
            ,"Uint",-1) 
            
    DllCall("CloseHandle", uint, Handle)   ;Closes the handle
    
}    


GetUTCFiletime(Year, Month, Day, Hour, Min) 
;Converts "System Time" (readable time format) to "UTC File Time" (number of 100-nanosecond intervals since January 1, 1601 in  Coordinated Universal Time UTC) 
{ 
    DayOfWeek=0 

    Second=00 
    Millisecond=00 
    

    ;Converts System Time to Local File Time: 
    VarSetCapacity(MyFiletime  , 64, 0) 
    VarSetCapacity(MySystemtime, 32, 0) 
    
    InsertInteger(Year,       MySystemtime,0) 
    InsertInteger(Month,      MySystemtime,2) 
    InsertInteger(DayOfWeek,  MySystemtime,4) 
    InsertInteger(Day,        MySystemtime,6) 
    InsertInteger(Hour,       MySystemtime,8) 
    InsertInteger(Min,        MySystemtime,10) 
    InsertInteger(Second,     MySystemtime,12) 
    InsertInteger(Millisecond,MySystemtime,14) 

    DllCall("SystemTimeToFileTime", Str, MySystemtime, UInt, &MyFiletime) 
    LocalFiletime := ExtractInteger(MyFiletime, 0, false, 8) 

    ;Converts local file time to a file time based on the Coordinated Universal Time (UTC): 
    VarSetCapacity(MyUTCFiletime  , 64, 0) 
    DllCall("LocalFileTimeToFileTime", Str, MyFiletime, UInt, &MyUTCFiletime) 
    UTCFiletime := ExtractInteger(MyUTCFiletime, 0, false, 8) 
    
    Return UTCFileTime 
} 


ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 32) 
; Documented in Autohotkey Help 
{ 
    Loop %pSize%  
        result += *(&pSource + pOffset + A_Index-1) << 8*(A_Index-1) 
    if (!pIsSigned OR pSize > 4 OR result < 0x80000000) 
        return result  
    return -(0xFFFFFFFF - result + 1) 
} 



InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4) 
; Documentated in Autohotkey Help 
{ 
  Loop %pSize% 
          DllCall("RtlFillMemory", UInt, &pDest + pOffset + A_Index-1 
                  , UInt, 1, UChar, pInteger >> 8*(A_Index-1) & 0xFF) 
}