1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
Type strucPROCESSENTRY32
dwSize As Long ' DWORD : Size of the structure, in bytes
cntUsage As Long ' DWORD : not used (0)
th32ProcessID As Long ' DWORD : PID
th32DefaultHeapID As Long ' ULONG_PTR : not used (0)
th32ModuleID As Long ' DWORD : not used (0)
cntThreads As Long ' DWORD : Threads
th32ParentProcessID As Long ' DWORD : parent process ID
pcPriClassBase As Long ' LONG
dwFlags As Long ' LONG : not longer used (0)
szExeFile As String * 512 ' TCHAR szExeFile[MAX_PATH] name of the executable file for the process
End Type
Const TH32CS_SNAPPROCESS As Long = &H2
Const PROCESS_TERMINATE As Long = &H1
Const PROCESS_QUERY_INFORMATION As Long = &H400
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, ByRef lppe As strucPROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32.dll" _
(ByVal hSnapshot As Long, ByRef lppe As strucPROCESSENTRY32) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "Kernel32.dll" _
(ByVal hProcess As Long, ByVal dwExitCode As Long) As Long
' _____________________________________________________
Sub TermProcess(strProcessName As String)
Dim hSnapshot As Long, lppe As strucPROCESSENTRY32
Dim hProc As Long, Retval As Long, strPrcsName As String
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
lppe.dwSize = Len(lppe)
Retval = Process32First(hSnapshot, lppe)
Do While Retval
strPrcsName = Left(lppe.szExeFile, InStr(1, lppe.szExeFile, vbNullChar) - 1)
' Debug.Print strPrcsName, lppe.th32ProcessID
If strPrcsName Like strProcessName Then
hProc = OpenProcess(PROCESS_TERMINATE, 0, lppe.th32ProcessID)
If hProc <> 0 Then
Retval = TerminateProcess(hProc, 0)
' Si Retval=0 échec de la fonction TerminateProcess(..)
Call CloseHandle(hProc)
End If
End If
' Process Suivant
Retval = Process32Next(hSnapshot, lppe)
Loop
Call CloseHandle(hSnapshot)
End Sub
' _____________________________________________________
Sub Test()
TermProcess "EXCEL.EXE"
End Sub |
Partager