Rubrik: Windows/System | 27.07.01 |
CreateToolhelp32Snapshot-Funktion Erstellt einen "Schnappschuss" von den System-Threads, Prozessen, Modulen oder Heaplisten und gibt ein entsprechendes Handle zurück. | ||
Betriebssystem: Win95, Win98, WinNT 4.0, Win2000, WinME | Views: 14.217 |
Deklaration:
Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" ( _ ByVal dwFlags As Long, _ ByVal th32ProcessID As Long) As Long
Beschreibung:
Erstellt einen "Schnappschuss" von den System-Threads, Prozessen, Modulen oder Heaplisten und gibt ein entsprechendes Handle zurück. Um die Ressourcen wieder frei zu geben, rufen Sie die CloseHandle-Funktion auf.
Parameter:
dwFlags | Erwartet eine oder eine Kombination von mehreren "dwFlags"-Konstanten, um die Eigenschaften des Snapshots festzulegen. |
th32ProcessID | Erwartet eine Prozess-ID, wenn nach Heaplisten oder Modulen gesucht werden soll. |
dwFlags Konstanten:
' Erzeugt einen vererbbaren Snapshot Const TH32CS_INHERIT = &H80000000 ' Erzeugt einen Snapshot aller Ressourcen Const TH32CS_SNAPALL = &HF ' Erzeugt zusätzlich einen Snapshot von Heaplisten Const TH32CS_SNAPHEAPLIST = &H1 ' Erzeugt zusätzlich einen Snapshot von Prozessen Const TH32CS_SNAPPROCESS = &H2 ' Erzeugt zusätzlich einen Snapshot von Threads Const TH32CS_SNAPTHREAD = &H4 ' Erzeugt zusätzlich einen Snapshot von Modulen Const TH32CS_SNAPMODULE = &H8
Rückgabewert:
Bei erfolgreichem Funktionsaufruf wird ein Handle zu dem erstelltem Snapshot zurückgegeben, andernfalls ist der Rückgabewert "-1". Für erweiterte Fehlerinformationen rufen Sie die GetLastError-Funktion auf.
Beispiel:
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, _ lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32.dll" ( _ ByVal hSnapshot As Long, _ lppe As PROCESSENTRY32) As Long Private Declare Function CloseHandle Lib "kernel32.dll" ( _ ByVal hObject As Long) As Long Private Declare Function lstrlen Lib "kernel32" _ Alias "lstrlenA" ( _ ByVal lpString As String) As Long 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 * 260 End Type Private Const TH32CS_INHERIT = &H80000000 Private Const TH32CS_SNAPALL = &HF Private Const TH32CS_SNAPHEAPLIST = &H1 Private Const TH32CS_SNAPPROCESS = &H2 Private Const TH32CS_SNAPTHREAD = &H4 Private Const TH32CS_SNAPMODULE = &H8
' alle laufenden Prozesse ermitteln Private Sub Command1_Click() Dim Retval As Long Dim hSnap As Long Dim PInfo As PROCESSENTRY32 ' Snapshot vom gesamten System erstellen hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) If hSnap = -1 Then MsgBox "Der System-Snapshot konnte nicht erstellt werden.", _ vbInformation, "Fehler" Exit Sub End If PInfo.dwSize = Len(PInfo) Retval = Process32First(hSnap, PInfo) ' ersten Prozess ermitteln List1.Clear Do Until Retval = 0 With PInfo .szExeFile = Trim$(Left$(.szExeFile, lstrlen(.szExeFile))) _ ' VBNullChar abtrennen List1.AddItem "# " & Format$(Abs(.th32ProcessID), "00000000") & " - _ " & Mid$(.szExeFile, InStrRev(.szExeFile, "\") + 1) End With Retval = Process32Next(hSnap, PInfo) ' nächsten Prozess ermitteln DoEvents Loop CloseHandle hSnap ' Snapshot zerstören End Sub