Task ermitteln und terminieren

GetDesktopWindow, GetWindowThreadProcessId, TerminateProcess
'Verbleibt eine Anwendung nach dem Beenden im Speicher,
'wird sie mit großer Wahrscheinlichkeit mit End beendet.
'Dies hat den Nachteil, daß Dialoge und Objekte nicht
'entladen, Datenbanken nicht geschlossen, Captures
'und Hooks nicht freigegeben werden.
'Der Stop-Button in der VB-IDE hat übrigens die gleiche
'Wirkung wie End.
'Eine Anwendung wird korrekt mit Unload beendet.
'Dieser Source stammt von http://www.activevb.de/
'und kann frei verwendet werden.
'Für eventuelle Schäden wird nicht gehaftet.
Private Declare Function GetDesktopWindow Lib "user32" () _
        As Long
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 GetParent Lib "user32" (ByVal hWnd _
        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 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
Const PROCESS_TERMINATE = &H1
Dim x&  'Anzahl der Prozesse
Private Sub EnumWindows()
Dim hWnd&
  List1(0).Clear
  List1(1).Clear
  List1(2).Clear
 
  hWnd = GetDesktopWindow  'Auch der Desktop ist ein Fenster
  Call GetWindowInfo(hWnd)
 
  hWnd = GetWindow(Me.hWnd, GW_HWNDFIRST)  'Einstieg
 
  Do  'Alle vorhandenen Fenster durchlaufen
    Call GetWindowInfo(hWnd)
    hWnd = GetWindow(hWnd, GW_HWNDNEXT)
  Loop Until hWnd = 0
End Sub
Private Sub GetWindowInfo(ByVal hWnd&)
Dim Parent&, Task&, Result&, Style&, Title$
  Style = GetWindowLong(hWnd, GWL_STYLE)  'Darstellung des Fensters
  Style = Style And (WS_VISIBLE Or WS_BORDER)
         
  Result = GetWindowTextLength(hWnd) + 1
  Title = Space$(Result)  'Title des Fensters auslesen
  Result = GetWindowText(hWnd, Title, Result)
  Title = Left$(Title, Len(Title) - 1)
 
  If (Title <> "" Or (Check1.Value = vbChecked)) And _  'In Abhängigkeit der Optionen die Ausgabe erstellen
     (Style = (WS_VISIBLE Or WS_BORDER) Or Option2.Value) Then
   
    List1(0).AddItem CStr(hWnd)
    List1(1).AddItem Title
   
    Parent = hWnd  'Elternfenster ermitteln
    Do
      Parent = GetParent(Parent)
    Loop Until Parent = 0
   
    Result = GetWindowThreadProcessId(hWnd, Task)  'Task Id ermitteln
    List1(2).AddItem Task
   
    x = x + 1  'Anzahl der Prozesse
  End If
End Sub
Private Sub Option1_Click()
  x = 0
  Call EnumWindows
  Me.Caption = x & " offene Prozesse ermittelt...   "
End Sub
Private Sub Option2_Click()
  x = 0
  Call EnumWindows
  Me.Caption = x & " offene Prozesse ermittelt...   "
End Sub
Private Sub Check1_Click()
  x = 0
  Call EnumWindows
  Me.Caption = x & " offene Prozesse ermittelt...   "
End Sub
Private Sub Command1_Click()   'Refresh
  x = 0
  Call EnumWindows
  Me.Caption = x & " offene Prozesse ermittelt...   "
End Sub
Private Sub Command2_Click()   'Task beenden
Dim Result&, Task&
 
  If List1(2).ListIndex > -1 Then
    Result = CLng(List1(2).List(List1(2).ListIndex))
  
    Task = OpenProcess(PROCESS_TERMINATE, 0&, Result)
    Result = TerminateProcess(Task, 1&)
    Result = CloseHandle(Task)
  End If
End Sub
Private Sub Form_Load()
  x = 0
  Call EnumWindows
  Me.Caption = x & " offene Prozesse ermittelt...   "
End Sub
Private Sub List1_Click(Index As Integer)
Static Flag, y%
  If Flag Then Exit Sub
  Flag = True
  For y = 0 To List1.UBound
    List1(y).ListIndex = List1(Index).ListIndex
    List1(y).TopIndex = List1(Index).TopIndex
  Next y
  Flag = False
End Sub

 Ranking-Hits zurück Sitemap
Designed by www.wbrnet.info