Function Reference


_WinAPI_SetProcessShutdownParameters

Sets a shutdown order for a process relative to the other processes in the system

#include <WinAPISys.au3>
_WinAPI_SetProcessShutdownParameters ( $iLevel [, $bDialog = False] )

Parameters

$iLevel The shutdown priority. The system shuts down processes from high $iLevel values to low. The highest
and lowest shutdown priorities are reserved for system components.
This parameter must be in the following range of values.
0x0000-0x00FF - System reserved last shutdown range.
0x0100-0x01FF - Application reserved last shutdown range.
0x0200-0x02FF - Application reserved "in between" shutdown range.
0x0300-0x03FF - Application reserved first shutdown range.
0x0400-0x04FF - System reserved first shutdown range.

All processes start at shutdown level 0x0280.
$bDialog [optional] Specifies whether display a retry dialog box for the user, valid values:
    True - Display a retry dialog box if process takes longer than the specified timeout to shutdown.
    False - Directly terminates the process (Default).

Return Value

Success: True
Failure: False

See Also

Search SetProcessShutdownParameters in MSDN Library.

Example

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

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

Global $g_hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 200, 200)
Local $idButton = GUICtrlCreateButton('', 73, 62, 54, 54, $BS_ICON)
GUICtrlSetImage(-1, @SystemDir & '\shell32.dll', 45)
GUICtrlSetTip(-1, 'Log off ' & @UserName)
Local $idCheck = GUICtrlCreateCheckbox('Block Windows shutdown', 10, 173, 144, 21)
GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION')
GUISetState(@SW_SHOW)

; Set the highest shutdown priority for the current process to prevent closure the other processes.
_WinAPI_SetProcessShutdownParameters(0x03FF)

While 1
        Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        ExitLoop
                Case $idButton
                        Shutdown(0)
                Case $idCheck
                        If GUICtrlRead($idCheck) = $GUI_CHECKED Then
                                _WinAPI_ShutdownBlockReasonCreate($g_hForm, 'This application is blocking system shutdown because the saving critical data is in progress.')
                        Else
                                _WinAPI_ShutdownBlockReasonDestroy($g_hForm)
                        EndIf
        EndSwitch
WEnd

Func WM_QUERYENDSESSION($hWnd, $iMsg, $wParam, $lParam)
        #forceref $iMsg, $wParam, $lParam

        Switch $hWnd
                Case $g_hForm
                        If _WinAPI_ShutdownBlockReasonQuery($g_hForm) Then
                                Return 0
                        EndIf
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_QUERYENDSESSION