Terminate a Windows processTag(s): WinAPI/Registry


In this HowTo, we are killing all the Notepad instance currently running, the script returns the number of instance found.
OleObject mssc
int li_rc
string ls_code
long i

mssc = CREATE OleObject
li_rc = mssc.ConnectToNewObject( "MSScriptControl.ScriptControl" )
mssc.language = "VBScript"

ls_code = "function killnotepad() ~r~n" + &
 "Set locator = CreateObject(~"WbemScripting.SWbemLocator~")~r~n" + &
 "Set service = locator.ConnectServer()~r~n" + &
 "Set props = service.ExecQuery" + &
 "(~"select name, description from Win32_Process" + &
 " where name = 'notepad.exe'~")~r~n" + &
 "num = props.count~r~n" + &
 "for each notepad in props~r~n " + &
 "   notepad.terminate ~r~n " + &
 "next~r~n" + &
 "killnotepad = num~r~n" + &
 "end function"
 // messagebox("", ls_code)
mssc.AddCode(ls_code)             
i = mssc.Eval("killnotepad()")

messagebox("Notepad killed", string(i))

mssc.DisconnectObject()
DESTROY mssc
You need to run this code from an executable. From the IDE, the Terminate method returns "2" (Access is denied).
blog comments powered by Disqus