|
| |

VB & Windows API| Re: ProzessID rausfinden und schließen | |  | | Autor: mollte | | Datum: 17.09.07 20:16 |
| Option Explicit
Private Declare Function GetWindow Lib "user32" _
(ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal wIndx As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hWnd As Long, _
ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib _
"user32" (ByVal hWnd As Long, lpdwProcessId As Long) _
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject 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 TerminateProcess Lib "kernel32" (ByVal _
hProcess As Long, ByVal uExitCode As Long) As Long
Const PROCESS_TERMINATE = &H1
Public lTaskID As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5
Const GW_MAX = 5
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Public Function GetWindowList(oLSV As ListView, _
Optional bVisible As Boolean = True)
' oLSV - Ein beliebiges ListView, das die
' Informationen aufnehmen soll
' bVisible - Wenn True werden nur sichtbare Windows
' angezeigt, ansonsten alles
Dim hWnd As Long
Dim sTitle As String
Dim lTaskID As Long
Dim lStyle As Long
Dim oListItem As ListItem
' ListView-Spalten festlegen
With oLSV.ColumnHeaders
.Clear
.Add , "hWnd", "hWnd"
.Add , "TaskID", "TaskID"
.Add , "Visible", "Visible"
.Add , "Title", "Title"
End With
' Inhalt löschen und umschalten auf "Detail"-Ansicht
oLSV.ListItems.Clear
oLSV.View = lvwReport
' Erstes WindowHandle holen
hWnd = GetWindow(oLSV.Parent.hWnd, GW_HWNDFIRST)
Do
' Handle auswerten und im ListView eintragen
sTitle = GetWindowTitle(hWnd)
If InStr(1, sTitle, "Unbenannt", _
vbTextCompare) > 0 Then
lTaskID = GetWindowTaskID(hWnd)
lStyle = GetWindowLong(hWnd, GWL_STYLE)
lStyle = lStyle And (WS_VISIBLE Or WS_BORDER)
sTitle = GetWindowTitle(hWnd)
If (lStyle = (WS_VISIBLE Or WS_BORDER)) = bVisible Then
' in das ListView eintragen
Set oListItem = oLSV.ListItems.Add(, , hWnd)
With oListItem
.SubItems(1) = lTaskID
.SubItems(2) = lStyle = (WS_VISIBLE Or WS_BORDER)
.SubItems(3) = sTitle
End With
End If
End If
' Nächses Handle
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop Until hWnd = 0
End Function
Private Function GetWindowTitle(ByVal hWnd As Long) _
As String
' Ermittelt den Namen eines Windows anhand des
' Window Handle
Dim lResult As Long
Dim sTemp As String
lResult = GetWindowTextLength(hWnd) + 1
sTemp = Space(lResult)
lResult = GetWindowText(hWnd, sTemp, lResult)
GetWindowTitle = Left(sTemp, Len(sTemp) - 1)
End Function
Private Function GetWindowTaskID(ByVal hWnd As Long) _
As Long
' Ermittelt die TaskID eines Windows anhand des
' Window Handle
Dim lResult As Long
Dim lTemp As Long
lResult = GetWindowThreadProcessId(hWnd, lTemp)
GetWindowTaskID = lTemp
End Function
Public Function WordIsRunning(oForm As Form) As Boolean
' Gibt True zurück, wenn Word geladen ist
Dim hWnd As Long
Dim sTitle As String
Dim lStyle As Long
WordIsRunning = False ' Ich alter Pessimist
' Erstes WindowHandle holen
hWnd = GetWindow(oForm.hWnd, GW_HWNDFIRST)
Do
' Handle auswerten
lStyle = GetWindowLong(hWnd, GWL_STYLE)
lStyle = lStyle And (WS_VISIBLE Or WS_BORDER)
' Titelzeile ermitteln und prüfen, ob
' das "Microsoft Word" darin enthalten ist
If (lStyle = (WS_VISIBLE Or WS_BORDER)) = True Then
sTitle = GetWindowTitle(hWnd)
If InStr(1, sTitle, "Microsoft Word", _
vbTextCompare) > 0 Then
WordIsRunning = True
Exit Do
End If
End If
' Nächses Handle
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop Until hWnd = 0
End Function |  |
 | Sie sind nicht angemeldet! Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.
Einloggen | Neu registrieren |
  |
|
Neu! sevPopUp 2.0 
Dynamische Kontextmenüs!
Erstellen Sie mit nur wenigen Zeilen Code Kontextmenüs dynamisch zur Laufzeit. Vordefinierte Styles (XP, Office, OfficeXP, Vista oder Windows 8) erleichtern die Anpassung an die eigenen Anwendung... Weitere InfosTipp des Monats Access-Tools Vol.1 
Über 400 MByte Inhalt
Mehr als 250 Access-Beispiele, 25 Add-Ins und ActiveX-Komponenten, 16 VB-Projekt inkl. Source, mehr als 320 Tipps & Tricks für Access und VB
Nur 24,95 EURWeitere Infos
|