?
Solved

Need help with visual basic 6 error handling

Posted on 2011-03-04
3
Medium Priority
?
353 Views
Last Modified: 2012-05-11
I wrote a script using WMI that will enumerate all currently running processes. For each process it enumerates, it then executes another WMI script that enumerates all DLLs for that process. All processes and DLLs need to be stored in arrays. The DLL array only adds DLLs that are not already in the array, so that there are no duplicates. The first issue I ran into is that when I check the DLL array for duplicates values, it throws an error on the very first loop, since I have not redimmed the array yet. I fixed this by doing the following:
If array is not initialized and throws an error,  jump over the code that checks the array for duplicates, add the DLL to the array and redim the array. Then on the next loop, it is already initialized and works fine. This was fine until I discovered that if a process is terminated during this loop, it will query for DLLs using an invalid PID. The query does not give an error, but when I try to use the object in any way, it gives an error. This is easily fixed by using "on error resume next", but I cannot seem to get error handling to work for both of these issues. The error handling for the uninitialized array works by itself and the error handling for the invalid PID works by itself, but they do not work when they are both added to the code. Obviously I am going about this wrong. The code I posted does not have the error handling, since it did not work. Basically, I just need proper error handling for the two errors or if someone knows how to handle the uninitialized array in a better way.
'GET RUNNING PROCESSES
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Debug)}!\\" & "." & "\root\cimv2")
    Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process")
    
    For Each objProcess In colProcess
        processName = ""
        processPath = ""
        processPath = LCase(objProcess.ExecutablePath)
        processPid = objProcess.ProcessID
        processName = LCase(objProcess.Name)
    
        Dim arrProcessPath()
        Dim arrProcessName()
        Dim arrProcessPid()
        Dim arrModules()
        processCount = processCount + 1
        ReDim Preserve arrProcessPath(processCount)
        ReDim Preserve arrProcessName(processCount)
        ReDim Preserve arrProcessPid(processCount)
    
        'LOAD PROCESS PATHS INTO AN ARRAY
        arrProcessPath(processCount) = processPath
    
        'LOAD PROCESS NAMES INTO AN ARRAY
        arrProcessName(processCount) = processName

        'LOAD PROCESS PID INTO AN ARRAY
        arrProcessPid(processCount) = processPid

        'LOAD PROCESS MEMORY INTO AN ARRAY
        arrProcessMemory(processCount) = objProcess.WorkingSetSize
    
        'LOAD MODULES INTO AN ARRAY
        strAssocQuery = "ASSOCIATORS OF {Win32_Process.Handle='" & processPid & "'} WHERE ResultClass=CIM_DataFile"
        Set colModule = objWMIService.ExecQuery(strAssocQuery, , 48)

'#### IF PID DOES NOT EXIST ANYMORE, YOU WILL GET AN ERROR ON THIS LINE ####
        For Each module In colModule
            DoEvents
        
            'IF NOT VALID MOD FILE, SKIP MOD
            If module.Name <> "" And Right(module.Name, 4) <> ".exe" And Right(module.Name, 4) <> ".com" Then

                'TEST IF MODULE PATH IS IN ARRAY ALREADY
                numModules = 0
'#### IF IT IS THE FIRST LOOP, YOU GET AN ERROR HERE FROM TRYING TO USE AN UNINITILIZED ARRAY, SO ERROR HANDLING NEEDED HERE
                For xx = 0 To UBound(arrModules)
                    If module.Name = arrModules(xx) Then
                        numModules = numModules + 1
                    End If
                Next

                'IF MODULE PATH IS NOT IN ARRAY, THEN ADD IT TO THE ARRAY
                If numModules = 0 Then
                    moduleCount = moduleCount + 1
                    ReDim Preserve arrModules(moduleCount)
                    arrModules(moduleCount) = module.Name
                End If
            End If
        Next
    Next

Open in new window

0
Comment
Question by:advcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 35034755
Hi, you would be best to use a dictionary object, rather than an array:
http://support.microsoft.com/kb/187234

So, instead of all of this:

                'TEST IF MODULE PATH IS IN ARRAY ALREADY
                numModules = 0
'#### IF IT IS THE FIRST LOOP, YOU GET AN ERROR HERE FROM TRYING TO USE AN UNINITILIZED ARRAY, SO ERROR HANDLING NEEDED HERE
                For xx = 0 To UBound(arrModules)
                    If module.Name = arrModules(xx) Then
                        numModules = numModules + 1
                    End If
                Next

                'IF MODULE PATH IS NOT IN ARRAY, THEN ADD IT TO THE ARRAY
                If numModules = 0 Then
                    moduleCount = moduleCount + 1
                    ReDim Preserve arrModules(moduleCount)
                    arrModules(moduleCount) = module.Name
                End If

Open in new window


Just use this

If dctModules.Exists(arrModules(xx)) = False Then dctModules.Add arrModules(xx), 0

Open in new window


Regards,

Rob.
0
 

Author Comment

by:advcom
ID: 35038400
That worked perfectly and is much cleaner, thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35045096
No worries. Glad to help.  You could use Dictionaries for your other arrays there too.

Regards,

Rob.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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