Solved

Need to get result of WMI query for error handling

Posted on 2011-03-03
3
888 Views
Last Modified: 2012-05-11
I have vbscript that uses WMI to enumerate all 32 and 64 bit processes. Then it takes the results and does another WMI query to get the loaded DLL's. It works most of the time, but often gets an automation error saying "unknown error". I realized that it is getting the list of processes and while it is queying for loaded drivers, a  process stops and so when the script tries to get the loaded DLL's of a process that no longer exists, I get an error when I try to use the results of the query. Obviously I need to check to see if I have a valid object before I use it, but it seems that nothing I do works to see if it is valid. The code is posted below. Here are the functions I have tried.
IsNull
IsObject
IsEmpty
Is Nothing
obj.Count
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Debug)}!\\" & "." & "\root\cimv2")

processPid = 6000 'ANY INVALID PID

strAssocQuery = "ASSOCIATORS OF {Win32_Process.Handle='" & processPid & "'} WHERE ResultClass=CIM_DataFile"
Set colModule = objWMIService.ExecQuery(strAssocQuery, , 48)

'NEED WAY TO MAKE SURE THAT colModule is valid

For Each module In colModule 'GIVES ERROR HERE WHEN INVALID PID
     msgbox module.Name      
Next

Open in new window

0
Comment
Question by:advcom
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35033952
Maybe just this:

strError = ""
On Error Resume Next
For Each module In colModule
     If Err.Number = 0 Then
          msgbox module.Name
     Else
          strError = Err.Number & ": " & Err.Description
          Err.Clear
     End If
Next
If strError <> "" Then MsgBox strError

Open in new window


Regards,

Rob.
0
 

Author Comment

by:advcom
ID: 35033968
That did it, thanks
0
 
LVL 13

Expert Comment

by:connectex
ID: 35033988
Add this code:

processValid = false
Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE ProcessID = ' & processPID & "'")
For Each objProcess in colProcessList
  processFound = true
Next

Now if process is found it's still active or valid and you should be able to query it.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question