DotNetFactory interface functions – SysProcessKill
Close/Terminate Specified Process
Parent page: Service Functions – DotNetFactory (QTP, VBScript)
Description
Create an instance of Process object through DotNetFactory – search for local processes by name – iterate through the returned array and close one by one.
First attempt – “gentle” close (via CloseMainWindow()). WaitForExit() gives a process some time to finalize, you may customize the timeout. If didn’t work – force close and unload (via Kill()).
Note. Name is case-insensitive but no wildcards supported.
The function returns “True” if no specified processes found remaining.
Errors/exceptions might be thrown if your account’s privileges do not allow closing the process (you can first try doing this via TaskManager to see if you are allowed). You can also wrap external calls in On Error statement.
Implementation
Public Function SysProcessKill(ByVal sName)
Dim objSDPMain, objSDP
Dim objSDPArr
Dim boolRC
Dim Iter
Set objSDPMain = DotNetFactory.CreateInstance("System.Diagnostics.Process", "System")
Set objSDPArr = objSDPMain.GetProcessesByName(sName)
' Trying to close first
For Iter=0 To objSDPArr.length-1
Set objSDP = objSDPArr.GetValue(CInt(Iter))
objSDP.CloseMainWindow()
objSDP.WaitForExit(CInt(5000))
Next
Set objSDPArr = objSDPMain.GetProcessesByName(sName)
'If anything is still left, trying to terminate
For Iter=0 To objSDPArr.length-1
Set objSDP = objSDPArr.GetValue(CInt(Iter))
objSDP.Kill()
objSDP.WaitForExit(CInt(5000))
Next
'Checking if something is still in memory
Set objSDPArr = objSDPMain.GetProcessesByName(sName)
boolRC = CInt(objSDPArr.length) = 0
Set objSDP = Nothing
Set objSDPArr = Nothing
Set objSDPMain = Nothing
SysProcessKill = boolRC
End Function
Examples
boolBanished = True
boolRC = SysProcessExists("iexplore")
If boolRC Then
boolGhostProcess = True
boolBanished = boolBanished AND SysProcessKill("iexplore")
End If

