Function Reference


Registers the active instance of an application for restart

#include <WinAPIDiag.au3>
_WinAPI_RegisterApplicationRestart ( [$iFlags = 0 [, $sCmd = '']] )


$iFlags [optional] The flags that specifies an events when application will not be restarted. This parameter can be
0 or one or more of the following values.
$sCmd [optional] The command-line arguments for the application when it is restarted. The maximum size of the command
line that you can specify is 2048 characters. If this parameter is empty string (Default), the previously
registered command line is removed.

Return Value

Success: 1.
Failure: 0 and sets the @error flag to non-zero, @extended flag may contain the HRESULT error code.


Your initial registration for restart must occur before the application encounters an unhandled exception
or becomes unresponsive. You could then call this function from inside your recovery callback to update the
command line. To prevent cyclical restarts, the system will only restart the application if it has been
running for a minimum of 60 seconds.

If you register for restart and the application encounters an unhandled exception or is not responsive,
the user is offered the opportunity to restart the application; the application is not automatically restarted
without the user's consent.

This function requires Windows Vista or later.

See Also

Search RegisterApplicationRestart in MSDN Library.


#include <APIDiagConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <WinAPIDiag.au3>
#include <WinAPISys.au3>

If Number(_WinAPI_GetVersion()) < 6.0 Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Require Windows Vista or later.')

Global $g_iCount = 10

Local $hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 300, 100)
Local $idLabel = GUICtrlCreateLabel('The application will be crashes after ' & $g_iCount & ' seconds.', 10, 43, 280, 14, $SS_CENTER)

If $CmdLine[0] And ($CmdLine[1] = '/crash') Then
    MsgBox(BitOR($MB_ICONWARNING, $MB_SYSTEMMODAL), 'Attention', 'The application has been restarted after an abnormal exit.', 0, $hForm)

If Not @Compiled Then
    _WinAPI_RegisterApplicationRestart(BitOR($RESTART_NO_PATCH, $RESTART_NO_REBOOT), '"' & @ScriptFullPath & '" /crash')
    _WinAPI_RegisterApplicationRestart(BitOR($RESTART_NO_PATCH, $RESTART_NO_REBOOT), '/crash')

AdlibRegister('_Countdown', 1000)


Func _Countdown()
    Local $iData
    #forceref $iData

    $g_iCount -= 1
    If $g_iCount Then
        GUICtrlSetData($idLabel, 'The application will be crashes after ' & $g_iCount & ' seconds.')
        Local $tData
        ; Forced script crash due to a memory access violation
        $tData = DllStructCreate('int', 0x12345678)
        $iData = DllStructGetData($tData, 1)
EndFunc   ;==>_Countdown