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.

Ian
  • 4,169
  • 3
  • 37
  • 62

2 Answers2

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.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • 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. – Daniel Nov 14 '12 at 20:59
  • WRT parentheses when calling a sub, see http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx – Ekkehard.Horner Nov 14 '12 at 21:09
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
Daniel
  • 12,982
  • 3
  • 36
  • 60