0

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for implementing one possible solution and calling MsgBox correctly.
1

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.

2 Comments

What do you mean by no parentheses allowed? Given the value passed to msgbox is valid, the code works whether or not a parenthesis is used.
WRT parentheses when calling a sub, see blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.