找回密码
 加入
搜索
查看: 3824|回复: 4

[系统综合] [已解决]自动开机之DllCall问题:SetWaitableTimer函数

[复制链接]
发表于 2013-5-2 00:42:58 | 显示全部楼层 |阅读模式
本帖最后由 wsycharles0o 于 2013-5-2 11:20 编辑

我想通过DllCall实现形如这种功能:

两个例子基本相同:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687008(v=vs.85).aspx

http://stackoverflow.com/questio ... h-has-been-shutdown

需要用到Kernel32.dll中这三个函数:
CreateWaitableTimer
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682492(v=vs.85).aspx
SetWaitableTimer
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686289(v=vs.85).aspx
WaitForSingleObject
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx

这是我用Autoit写的源码:
#include <Array.au3>
#include <Date.au3>

Const $Duration=15


$OriTime = _Date_Time_GetSystemTimeAsFileTime()

$OriTimeArray = _Date_Time_FileTimeToArray($OriTime)

$OriTimeArray[4]+=$Duration

$TimeOBJ=_Date_Time_EncodeFileTime($OriTimeArray[0],$OriTimeArray[1],$OriTimeArray[2],$OriTimeArray[3],$OriTimeArray[4],$OriTimeArray[5],$OriTimeArray[6])


$kr32=DllOpen("Kernel32.dll")

$DllRes1=DllCall($kr32,"HANDLE","CreateWaitableTimer","INT_PTR",0,"BOOL",1,"str","MyTimer")

$Timer=$DllRes1[0]

$DllRes2=DllCall($kr32,"BOOL","SetWaitableTimer","HANDLE",$Timer,"INT64*",$TimeOBJ,"long",0,"INT_PTR",0,"INT_PTR",0,"BOOL",1)

_ArrayDisplay($DllRes2)

MsgBox(0,Hex($DllRes2[0]),"=v=")

$DllRes3=DllCall($kr32,"DWORD","WaitForSingleObject","HANDLE",$Timer,"UINT",0xFFFFFFFF)

_ArrayDisplay($DllRes3)

DllClose($kr32)

;PENDING DEBUG
运行的结果是SetWaitableTimer后瞬间就signal并没有等设置duration...

求DllCall大神解答。

Ps.自认为鄙人英文还不错,有关于英语的理解问题可以附在回复里。
发表于 2013-5-2 08:17:52 | 显示全部楼层
#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):     http://msdn.microsoft.com/library/default....error_codes.asp
;
; 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=False,$force=true)
    $result = DllCall("PowrProf.dll", "long", "SetSuspendState", "long",$mode, "long",$force, "long", false)
EndFunc






;示例        .注意必需先设置唤醒时间,否则无法唤醒
;~ SetWakeUpTime(@HOUR,@min+1); wakeup the system in 1 minutes from now
;~ SetSuspend(); go to hibernate mode
 楼主| 发表于 2013-5-2 09:56:06 | 显示全部楼层
F**K !!!!! UDF里面居然有!?!?!?
 楼主| 发表于 2013-5-2 09:56:10 | 显示全部楼层
F**K !!!!! UDF里面居然有!?!?!?
发表于 2013-5-2 13:27:57 | 显示全部楼层
我算是白帮忙了,不谢也就算了,居然爆粗口

评分

参与人数 1金钱 +30 贡献 +2 收起 理由
pusofalse + 30 + 2 没白忙活~ - -|||

查看全部评分

您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-29 05:38 , Processed in 0.078102 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表