GerhardSchrader
Mitglied
Hallo habe auf einer Seite ein Code gefunden zum Task beenden, aber irgendwie haut das nicht bei mir hin. Erhalte laufend Fehlermeldung Anweisung ausserhalb eines Type-Blogs ungültig. Evtl. hat ja einer ne bessere Lösung, da ich noch nicht so fitt bin in VB. Der Grund warum ich ein Problem habe ist das auf der Seite kein richtiger Zeilenumbruch ist. Erst mal hier die Seite:
http://www.vb-magazin.de/archiv/3451.htm
Ich vermute mal das ich den Zeilenumbruch in VB an irgendeiner Stelle nicht richtig durchgeführt habe. Hier der Code den ich eingegeben habe mit Zeilenumbruch.
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CreateToolHelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
h32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Public I%
'Trage hier deine Exe rein gtrennt mit komma
Const Killlist = "ping.exe, notepad.exe"
End Type
Public Sub ProcessKiller()
Dim hSnapshot As Long, r As Long
PID As Long, q As Long, szExename As String, uProcess As PROCESSENTRY32, killa As String
hSnapshot = CreateToolHelpSnapshot(2&, 0&)
If hSnapshot = 0 Then Exit Sub
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapshot, uProcess)
Do While r
I = InStr(1, uProcess.szExeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szExeFile, I - 1))
PID = uProcess.th32ProcessID
If InStr(1, Killlist, StripFileName(szExename), vbTextCompare) Then KillProcessById
PID r = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
End Sub
Public Sub KillProcessById(p_lngProcessId As Long)
Dim lnghProcess As Long, lngReturn As Long
lnghProcess = OpenProcess(1&, -1&, p_lngProcessId)
lngReturn = TerminateProcess(lnghProcess, 0&)
End Sub
Public Function StripFileName(FilePAth As String) As String
Dim Path As Variant
If InStr(1, FilePAth, "\", vbTextCompare) > 0 Then Path = Split(FilePAth, "\")
StripFileName = Path(UBound(Path))
Else: StripFileName = FilePAth
End If
End Function
Private Sub Form_Load()
ProcessKiller
End Sub
Währe für eure Hilfe dankbar.
Gerhard
http://www.vb-magazin.de/archiv/3451.htm
Ich vermute mal das ich den Zeilenumbruch in VB an irgendeiner Stelle nicht richtig durchgeführt habe. Hier der Code den ich eingegeben habe mit Zeilenumbruch.
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CreateToolHelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
h32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Public I%
'Trage hier deine Exe rein gtrennt mit komma
Const Killlist = "ping.exe, notepad.exe"
End Type
Public Sub ProcessKiller()
Dim hSnapshot As Long, r As Long
PID As Long, q As Long, szExename As String, uProcess As PROCESSENTRY32, killa As String
hSnapshot = CreateToolHelpSnapshot(2&, 0&)
If hSnapshot = 0 Then Exit Sub
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapshot, uProcess)
Do While r
I = InStr(1, uProcess.szExeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szExeFile, I - 1))
PID = uProcess.th32ProcessID
If InStr(1, Killlist, StripFileName(szExename), vbTextCompare) Then KillProcessById
PID r = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
End Sub
Public Sub KillProcessById(p_lngProcessId As Long)
Dim lnghProcess As Long, lngReturn As Long
lnghProcess = OpenProcess(1&, -1&, p_lngProcessId)
lngReturn = TerminateProcess(lnghProcess, 0&)
End Sub
Public Function StripFileName(FilePAth As String) As String
Dim Path As Variant
If InStr(1, FilePAth, "\", vbTextCompare) > 0 Then Path = Split(FilePAth, "\")
StripFileName = Path(UBound(Path))
Else: StripFileName = FilePAth
End If
End Function
Private Sub Form_Load()
ProcessKiller
End Sub
Währe für eure Hilfe dankbar.
Gerhard