1

如何使用 AutoIt 检测系统何时关闭或注销?

如果计算机在我的脚本仍在运行时关闭或注销,我需要运行一个函数。

4

3 回答 3

3

您需要处理消息WM_QUERYENDSESSION

示例代码:

; Define Windows Message Codes 
$WM_QUERYENDSESSION = 0x11
; Define Callback Parameter Codes
$ENDSESSION_CLOSEAPP = 0x00000001
$ENDSESSION_CRITICAL = 0x40000000
$ENDSESSION_LOGOFF = 0x80000000

; Register a Callback on the Message to the Function "onShutDown"
GUIRegisterMsg($WM_QUERYENDSESSION, "onShutDown") 

; #FUNCTION# ========================================================
; Name...........: onShutDown Example Function
; Description ...: Application receives the WM_QUERYENDSESSION message
; 
; Parameters ....:
; $hWndGUI - A handle to the window
; $MsgID  - The WM_QUERYENDSESSION identifier
; $wParam - This parameter is reserved for future use
; $lParam - This parameter can be one or more of the following values. 
; |0 - The system is shutting down or restarting 
;      (it is not possible to determine which event is occurring)
; |$ENDSESSION_CLOSEAPP - Application is using a ressource or file that needs to be freed
; |$ENDSESSION_CRITICAL - Application is forced to shut down
; |$ENDSESSION_LOGOFF   - The user is logging off
; 
; Return values .: 
; |True  - Allow to Shutdown
; |False - Prevent Shutdown
; Remarks .......: Applications should respect the user's intentions and return TRUE. By default any application returns **TRUE** for this message.
; If shutting down would corrupt the system or media that is being burned, the application can return **FALSE**. 
; However, it is good practice to respect the user's actions.
; 
Func onShutDown($hWndGUI, $MsgID, $wParam, $lParam)
    Return True  ; allow Shutdown ( or return "False" to prevent Shutdown )
EndFunc

另请参阅:Windows 消息代码列表

于 2014-05-22T18:27:04.400 回答
2

您可以OnAutoItExitRegister()以假设 PC 正在关闭的方式实施。

OnAutoItExitRegister("DetectShutdown")

GUICreate("My GUI") ; will create a dialog box that when displayed is centered
GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While True
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then
        OnAutoItExitUnRegister("DetectShutdown")
        Exit
    EndIf
WEnd

Func DetectShutdown()

    MsgBox(0, "", "Shutdown Detected!")

EndFunc
于 2014-05-23T18:48:27.340 回答
0

如何使用 AutoIt 检测系统何时关闭或注销?

根据文档 - 功能参考 -OnAutoItExitRegister()

可以使用 检索退出模式@exitMethod

还检测用户注销和 Windows 关闭。

如果计算机在我的脚本仍在运行时关闭或注销,我需要运行一个函数。

示例(确定退出方法和对退出方法的反应):

#include <AutoItConstants.au3>; @exitMethod constants.

Global Enum  $EXIT_OK, _
             $EXIT_ONAUTOITEXITREGISTER, _
             $EXIT_HOTKEYSET

Global Const $g_iDelayMain = 250
Global Const $g_iDelayExit = 1000 * 5
Global Const $g_sKeyExit   = '{ESC}'
Global Const $g_sConsole   = '%s-%s-%s %s:%s:%s.%s - @exitCode = %i (%s) @exitMethod = %i (%s)\n'
Global Const $g_aExit      = [ _
                             'OK', _
                             'OnAutoItExitRegister() failed', _
                             'HotKeySet() failed' _
                             ]
Global Const $g_aMethod    = [ _
                             'end of script reached', _
                             'Exit function executed', _
                             'systray exit function activated', _
                             'Windows logoff', _
                             'Windows shutdown' _
                             ]

Global       $g_bStateExit = False

Main()

Func Main()
    If Not OnAutoItExitRegister('_ReportExit')    Then Exit $EXIT_ONAUTOITEXITREGISTER
    If Not HotKeySet($g_sKeyExit, 'SetStateExit') Then Exit $EXIT_HOTKEYSET

    While Not $g_bStateExit
        Sleep($g_iDelayMain)
    WEnd

    Exit $EXIT_OK
EndFunc

Func _ReportExit()
    Local Const $sLine = StringFormat($g_sConsole, _
                            @YEAR, @MON, @MDAY, @HOUR, @MIN, @SEC, @MSEC, _
                            @exitCode,   $g_aExit[@exitCode], _
                            @exitMethod, $g_aMethod[@exitMethod] _
    )

    ConsoleWrite($sLine)
    Sleep($g_iDelayExit)

    Switch @exitMethod

;       Case $EXITCLOSE_NORMAL
            ; Your code here.

;       Case $EXITCLOSE_BYEXIT
            ; Your code here.

;       Case $EXITCLOSE_BYCLICK
            ; Your code here.

        Case $EXITCLOSE_BYLOGOFF
            ; Your code here.

        Case $EXITCLOSE_BYSHUTDOWN
            ; Your code here.

    EndSwitch

EndFunc

Func SetStateExit()
    $g_bStateExit = True
EndFunc
于 2018-05-14T22:30:55.787 回答