Function Reference


_WinAPI_RegisterApplicationRestart

Registers the active instance of an application for restart

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

Parameters

$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.
$RESTART_NO_CRASH
$RESTART_NO_HANG
$RESTART_NO_PATCH
$RESTART_NO_REBOOT
$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.

Remarks

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.

Example

#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(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Require Windows Vista or later.')
        Exit
EndIf

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)
GUISetState(@SW_SHOW)

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

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

AdlibRegister('_Countdown', 1000)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _Countdown()
        Local $iData
        #forceref $iData

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