KillProcess – System Function**





KillProcess Function


Usage

The KillProcess function kills the specified process. This function is not available in WinTask Lite.

Used to kill a process specified by its internal Windows PID. It should be used only in advanced system scripts, as KillApp function is much easier to use if you need to kill an application.

Syntax

KillProcess(<PID_name>, 0|1)
or
ret = KillProcess(<PID_name>, 0|1)

Parameters

  • <PID_name>: numeric, PID of the process.
  • If the second parameter is 1, the process is stopped immediately. If 0 and the process is not stopped after 60 secs, a confirmation dialog box is displayed. If the user does not click OK, the process is then stopped.

Return Value

Ret, numeric return code. Possible values:

  • 0 if the process has been stopped successfully.
  • 1 if the process has been stopped after user confirmation.
  • Negative values if the process has not been stopped:
    • -2 if the <PID_name> is incorrect.
    • -3 if the function has been cancelled by the user.
    • -4 if access is denied.
    • -1 for any other reason.

Example

This complete example displays a dialog box with a list of all active windows and their associated process IDs. With this dialog box, the user can kill (forced or not) the process chosen.

'Arrays for window information
Dim name$ ( 50 )
Dim instance( 50 )
Dim handles ( 50 )
Dim flags$ (50)
'-----------------------------------------------------
'Arrays for processes
Dim name_proc$ ( 50 )
Dim pid ( 50 )
Dim times ( 50 )
Dim memory ( 50 )
'-----------------------------------------------------
BEGINDIALOG liste 79, 70, 467, 326
    CAPTION "Window list"
    TEXT label$, 93, 16
    LISTBOX name$(), 28, 45, 403, 156, win$
    PUSHBUTTON "&Quit", canc, 359, 243, 76, 24
    DEFPUSHBUTTON "&Activate App", activat, 29, 243, 76, 24
    PUSHBUTTON "&Close App", clos, 115, 243, 75, 24
    PUSHBUTTON "Kill(&forced) App", killed, 196, 243, 90, 24
    PUSHBUTTON "&Refresh", refresh, 292, 243, 60, 24
ENDDIALOG
'-----------------------------------------------------
sub kill_process(name_win$, param)
    local pos, i, find
    pos = instr(name_win$, "|")
    name_exe$ = left$(name_win$, pos - 1)
    'msgbox(name_exe$)
    size = GetProcessList(name_proc$(), pid(), times(), memory(), 1)
    i = 0
    name_exe$ = ucase$(name_exe$)
    repeat
        if ucase$(name_proc$(i)) = name_exe$ then
            'msgbox("compared with : " + name_proc$(i))
            proc_id = pid(i)
            find = 1
        else
            i = i + 1
        endif
    until i = size or find = 1
    if find = 1 then
        if killed = 1 then
            ret = KillProcess(proc_id, 1)
        else
            ret = KillProcess(proc_id, 0)
        endif
        msgbox("Return code from KillProcess : " + str$(ret))
    else
        msgbox("No corresponding process for : " + name_exe$)
    endif
endsub
'------------------------------------------------------
sub displayed()
    killed = 0
    CallDialog liste
    if canc = 1 then
        stop
    else
        #IgnoreErrors = 1
        'msgframe("<" + win$ + ">", 1, 0, 0, 8)
        'pause 1
        if activat = 1 then
            usewindow(win$)
        else
            if killed = 1 then
                kill_process(win$, 1)
            else
                if refresh <> 1 then
                    kill_process(win$, 0)
                endif
            endif
        endif
        #IgnoreErrors = 1
    endif
endsub
'-------------------------------------------------------
sub terminate()
    stop
endsub
'-------------------------------------------------------
OnAction end_process
    'If the user presses Alt+F1, the script stops.
    key(">")
    dosub terminate
EndAction
'********************************************************
' MAIN PROGRAM
'********************************************************
#IgnoreErrors = 1
repeat
    name$() = ""
    instance() = 0
    handles() = 0
    flags$() = ""
    size = GetWindowsList(name$(), instance(), handles(), flags$())
    if size <> -1 then
        label$ = "Total of active windows : " + str$(size)
        displayed()
    else
        msgbox("Array too small")
        label$ = "Total of active windows : "
        displayed()
    endif
until 1 = 2