Home > Language Reference > System Functions > KillProcess

KillProcess

The KillProcess function kills the specified process.

Usage

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

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

Parameters

<PID_name>. integer, PID of the process to kill.

0|1. If 1, the process is killed immediately. If 0, a confirmation dialog box is displayed; after 60 seconds, the process is killed.

Return value

ret, optional integer return value. If the process has been killed successfully, the return value is 0. If the process has been killed successfully after the user confirmation, the return value is 1. If the function fails, the return value is different a negative value (see System Functions Error Codes).

See also

KillApp
KillAppChildren

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 )
'-----------------------------------------------------
'Define the dialog box which receives the windows list
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)
'Kill the process associated to the window specified in name_win$
local pos,i, find
'Extract the EXE part
pos=instr(name_win$,"|")
name_exe$=left$(name_win$,pos-1)
size = GetProcessList(name_proc$(),pid(),times(),memory(), 1 )
i=0
name_exe$=ucase$(name_exe$)
repeat
if ucase$(name_proc$(i))=name_exe$ then
'Retrieve the PID
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()
'The Sub displays the retrieved windows list using the dialog box defined at the beginning
killed=0
CallDialog liste
if canc=1 then
stop
else
#IgnoreErrors=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("<Alt <F1>>")
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