KillProcess
System function.
The KillProcess function kills the specified process. Not available in WinTask Lite.
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
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 the second parameter is 0 and if 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. 0 if the process has been stopped successfully, 1 if the process has been stopped after user confirmation; a negative value is returned if the process has not been stopped. The possible negative error codes are -2 if the <PID_name> is incorrect, -3 if the function has been cancelled by the user, -4 if the access is denied, -1 for any other reason.
Depending on the setting of #IgnoreErrors, a negative return code stops the script and an error message is displayed or the return code can be tested in order to continue executing the script. See Error Handling.
Example code
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("<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