Von Fensterhandle Prozessnamen bekommen

16/12/2007 - 14:56 von Jens Müller | Report spam
Hallo,

wie kann ich von einem Fensterhandle die ProcessID und dann den Namen
der zugehörigen ausführbaren Datei / den dll-Namen des Moduls bekommen?

Danke!
Jens
 

Lesen sie die antworten

#1 Thorsten Doerfler
16/12/2007 - 16:36 | Warnen spam
Jens Müller schrieb:
wie kann ich von einem Fensterhandle die ProcessID und dann den Namen
der zugehörigen ausführbaren Datei / den dll-Namen des Moduls bekommen?



' -- Deklarationen --
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE As Long = -1

Private Declare Function GetCurrentProcessId _
Lib "kernel32.dll" () As Long
Private Declare Function GetWindowThreadProcessId _
Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByRef lpdwProcessId As Long _
) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long _
) As Long

Private Const TH32CS_SNAPPROCESS As Long = &H2

Private Declare Function CreateToolhelp32Snapshot _
Lib "kernel32.dll" ( _
ByVal dwFlags As Long, _
ByVal th32ProcessID As Long _
) As Long

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 * MAX_PATH
End Type

Private Declare Function Process32First Lib "kernel32.dll" ( _
ByVal hSnapshot As Long, _
ByRef lppe As PROCESSENTRY32 _
) As Long
Private Declare Function Process32Next Lib "kernel32.dll" ( _
ByVal hSnapshot As Long, _
ByRef lppe As PROCESSENTRY32 _
) As Long

Public Function GetProcessByHandle(ByVal hWnd As Long) As String
Dim lSnapshot As Long
Dim lPE32 As PROCESSENTRY32
Dim lPID As Long

lSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

If lSnapshot = INVALID_HANDLE_VALUE Then
Debug.Print "CreateToolhelp32Snapshot() fehlgeschlagen"
Exit Function
End If

Call GetWindowThreadProcessId(hWnd, lPID)

If FindProcessEntry(lSnapshot, lPID, lPE32) Then
GetProcessByHandle = Left$(lPE32.szExeFile, _
InStr(1, lPE32.szExeFile, vbNullChar) - 1)
End If

CloseHandle lSnapshot
End Function



Private Function FindProcessEntry( _
ByVal hSnapshot As Long, _
ByVal ProcessID As Long, _
ByRef ProcessEntry As PROCESSENTRY32 _
) As Boolean

ProcessEntry.dwSize = Len(ProcessEntry)

If Not CBool(Process32First(hSnapshot, ProcessEntry)) Then
Debug.Print "Process32First() fehlgeschlagen"
Exit Function
End If

Do
If ProcessEntry.th32ProcessID = ProcessID Then
FindProcessEntry = True
Exit Do
End If

Loop While CBool(Process32Next(hSnapshot, ProcessEntry))

End Function

' -- Anwendung --
Debug.Print GetProcessByHandle(Handle)

'

Den DLL Namen dürftest Du nicht ermitteln können, da diese ja in den
Prozess eingeflochten werden. Windows verwaltet dazu m.W. keine
Information, welche DLL ein Fenster erstellt hat.

Thorsten Dörfler
Microsoft MVP Visual Basic

vb-hellfire visual basic faq | vb-hellfire - einfach anders
http://vb-faq.de/ | http://www.vb-hellfire.de/

Ähnliche fragen