Results 1 to 15 of 15

Thread: find out the .exe

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    15

    find out the .exe

    Hi!

    I have the processID and the window handler. I need the .exe of the application. can you help me?

    Thank you Wuerfel

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Public Const TH32CS_SNAPPROCESS As Long = 2&
    2. Public Const MAX_PATH As Long = 260
    3.  
    4. Public Type PROCESSENTRY32
    5. dwSize As Long
    6. cntUsage As Long
    7. th32ProcessID As Long
    8. th32DefaultHeapID As Long
    9. th32ModuleID As Long
    10. cntThreads As Long
    11. th32ParentProcessID As Long
    12. pcPriClassBase As Long
    13. dwflags As Long
    14. szexeFile As String * MAX_PATH
    15. End Type
    16.  
    17. Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd
    18. As Long, lpdwProcessId As Long) As Long
    19.  
    20. Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias
    21. "CreateToolhelp32Snapshot" (ByVal lFlgas As Long, ByVal lProcessID As
    22. Long) As Long
    23.  
    24. Public Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First"
    25. (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    26.  
    27. Public Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next"
    28. (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    29.  
    30. Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
    31.  
    32. Public Function GetExeFromHandle(hWnd As Long) As String
    33. Dim threadID As Long, processID As Long, hSnapshot As Long
    34. Dim uProcess As PROCESSENTRY32, rProcessFound As Long
    35. Dim i As Integer, szExename As String
    36. ' Get ID for window thread
    37. threadID = GetWindowThreadProcessId(hWnd, processID)
    38. ' Check if valid
    39. If threadID = 0 Or processID = 0 Then Exit Function
    40. ' Create snapshot of current processes
    41. hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    42. ' Check if snapshot is valid
    43. If hSnapshot = -1 Then Exit Function
    44. 'Initialize uProcess with correct size
    45. uProcess.dwSize = Len(uProcess)
    46. 'Start looping through processes
    47. rProcessFound = ProcessFirst(hSnapshot, uProcess)
    48. Do While rProcessFound
    49. If uProcess.th32ProcessID = processID Then
    50. 'Found it, now get name of exefile
    51. i = InStr(1, uProcess.szexeFile, Chr(0))
    52. If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1)
    53. Exit Do
    54. Else
    55. 'Wrong ID, so continue looping
    56. rProcessFound = ProcessNext(hSnapshot, uProcess)
    57. End If
    58. Loop
    59. Call CloseHandle(hSnapshot)
    60. GetExeFromHandle = szExename
    61. End Function
    62.  
    63. Private Sub Command1_Click()
    64. MsgBox GetExeFromHandle(Me.hWnd)
    65. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    15

    Talking Thank You very much for your help n.T.

    *

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    i didn't know you could do that

  5. #5
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    It seemed a wonderful solution, but on my NT4 station 'CreateToolhelp32Snapshot' is not part of kernel32.

    I've seen other VB apps do the same thing on NT4, so it should be possible another way. Can someone please help?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    ??? That doesn't make any sense. kernel32 is kernel 32 is kernal 32. How can one kernal32 support something and not another. Are you getting some kind of error message?

  7. #7
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    It does make sense -NT4 workstation is quite a limited version of Windows.

    I already found a piece of code that runs under NT4:

    Code:
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    
    Public Const GWL_HINSTANCE = -6
    
    Public Function getHwndExe(ByVal myHwnd As Long) As String
        Dim str As String
        Dim hInstance, i As Long
        
        str = String$(254, " ") + Chr(0)
    
        hInstance = GetWindowLong(myHwnd, GWL_HINSTANCE)
        If GetModuleFileName(hInstance, str, 255) Then
            i = InStr(1, str, Chr(0))
            str = LCase$(Left$(str, i - 1))
            getHwndExe = str
            Exit Function
        End If
        getHwndExe = ""
    End Function

  8. #8
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    I checked C:\WINNT\system32\kernel32.dll with Quickview. You can get a list of subroutines inside the dll. The subroutines above can't be found in this list.
    When I check it at my colleagues desktop (Win 2000), he has lots more subroutines in kernel32.

    I heared from a friend that NT is dead, as far as Microsoft is concerned.
    Last edited by hgroot; Jan 29th, 2002 at 10:24 AM.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Originally posted by hgroot
    I heared from a friend that NT is dead, as far as Microsoft is concerned.
    Perhaps the term NT is dead, but NT itself is very much alive. Windows 2000 started out life as NT5, and continues to be the new version of NT, under a different name.

    Your findings are interesting. I will definately make a note of the routine that you posted.

  10. #10
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    You're right - what I meant was NT as in NT4 etc.
    Windows 2000 combines the strength of both '95 and NT.

    I had to port that piece of code from C++.

    Though I now found out that it doesn't work as well with hWnd as with hDC. When I use hWnd I get c:\winnt\system32\msvbvm60.dll
    This is also interesting - Does all compiled VB code run inside a VB Virtual Machine?

    I can only get the real executable name when I call: getHwndExe(Me.hDC)

    It still shouldn't be too hard though to rewrite it for hWnd.
    Last edited by hgroot; Jan 29th, 2002 at 11:30 AM.

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Originally posted by hgroot
    It still shouldn't be too hard though to rewrite it for hWnd.
    Post it when you do.

  12. #12
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    .
    Last edited by hgroot; Jan 30th, 2002 at 05:26 AM.

  13. #13
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52

    NT code

    It has been a while since I last posted on this thread (or posted at all )

    The NT version of the code was more complicated than I thought. I modified some code I found at AllApi.net. Here it is:

    Code:
    Public Function hWnd2EXE(hwnd As Long) As String
        Const clMaxNumProcesses As Long = 5000
        Const MAX_PATH = 260, PROCESS_QUERY_INFORMATION = 1024, PROCESS_VM_READ = 16
        Dim sModuleName As String * MAX_PATH, sProcessNamePath As String, sProcessName As String
        Dim alMatchingNames() As String
        Dim alModules(1 To 400) As Long
        Dim lBytesReturned As Long, lNumMatching As Long
        Dim lNumProcesses As Long, lBytesNeeded As Long, alProcIDs() As Long
        Dim lHwndProcess As Long, lThisProcess As Long, lRet As Long
        Dim sImageName As String
        Dim theHwnd As Long
        
        On Error GoTo ErrFailed
        
        'Size array To hold process IDs
        ReDim alProcIDs(clMaxNumProcesses * 4) As Long
        'Populate an array containing all process ID's
        lRet = EnumProcesses(alProcIDs(1), clMaxNumProcesses * 4, lBytesReturned)
        'Count number of processes returned
        lNumProcesses = lBytesReturned / 4
        'Resize the array containing all the processes
        ReDim Preserve alProcIDs(lNumProcesses)
        'Resize the array To contain all the matching processes
        ReDim alMatchingNames(1 To lNumProcesses)
        
        For lThisProcess = 1 To lNumProcesses
            'Open the process
            lHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, alProcIDs(lThisProcess))
            theHwnd = pid2hWnd(alProcIDs(lThisProcess))
            
            If lHwndProcess <> 0 And theHwnd = hwnd Then
                'Get an array of the module handles For the specified process
                lRet = EnumProcessModules(lHwndProcess, alModules(1), 200&, lBytesNeeded)
                
                If lRet <> 0 Then
                    'Get Process Path And Name
                    lRet = GetModuleFileName(lHwndProcess, alModules(1), sModuleName, MAX_PATH)
                    sProcessNamePath = Trim$(UCase$(Left$(sModuleName, lRet)))
                    'Get the Process Name
                    sProcessName = sProcessNamePath ' Mid$(sProcessNamePath, InStrRev(sProcessNamePath, "\") + 1)
                    'Get the Process Name
                    hWnd2EXE = Mid$(sProcessNamePath, InStrRev(sProcessNamePath, "\") + 1)
                    lRet = CloseHandle(lHwndProcess)
                    Exit Function
                End If
            End If
            'Close the handle To this process
            lRet = CloseHandle(lHwndProcess)
        Next
        
        'No matching processes found
        hWnd2EXE = "unknown"
        Exit Function
    
    ErrFailed:
        hWnd2EXE = "failed"
    End Function

  14. #14
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52

    Arrow

    Oops, almost forgot some other required routines and declarations:

    Code:
    Public Function pid2hWnd(ByVal pid As Long) As Long
        Dim hwCurr As Long
        Dim ShellPid As Long
        Dim GetPid As Long
        Dim aHwnd As Long
        
        aHwnd = getMyHwnd
        
        hwCurr = GetWindow(aHwnd, GW_HWNDFIRST) ' Get first window
        Do While hwCurr ' repeat For all windows
            
            GetWindowThreadProcessId hwCurr, GetPid
            
            If GetPid = pid And IsWindowVisible(hwCurr) Then
                pid2hWnd = hwCurr
                Exit Function
            End If
            
            hwCurr = GetWindow(hwCurr, GW_HWNDNEXT)
        Loop
        pid2hWnd = -1
    End Function
    Well... It's a little hard to extract all the required declarations from my vb module, so you'd have to find them for yourself...

  15. #15
    New Member
    Join Date
    May 2002
    Location
    US
    Posts
    6

    .exe

    i have some stuff that works on win2k and winnt dead system as well.

    More over i have C++ and VB code for that in case anybody needs ...




    don't kill ur thirst for knowledge

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