| |
VB & Windows APIRe: 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 |
|
|
vb@rchiv CD Vol.6 vb@rchiv Vol.6
Geballtes Wissen aus mehr als 8 Jahren vb@rchiv!
Online-Update-Funktion Entwickler-Vollversionen u.v.m.Jetzt zugreifen Tipp des Monats TOP! Unser Nr. 1
Neu! sevDataGrid 3.0
Mehrspaltige Listen, mit oder ohne DB-Anbindung. Autom. Sortierung, Editieren von Spalteninhalten oder das interaktive Hinzufügen von Datenzeilen sind ebenso möglich wie das Erstellen eines Web-Reports. Weitere Infos
|
|
|
Copyright ©2000-2024 vb@rchiv Dieter Otter Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.
Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel
|
|