Rubrik: System/Windows · Prozesse/Tasks | VB-Versionen: VB4, VB5, VB6 | 28.04.04 |
EXE-Name anhand eines Fenster-Handles ermitteln II Mit diesem Tipp lässt sich der Pfad+Dateiname einer Anwendung anhand des Fensterhandles ermitteln. | ||
Autor: Dieter Otter | Bewertung: | Views: 17.205 |
www.tools4vb.de | System: Win9x, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Vor einiger Zeit haben wir Ihnen gezeigt, wie man das Fensterhandle einer gestarteten EXE-Datei ermitteln kann:
Handle einer gestarteten EXE-Datei ermitteln II
Unser heutiger Tipp zeigt den umgekehrten Weg: anhand eines Fensterhandles soll der EXE-Name ermittelt werden. Wird der Fensterhandle nicht explizit angegeben, soll die Funktion den EXE-Namen (inkl. Pfadangabe) der aktiven Anwendung ermitteln.
Option Explicit ' benötigte API-Deklarationen Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Declare Function GetWindowThreadProcessId Lib "user32" ( _ ByVal hwnd As Long, _ lpdwProcessId As Long) As Long Private Declare Function OpenProcess Lib "kernel32" ( _ ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Private Declare Function EnumProcessModules Lib "psapi.dll" ( _ ByVal hProcess As Long, _ ByRef lphModule As Long, _ ByVal cb As Long, _ ByRef lpcbNeeded As Long) As Long Private Declare Function GetModuleFileNameEx Lib "psapi.dll" _ Alias "GetModuleFileNameExA" ( _ ByVal hProcess As Long, _ ByVal hModule As Long, _ ByVal lpFilename As String, _ ByVal nSize As Long) As Long Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long) Private Const PROCESS_QUERY_INFORMATION = 1024 Private Const PROCESS_VM_READ = 16
' EXE-Datei anhand eines Fensterhandles ermitteln Public Function GetEXEFromHandle( _ Optional ByVal nHWnd As Long = 0) As String Dim nProcID As Long Dim nResult As Long Dim nTemp As Long Dim lModules(1 To 200) As Long Dim sFile As String Dim hProcess As Long ' Falls kein Fensterhandle angegeben, ' Handle des aktiven Fensters verwenden If nHWnd = 0 Then nHWnd = GetForegroundWindow() ' ProcessID ermitteln If GetWindowThreadProcessId(nHWnd, nProcID) <> 0 Then ' Prozess-Handle ermitteln hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or _ PROCESS_VM_READ, 0, nProcID) If hProcess <> 0 Then ' Module des Prozess enumerieren nResult = EnumProcessModules(hProcess, lModules(1), _ 200, nTemp) If nResult <> 0 Then ' Dateiname ermitteln sFile = Space$(260) nResult = GetModuleFileNameEx(hProcess, 0, _ sFile, Len(sFile)) sFile = LCase$(Left$(sFile, nResult)) GetEXEFromHandle = sFile End If ' Handle schließen CloseHandle hProcess End If End If End Function
Beispiel:
Erstellen Sie ein neues Projekt und fügen obigen Code in ein Modul ein. Platzieren Sie auf die Form eine ListBox, sowie ein Timer-Control. In der ListBox soll bei Programmausführung immer der EXE-Name (inkl. Pfadangabe) der Anwendung angezeigt werden, die gerade aktiv ist.
Option Explicit Private Sub Form_Load() ' Timer initialisieren Timer1.Interval = 250 Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() ' EXE-Name der aktiven Anwendung ermitteln ' und in ListBox übertragen Static sLast As String Dim sFile As String sFile = GetEXEFromHandle() If sFile <> "" And sFile <> sLast Then sLast = sFile List1.AddItem sFile List1.ListIndex = List1.ListCount - 1 End If End Sub