Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
624to628
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
624to628
624to628
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Prüfen, ob ein Programm läuft

Prüfen, ob ein Programm läuft
21.06.2005 14:26:07
marcl
Hallo Forum,
ich habe ein Makro geschrieben, welches eine html Seite öffnet. Nun kommt es vor, dass die Seite schon einmal offen ist und ein 2. Mal geöffnet wird, oder eine Fehlermeldung kommt.
Ich möchte gerne:
If Seite offen Then Exit Sub
oder so was in der Art.
Ist das möglich?
Gruß
marcl

8
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Prüfen, ob ein Programm läuft
21.06.2005 15:52:41
Nepumuk
Hi,
die Datei wird, wie ich annehme im Explorer geöffnet. Welcher Explorer? Beispiele: Opera, Netscape, Firefox, Internetexplorer .....
Hast du ein bisschen Ahnung von API?
Gruß
Nepumuk
AW: Prüfen, ob ein Programm läuft
21.06.2005 17:15:47
marcl
Nein.
Läuft im Internetexplorer. Habe auch noch ein anderes Programm (hlp.exe). Ob das läuft, muss ich auch wissen.
Vielen Dank.
Gruß
marcl
AW: Prüfen, ob ein Programm läuft
21.06.2005 18:01:12
Nepumuk
Hi,
das sind aber zwei paar Stiefel. Beim Explorer musst du nach dem Festertitel in der Titelleiste suchen, bei hlp.exe muss du nach dem Prozess suchen.
1. Explorerfenster mit einem bestimmten Titel suchen:
Option Explicit

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32.dll" ( _
    ByVal hwnd As Long, _
    ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" ( _
    ByVal hwnd As Long, _
    ByVal nIndex As Long) As Long
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" ( _
    ByVal hwnd As Long, _
    ByVal lpClassName As String, _
    ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" ( _
    ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" ( _
    ByVal hwnd As Long, _
    ByVal lpString As String, _
    ByVal cch As Long) As Long

Private Const MAX_COUNT = 256&

Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2

Private Const WS_VISIBLE = &H10000000
Private Const WS_BORDER = &H800000

Private Const GWL_STYLE = -16

Private Const GC_CLASSNAMEMSIEXPLORER = "IEFrame"

Private Function fncSearchWindow(strWindowtitle As String) As Boolean
    Dim intIndex As Integer
    Dim lnghWnd As Long, lngStyle As Long, lngReturn As Long
    Dim strClassName As String, strClass As String
    strClassName = Space$(MAX_COUNT)
    lnghWnd = GetWindow(FindWindow(vbNullString, vbNullString), _
        GW_HWNDFIRST)
    Do
        lngStyle = GetWindowLong(lnghWnd, GWL_STYLE) And _
            (WS_VISIBLE Or WS_BORDER)
        If lngStyle = (WS_VISIBLE Or WS_BORDER) Then
            lngReturn = GetClassName(lnghWnd, strClassName, _
                MAX_COUNT)
            strClass = Left$(strClassName, lngReturn)
            If strClass = GC_CLASSNAMEMSIEXPLORER Then
                If Trim$(fncGetWindowTitle(lnghWnd)) = strWindowtitle Then
                    fncSearchWindow = True
                    Exit Function
                End If
            End If
        End If
        lnghWnd = GetWindow(lnghWnd, GW_HWNDNEXT)
    Loop Until lnghWnd = 0
End Function

Private Function fncGetWindowTitle(ByVal lnghWnd As Long) As String
    Dim lngReturn As Long, strTemp As String
    lngReturn = GetWindowTextLength(lnghWnd) + 1
    strTemp = Space$(lngReturn)
    GetWindowText lnghWnd, strTemp, lngReturn
    fncGetWindowTitle = Left$(strTemp, Len(strTemp) - 1)
End Function

Public Sub test()
    If Not fncSearchWindow("Dein Fenstertitel") Then 'anpassen !!!
        ' Deine Datei öffnen
    End If
End Sub

2. Prüfen, ob ein bestimmter Prozess läuft:
Option Explicit

Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" ( _
    ByVal dwFlags As Long, _
    ByVal th32ProcessID As Long) As Long
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
Private Declare Function CloseHandle Lib "kernel32.dll" ( _
    ByVal hObject As Long) As Long

Private Const TH32CS_SNAPPROCESS = &H2

Private Const MAX_PATH = 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

Private Function fncSearchProcess(strProcessname As String) As Boolean
    Dim Snap As Long, Process As PROCESSENTRY32, Result As Long
    Snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If Snap <> -1 Then
        Process.dwSize = Len(Process)
        Result = Process32First(Snap, Process)
        Do Until Result = 0
            If InStr(1, LCase$(Process.szExeFile), LCase$(strProcessname)) Then
                fncSearchProcess = True
                Exit Function
            End If
            Result = Process32Next(Snap, Process)
        Loop
    End If
    CloseHandle Snap
End Function

Public Sub test()
    If Not fncSearchProcess("hlp.exe") Then
        MsgBox "hlp.exe läuft nicht"
    End If
End Sub

Gruß
Nepumuk
Anzeige
AW: Prüfen, ob ein Programm läuft
22.06.2005 10:29:13
marcl
Vielen Dank Nepumuk,
leider habe ich einen Fehler in folgernder Zeile:
Snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
' Laufzeitfehler ' 453 : DLL Einsprungspunkt... in kernel32.dll nicht gefunden
gruß
marcl
AW: Prüfen, ob ein Programm läuft
22.06.2005 10:32:14
Nepumuk
Hi,
welches Betriebssystem hast du?
Gruß
Nepumuk
AW: Prüfen, ob ein Programm läuft
22.06.2005 10:52:21
marcl
Ups,
noch haben wir Windows NT. Habe vergessen, dass wir in einem Netzwerk arbeiten.
Demnächst solls Windows XP werden.
Gruß
marcl
AW: Prüfen, ob ein Programm läuft
22.06.2005 10:58:54
Nepumuk
Hi,
dachte ich mir. Da kannst du den Code nicht benutzen. Unter XP funktioniert er dann. In der Zwischenzeit kannst du diesen Code verwenden:
Sub test1()
    Dim objWMI As Object, colPro As Object
    Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
    Set colPro = objWMI.ExecQuery("Select * from Win32_Process Where Name = 'hlp.exe'")
    If colPro.Count = 0 Then
        MsgBox "hlp.exe läuft nicht"
    Else
        MsgBox "hlp.exe läuft"
    End If
End Sub

Der ist halt ein bisschen langsamer.
Gruß
Nepumuk
Anzeige
Danke o.T.
22.06.2005 11:30:18
marcl
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige
Archiv - Verwandte Themen
Forumthread
Beiträge