Solved

Need help with visual basic 6 error handling

Posted on 2011-03-04
3
352 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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