Rubrik: System/Windows · Prozesse/Tasks | VB-Versionen: VB4, VB5, VB6 | 24.07.02 |
Prüfen, ob eine EXE-Datei ausgeführt wird Mit dieser Funktion lässt anhand des Dateinamens prüfen, ob eine Anwendung bereits ausgeführt wird. | ||
Autor: Dieter Otter | Bewertung: | Views: 35.231 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Ist der Internet-Explorer bereits gestartet? Läuft Microsoft Word irgendwo im Hintergrund? Diese Frage beantwortet Ihnen in Zukunft die Funktion IsEXERunning.
Übergeben Sie der Funktion einfach den Dateinamen der zu prüfenden Anwendung. Der Rückgabewert ist dann entweder True (die Anwendung ist gestartet) oder False.
' zunächst die benötigten API-Deklarationen Private Declare Function CreateToolhelpSnapshot Lib "Kernel32" _ Alias "CreateToolhelp32Snapshot" ( _ ByVal lFlgas As Long, _ ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "Kernel32" _ Alias "Process32First" ( _ ByVal hSnapshot As Long, _ uProcess As PROCESSENTRY32) As Long Private Declare Function ProcessNext Lib "Kernel32" _ Alias "Process32Next" ( _ ByVal hSnapshot As Long, _ uProcess As PROCESSENTRY32) As Long Private Declare Sub CloseHandle Lib "Kernel32" ( _ ByVal hPass As Long) Private Const TH32CS_SNAPPROCESS As Long = 2& Private Const MAX_PATH As Long = 260 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
' Prüft, ob eine EXE-Datei bereits ausgeführt wird Private Function IsEXERunning(ByVal sFilename As String) As Long Dim lSnapshot As Long Dim uProcess As PROCESSENTRY32 Dim nResult As Long ' "Snapshot" des aktuellen Prozess ermitteln lSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) If lSnapshot <> 0 Then uProcess.dwSize = Len(uProcess) ' Ersten Prozess ermitteln nResult = ProcessFirst(lSnapshot, uProcess) Do Until nResult = 0 ' Prozessliste durchlaufen If InStr(LCase$(uProcess.szexeFile), LCase$(sFilename)) > 0 Then ' Jepp - EXE gefunden IsEXERunning = True Exit Do End If ' nächster Prozess nResult = ProcessNext(lSnapshot, uProcess) Loop ' Handle schliessen CloseHandle lSnapshot End If End Function
Beispiel:
Um zu prüfen, ob eine Instanz des Windows-Explorers ausgeführt wird, folgenden Abfrage verwenden:
If IsEXERunning("explorer.exe") Then MsgBox "Jepp - Explorer ist gestartet." End If
Oder... wird WinWord ausgeführt?
If IsEXERunning("winword.exe") Then MsgBox "Jepp - auch WinWord ist gestartet." End If