Solved

Need help with visual basic 6 error handling

Posted on 2011-03-04
3
349 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
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now