Solved

GetModuleFileNameEx problem in visual basic process paths

Posted on 2004-09-13
6
1,097 Views
Last Modified: 2008-02-01
Hello,

I am using this module to grab path's from pid's.  This code only grabs paths to processes with windows.  I can't seem to find any code that works properly and actually grabs ALL of the process paths (or atleast most of them).  Any ideas??



Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

Public Sub GetProcesses(lstAddTo As ListBox)
   
   Dim lProcCount     As Long
   Dim lProcPoss      As Long
   Dim lNum           As Long
   Dim ProcessIDs()   As Long
   Dim cbNeeded2      As Long
   Dim NumElements2   As Long
   Dim arrModules(1 To 200) As Long
   Dim lRet           As Long
   Dim sModName       As String
   Dim hwndProcess    As Long
   Dim iCounter       As Long
   
   Const SIZE = 500
   Const PROCESS_QUERY_INFORMATION = 1024
   Const PROCESS_VM_READ = 16
   Const PROCESS_ALL_ACCESS = &H1F0FFF
   
   'need to get the array containing the process id's for each process object
   
   lProcCount = 8
   lProcPoss = 96
   Do While lProcCount <= lProcPoss
       lProcCount = lProcCount * 2
       ReDim ProcessIDs(lProcCount / 4) As Long
       lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
   Loop
   lNum = lProcPoss / 4
   
   For iCounter = 1 To lNum
       'Get a handle to the Process
       hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessIDs(iCounter))
       'Got a Process handle
       If hwndProcess <> 0 Then
           'Get an array of the module handles for the specified
           'process
           lRet = EnumProcessModules(hwndProcess, arrModules(1), 200, cbNeeded2)
           'If the Module Array is retrieved, Get the ModuleFileName
           If lRet <> 0 Then
               sModName = Space(255)
               lRet = GetModuleFileNameExA(hwndProcess, arrModules(1), sModName, SIZE)
               lstAddTo.AddItem ProcessIDs(iCounter) & vbTab & Left(sModName, lRet)
           End If
       End If
       'Close the handle
       lRet = CloseHandle(hwndProcess)
   Next
   
End Sub
0
Comment
Question by:bluedragon99
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:lbertacco
ID: 12052547
I tried it and it seems to work fine with vb6 but not with visualStudio.net. Which one are you using? With VS.NET it seems that calling EnumProcesses doesn't properly update the lProcPoss variable (so you jsut get the first 12 processes). To check if this is the problem in your case, try initializing both lproccount and lprocposs to 400 and 800 repsectively (instead of 8 and 96) thus you should get (the first) 200 processes (instead of 12). Note that this is not a solution, but is just to verify where the problem is.
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 12053153
I'm using vb6.  It is only showing proccesses with windows.  I was able to get it to show more than 12 by opening a bunch of apps with windows.
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 12053975
Ok so you are actually retrieving all the processes in the array ProcessIds, but then openProcess fails because of access rights. Try with the following. I'm not sure about the remaining openprocess errors (some process might have died...)

Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function GetProcessImageFileNameA Lib "psapi.dll" (ByVal hProcess As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

Public Sub GetProcesses(lstAddTo As ListBox)
   
   Dim lProcCount     As Long
   Dim lProcPoss      As Long
   Dim lNum           As Long
   Dim ProcessIDs()   As Long
   Dim cbNeeded2      As Long
   Dim NumElements2   As Long
   Dim arrModules(1 To 200) As Long
   Dim lRet           As Long
   Dim sModName       As String
   Dim hwndProcess    As Long
   Dim iCounter       As Long
   
   Const SIZE = 500
   Const PROCESS_QUERY_INFORMATION = 1024
   Const PROCESS_VM_READ = 16
   Const PROCESS_ALL_ACCESS = &H1F0FFF
   
   'need to get the array containing the process id's for each process object
   
   lProcCount = 8
   lProcPoss = 96
   Do While lProcCount <= lProcPoss
       lProcCount = lProcCount * 2
       ReDim ProcessIDs(lProcCount / 4) As Long
       lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
   Loop
   lNum = lProcPoss / 4
   Debug.Print lProcPoss / 4
   For iCounter = 1 To lNum
       'Get a handle to the Process
       hwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, ProcessIDs(iCounter))
       'Got a Process handle
       If hwndProcess = 0 Then
           sModName = "[OpenProcess Failed]"
       Else
           'Get an array of the module handles for the specified
           'process
           sModName = Space(SIZE)
           lRet = GetProcessImageFileNameA(hwndProcess, sModName, SIZE)
           If lRet = 0 Then
               sModName = ""
           Else
               sModName = Left(sModName, lRet \ 2 + 1)
           End If
       End If
       lstAddTo.AddItem ProcessIDs(iCounter) & vbTab & sModName
       Debug.Print iCounter, ProcessIDs(iCounter), sModName
       'Close the handle
       lRet = CloseHandle(hwndProcess)
   Next
   
End Sub
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Author Comment

by:bluedragon99
ID: 12054110
Nice!  but now the paths are \device\harddiskvolume2\windows\system32\winlogon.exe

any easy way to convert or have it spit out C:\windows\system32\winlogon.exe


it's getting close!
0
 
LVL 11

Accepted Solution

by:
lbertacco earned 500 total points
ID: 12054409
Well, sorry if I didn't mention but you can keep using your previous code to get names. The only relevant changes were the rights passed to OpenProcess and the handling of an error in return:

Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function GetProcessImageFileNameA Lib "psapi.dll" (ByVal hProcess As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

Public Sub GetProcesses(lstAddTo As ListBox)
   
   Dim lProcCount     As Long
   Dim lProcPoss      As Long
   Dim lNum           As Long
   Dim ProcessIDs()   As Long
   Dim cbNeeded2      As Long
   Dim NumElements2   As Long
   Dim hModule        As Long
   Dim lRet           As Long
   Dim sModName       As String
   Dim hwndProcess    As Long
   Dim iCounter       As Long
   
   Const SIZE = 500
   Const PROCESS_QUERY_INFORMATION = 1024
   Const PROCESS_VM_READ = 16
   Const PROCESS_ALL_ACCESS = &H1F0FFF
   
   'need to get the array containing the process id's for each process object
   
   lProcCount = 8
   lProcPoss = 96
   Do While lProcCount <= lProcPoss
       lProcCount = lProcCount * 2
       ReDim ProcessIDs(lProcCount / 4) As Long
       lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
   Loop
   lNum = lProcPoss / 4
   Debug.Print lProcPoss / 4
   For iCounter = 1 To lNum
       'Get a handle to the Process
       hwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessIDs(iCounter))
       'Got a Process handle
       If hwndProcess = 0 Then
           sModName = "[OpenProcess Failed]"
       Else
           'Get an array of the module handles for the specified
           'process
           sModName = Space(SIZE)
           lRet = EnumProcessModules(hwndProcess, hModule, 4, cbNeeded2)
           'If the Module Array is retrieved, Get the ModuleFileName
           If lRet <> 0 Then
               sModName = Space(SIZE)
               lRet = GetModuleFileNameExA(hwndProcess, hModule, sModName, SIZE)
                If lRet = 0 Then
                    sModName = ""
                Else
                    sModName = Left(sModName, lRet)
                End If
           Else
               sModName = "[EnumProcessModules Failed]"
           End If
       End If
       lstAddTo.AddItem ProcessIDs(iCounter) & vbTab & sModName
       Debug.Print iCounter, ProcessIDs(iCounter), sModName
       'Close the handle
       lRet = CloseHandle(hwndProcess)
   Next
   
End Sub



0
 
LVL 1

Author Comment

by:bluedragon99
ID: 12054511
Thanks man!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 109
word0 challenge 3 91
python question 5 76
Plain Text Editor for iPad 6 77
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 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