Question

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.

Was it helpful?

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

OTHER TIPS

As MsgBox needs a string to display and Null can't be stringyfied, your MsgBox line (btw: no parentheses allowed) will fail for nasty .ExecutablePathes; InStr(), however, allows for the first parameter to be Null (see the docs). Evidence:

>> MsgBox Null
>>
Error Number:       94
Error Description:  Invalid use of Null
>> p = Instr(Null, "whatever")
>>
>> WScript.Echo TypeName(p)
>>
Null

So get rid of the diagnostics or write a Sub/Function that deals with Null (and perhaps other border line cases like Empty) in an appropriate way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top