Page 1 sur 1

Killer un Process

MessagePublié: 05 Nov 2007 à 17:51
par Michael DELIQUE
Code : Tout sélectionner
Public Function KillProcess_API(Byval wProcessToKill As String) As Integer
   'tuer un process window
%REM
Const NILL = 0&
   Const TH32CS_SNAPPROCESS = &H2&
   Const hNull = 0
   Const WM_SYSCOMMAND = &H112
   Const SC_CLOSE = &HF060
   Const PROCESS_QUERY_INFORMATION = &H400
   Const PROCESS_VM_READ = &H10

Declare Function OpenProcess Lib "kernel32" (Byval dwDesiredAccess As Long, Byval bInheritHandle As Long, Byval dwProcessId As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (Byval hwnd As Long, Byval wMsg As Long, Byval wParam As Long, lParam As Long) As Long
Declare Function GetExitCodeProcess Lib "kernel32" (Byval hProcess As Long, lpExitCode As Long) As Long
Declare Function TerminateProcess Lib "kernel32" (Byval hProcess As Long, Byval uExitCode As Long) As Long
Declare Sub ExitProcess Lib "kernel32" (Byval uExitCode As Long)
Declare Function CloseHandle Lib "kernel32" (Byval hObject As Long) 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
Declare Function Process32First Lib "kernel32" (Byval hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Declare Function Process32Next Lib "kernel32" (Byval hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Declare Function CreateToolhelp32Snapshot Lib "kernel32" (Byval dwFlags As Long, Byval th32ProcessID As Long) As Long
%END REM
   
   'Déclaration Variable
   Dim nbhProc As Long
   Dim nblRet As Long
   Dim Proc As PROCESSENTRY32
   Dim fullstr As Long, string2 As String
   Dim sClean As String
   Dim nbIRETURNCODE As Long
   Dim nbProcFromprocid As Long
   Dim nblpExitCode As Long
   
   On Error Goto ErreurKillProcess_API
   
   nbhProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
   KillProcess_API = False
   
   If nbhProc = 0 Then
      Exit Function
   End If
   
   Proc.dwSize = Len(Proc)
   nblRet = Process32First(nbhProc, Proc)
   
   Do While nblRet
      sClean = Left(Proc.szExeFile, Instr(Proc.szExeFile, Chr(0)) - 1)
      string2 = ""
      For fullstr = Len(sClean) To 1 Step -1
         If Mid(sClean, fullstr, 1) = "\" Then Exit For
         string2 = Mid(sClean, fullstr, 1) & string2
      Next
      If Ucase(string2) = Ucase(wProcessToKill) Then
         KillProcess_API = True
         nbProcFromprocid = OpenProcess(PROCESS_QUERY_INFORMATION Or _
         PROCESS_VM_READ, 0, Proc.th32ProcessID)
         Call GetExitCodeProcess(nbProcFromprocid, nblpExitCode)
         Msgbox("Found " & nbProcfromprocid & " " & nblpexitcode)
         Call TerminateProcess(nbProcFromprocid, nblpExitCode)
         Exit Function
      End If
      
      nblRet = Process32Next(nbhProc, Proc)
   Loop
   
   Exit Function
ErreurKillProcess_API:
   Msgbox "("+Cstr(Getthreadinfo (1))+" Call by "+Cstr(Getthreadinfo(10))+")"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"
   KillProcess_API = False
   Exit Function
End Function