How to Retrieve the FilePath from process identified using a hwnd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'System & API - How to Retrieve the FilePath from process identified using a hwnd
Option Explicit
 
Private Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * 260
End Type
 
Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type
 
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 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 GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
    (lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, _
    lpdwProcessId As Long) As Long
 
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const TH32CS_SNAPPROCESS = &H2
 
Private Function CheckVersion() As Long
    Dim tOS As OSVERSIONINFO
    tOS.dwOSVersionInfoSize = Len(tOS)
    Call GetVersionEx(tOS)
    CheckVersion = tOS.dwPlatformId
End Function
 
Public Function GetHwndEXE(ByVal hWnd As Long) As String
    Dim lProcessID As Long, lThread As Long, lSnapShot As Long
    Dim lProcessHandle As Long, sName As String, lModule As Long
    Dim bMore As Boolean, tPROCESS As PROCESSENTRY32
 
    lThread = GetWindowThreadProcessId(hWnd, lProcessID)
 
    If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
        'Windows 9x
        'Create a SnapShot of the Currently Running Processes
        lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
        If lSnapShot < 0 Then Exit Function
 
        tPROCESS.dwSize = Len(tPROCESS)
 
        'Enumerate those processes until we find a match
        bMore = Process32First(lSnapShot, tPROCESS)
        While bMore And tPROCESS.th32ProcessID <> lProcessID
            bMore = Process32Next(lSnapShot, tPROCESS)
        Wend
 
        'If a match was found, get the EXE Path and Filename
        If tPROCESS.th32ProcessID = lProcessID Then
            sName = Left$(tPROCESS.szExeFile, InStr(tPROCESS.szExeFile,  chr ( 0 ) ) - 1)
            GetHwndEXE = sName
        End If
 
    Else
        'Win NT
        'Create an Instance of the Process
        lProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0&, lProcessID)
 
        'If the Process was successfully created, get the EXE
        If lProcessHandle Then
            'Just get the First Module, all we need is the Handle to get the Filename..
            If EnumProcessModules(lProcessHandle, lModule, 4, 0&) Then
                sName = Space(260)
                Call GetModuleFileNameExA(lProcessHandle, lModule, sName, Len(sName))
                GetHwndEXE = sName
            End If
            'Close the Process Handle
            Call CloseHandle(lProcessHandle)
        End If
    End If
End Function
 
'How can I use this function:
Private Sub Command1_Click()
    strFilePath = GetHwndEXE(Me.hWnd)
End Sub

Tags: , , , , , , , , , , , , , ,

Leave a Reply