CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2005
    Posts
    7

    finding file path from process name

    hi I need to find the file path for a running process. I have the code to get a list of the running processes, but cannot find a way to find the paths. Can anyone help?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: finding file path from process name

    Quote Originally Posted by kingbob86
    hi I need to find the file path for a running process. I have the code to get a list of the running processes, but cannot find a way to find the paths. Can anyone help?
    Take a look GetModuleFileNameExA API function. This function will return the full path and the file name of the EXE file.
    You would need to specify the handle of the process and handle of the module.

    Also try www.allapi.net web-site. You can download a nice tool from there which shows you how to use API functions. May come handy.

  3. #3
    Join Date
    Nov 2005
    Posts
    7

    Re: finding file path from process name

    thanks for that, it has helped me understand it a bit better, but I am not very strong on API calls. Can someone provide a code sample if possible?

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: finding file path from process name

    Quote Originally Posted by kingbob86
    thanks for that, it has helped me understand it a bit better, but I am not very strong on API calls. Can someone provide a code sample if possible?
    Download the API-Guide from www.allapi.net web-site. This tool contains sample code and a small explanation of the API functions.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: finding file path from process name

    Code:
     Option Explicit
    
    	'General Declarations
    
          Private Declare Function Process32First Lib "kernel32" ( _
             ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
          Private Declare Function Process32Next Lib "kernel32" ( _
             ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
          Private Declare Function CloseHandle Lib "Kernel32.dll" _
             (ByVal Handle As Long) As Long
    
          Private Declare Function OpenProcess Lib "Kernel32.dll" _
            (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
                ByVal dwProcId As Long) As Long
    
          Private Declare Function EnumProcesses Lib "psapi.dll" _
             (ByRef lpidProcess As Long, ByVal cb As Long, _
                ByRef cbNeeded As Long) As Long
    
          Private Declare Function GetModuleFileNameExA Lib "psapi.dll" _
             (ByVal hProcess As Long, ByVal hModule As Long, _
                ByVal ModuleName As String, ByVal nSize As Long) As Long
    
          Private Declare Function EnumProcessModules Lib "psapi.dll" _
             (ByVal hProcess As Long, ByRef lphModule As Long, _
                ByVal cb As Long, ByRef cbNeeded As Long) As Long
    
          Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
             ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    
          Private Declare Function GetVersionExA Lib "kernel32" _
             (lpVersionInformation As OSVERSIONINFO) As Integer
    
          Private Type PROCESSENTRY32
             dwSize As Long
             cntUsage As Long
             th32ProcessID As Long           ' This process
             th32DefaultHeapID As Long
             th32ModuleID As Long            ' Associated exe
             cntThreads As Long
             th32ParentProcessID As Long     ' This process's parent process
             pcPriClassBase As Long          ' Base priority of process threads
             dwFlags As Long
             szExeFile As String * 260       ' MAX_PATH
          End Type
    
          Private Type OSVERSIONINFO
             dwOSVersionInfoSize As Long
             dwMajorVersion As Long
             dwMinorVersion As Long
             dwBuildNumber As Long
             dwPlatformId As Long           '1 = Windows 95.
                                            '2 = Windows NT
    
             szCSDVersion As String * 128
          End Type
    
          Private Const PROCESS_QUERY_INFORMATION = 1024
          Private Const PROCESS_VM_READ = 16
          Private Const MAX_PATH = 260
          Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
          Private Const SYNCHRONIZE = &H100000
          'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
          Private Const PROCESS_ALL_ACCESS = &H1F0FFF
          Private Const TH32CS_SNAPPROCESS = &H2&
          Private Const hNull = 0
    
          Function StrZToStr(s As String) As String
             StrZToStr = Left$(s, Len(s) - 1)
          End Function
    
          Private Function getVersion() As Long
             Dim osinfo As OSVERSIONINFO
             Dim retvalue As Integer
             osinfo.dwOSVersionInfoSize = 148
             osinfo.szCSDVersion = Space$(128)
             retvalue = GetVersionExA(osinfo)
             getVersion = osinfo.dwPlatformId
          End Function
    
          Private Sub Command1_Click()
          List1.Clear
          Select Case getVersion()
    
          Case 1 'Windows 95/98
    
             Dim f As Long, sname As String
             Dim hSnap As Long, proc As PROCESSENTRY32
             hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
             If hSnap = hNull Then Exit Sub
             proc.dwSize = Len(proc)
             ' Iterate through the processes
             f = Process32First(hSnap, proc)
             Do While f
               sname = StrZToStr(proc.szExeFile)
               List1.AddItem sname
               f = Process32Next(hSnap, proc)
             Loop
    
          Case 2 'Windows NT
    
             Dim cb As Long
             Dim cbNeeded As Long
             Dim NumElements As Long
             Dim ProcessIDs() As Long
             Dim cbNeeded2 As Long
             Dim NumElements2 As Long
             Dim Modules(1 To 200) As Long
             Dim lRet As Long
             Dim ModuleName As String
             Dim nSize As Long
             Dim hProcess As Long
             Dim i As Long
             'Get the array containing the process id's for each process object
             cb = 8
             cbNeeded = 96
             Do While cb <= cbNeeded
                cb = cb * 2
                ReDim ProcessIDs(cb / 4) As Long
                lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
             Loop
             NumElements = cbNeeded / 4
    
             For i = 1 To NumElements
                'Get a handle to the Process
                hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                   Or PROCESS_VM_READ, 0, ProcessIDs(i))
                'Got a Process handle
                If hProcess <> 0 Then
                    'Get an array of the module handles for the specified
                    'process
                    lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                                                 cbNeeded2)
                    'If the Module Array is retrieved, Get the ModuleFileName
                    If lRet <> 0 Then
                       ModuleName = Space(MAX_PATH)
                       nSize = 500
                       lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                                       ModuleName, nSize)
                       List1.AddItem Left(ModuleName, lRet)
                    End If
                End If
              'Close the handle to the process
             lRet = CloseHandle(hProcess)
             Next
    
          End Select
          End Sub

  6. #6
    Join Date
    Nov 2005
    Posts
    7

    Re: finding file path from process name

    Thanks a lot Shuja Ali and HanneSThEGreaT appreciate the help

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: finding file path from process name

    No problem, buddy!

  8. #8
    Join Date
    Jun 2005
    Location
    gandhinagar,gujarat
    Posts
    51

    Talking Re: finding file path from process name

    tx for the code it realy works fine...

    Beacause many codes are on this forum regarding to get process name but no one I found about the path of that process..Your code give path.. Fine and tx

    I further decided to breakup the "path" from process name(!!!!!) I tried a lot but not successed..please tell.

    Actually I want to use shell statement to open the requiste folder in which this exe resides.....

  9. #9
    Join Date
    Sep 2007
    Posts
    2

    Re: finding file path from process name

    I know this is an ancient thread, but all threads I can find on this topic are such old, and none of them can solve my problem, even this one, which does not expected results in Windows XP;
    can anybody point me to a really working source for Win XP?

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: finding file path from process name

    The site remains alive here: http://allapi.mentalis.org/apilist/apilist.php

    and all api's work with xp that are in this thread
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured