[VBA-E] Tuer processus Excel cachés sous Win7
Bonjour à tous,
Sur la base du code suivant que j'ai trouvé sur ce post résolu de 2007 :http://www.developpez.net/forums/d27...s/#post6615539
serait-il possible d'adapter cette fonction pour qu'elle ne supprime que des processus Office invisibles à l'utilisateur ?
Voici la fonction en question :
Code:
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 67 68 69 70
| Option Explicit
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
Dim lppe As strucPROCESSENTRY32
Dim hProc As Long
Dim Retval As Long
Dim 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
Private Sub test()
TermProcess "EXCEL.EXE"
End Sub
Function KillProcess(pLng_ProcessId As Long)
Dim hProc As Long
Dim Retval As Long
hProc = OpenProcess(PROCESS_TERMINATE, 0, pLng_ProcessId)
If hProc <> 0 Then
Retval = TerminateProcess(hProc, 0)
' Si Retval=0 échec de la fonction TerminateProcess(..)
Call CloseHandle(hProc)
End If
End Function |
Actuellement lorsqu'on appelle cette fonction TermProcess "EXCEL.EXE" elle supprime toutes les instances d'excel sans exception. (Même l'instance Excel qui appelle elle-même la fonction se ferme).
Pour la petite histoire, j'avais un code du même style qui permettait de fermer des instances d'Excel cachées, mais le code ne fonctionne plus sur Windows 7.
Voilà, est ce qu'il y aurait des personnes qui pourrait m'aider à arriver à mes fins ? (l'avantage de la fonction TermProcess c'est qu'elle fonctionne aussi bien sur XP que Seven 32 et 64bits, mais peut être il y a-t-il une autre solution plus simple)
Merci d'avance pour votre aide précieuse :ccool: