DotNetFactory interface functions – SysProcessKill

Posted by Albert Gareev on Jan 06, 2011 | Categories: DotNetClassesSource code

Close/Terminate Specified Process

Parent page: Service Functions – DotNetFactory (QTP, VBScript)


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.


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))

 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))

 '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


boolBanished = True
 boolRC = SysProcessExists("iexplore")
 If boolRC Then
  boolGhostProcess = True
  boolBanished = boolBanished AND SysProcessKill("iexplore")
 End If

  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.