Search code examples
vbscriptexecutable-path

NULL ExecutablePath in vbScript process object


Salutations,

I have the following VBScript:

Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strList

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
   MsgBox(objProcess.ExecutablePath)
   'If InStr(objProcess.ExecutablePath, "EASE") <> 0 Then
   '   MsgBox("TERMINATING")
   '   objProcess.Terminate()
   'End If
Next

For some reason I get an error on the line MsgBox(objProcess.ExecutablePath). It says "Invalid use of Null: 'ExecutablePath'". Oddly enough I do not get this error when I uncomment the commented lines and comment out the problem line.

As you can see I am trying to terminate all processes with a certain path name, but it appears that the string matching isn't working, like there is something wrong with the executable path.


Solution

  • Ekkehard gave a good explanation of the problem, namely that Null cannot be implicitly converted to a string. Now here is a way solve the issue.

    Before trying to use objProcess.ExecutablePath check if it is null:

    For Each objProcess in colProcess
       if not isnull(objProcess.ExecutablePath) then
        MsgBox objProcess.ExecutablePath
       'If InStr(objProcess.ExecutablePath, "EASE") <> 0 Then
       '   MsgBox("TERMINATING")
       '   objProcess.Terminate()
       'End If
       end if
    Next