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 71 72 73 74 75 76 77 78 79 80 81 82 83
| Option Explicit
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Declare PtrSafe Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Declare PtrSafe Function CloseHandle Lib "Kernel32.dll" (ByVal hObject As Long) As Long
Declare PtrSafe Function TerminateProcess Lib "Kernel32.dll" (ByVal hProcess As Long, ByVal dwExitCode As Long) As Long
Const PROCESS_TERMINATE As Long = &H1
Sub FermerProcessAvecFenetreInvisible(sNomFenetre As String)
'---------------------------------------------------------------------------------------
' Procedure : FermerProcessAvecFenetreInvisible
' Date : 16/04/2012
' Purpose : Ferme les processus comportant des fenêtres cachées (testé sur XP et Win7 32bits et 64)
' Argument : une partie du nom de la fenêtre (minuscule ou majuscule indifférent)
'---------------------------------------------------------------------------------------
Dim titre As String
Dim hWnd As Long
Dim pid As Long
Dim i As Long
Dim colPid As Collection
Set colPid = New Collection
'1ère fenêtre
hWnd = FindWindow(0, 0)
Do While hWnd <> 0
If IsWindowVisible(hWnd) = 0 Then
'Si n'est pas visible => récupération du nom de la fenêtre
titre = String(100, Chr$(0))
GetWindowText hWnd, titre, 100
titre = Left$(titre, InStr(titre, Chr$(0)) - 1)
If LCase(titre) Like "*" & LCase(sNomFenetre) & "*" Then
'Fenêtre trouvée
'Récupération de l'ID du processus (pid) lié à la fenêtre
GetWindowThreadProcessId hWnd, pid
'Ajout du pid dans la collection
colPid.Add pid
End If
End If
hWnd = GetWindow(hWnd, 2) 'Prochaine fenêtre
Loop
'Fermeture de tous les processus stockés dans la collection
For i = 1 To colPid.Count
KillProcess colPid(i)
Next i
Set colPid = Nothing
End Sub
Sub KillProcess(pid As Long)
'Fermeture du processus
Dim hProc As Long
Dim Retval As Long
hProc = OpenProcess(PROCESS_TERMINATE, 0, pid)
If hProc <> 0 Then
Retval = TerminateProcess(hProc, 0)
' Si Retval = 0 échec de la fonction TerminateProcess(..)
CloseHandle hProc
End If
End Sub
Sub TesterFermeture()
'Autre exemple
FermerProcessAvecFenetreInvisible "excel"
' 'Nécessite la référence Microsoft Word x.0 Object Library
' Dim oAppWord As Word.Application, oAppWord2 As Word.Application
' Set oAppWord = CreateObject("Word.Application")
' Set oAppWord2 = CreateObject("Word.Application")
'
' oAppWord2.Visible = True 'une instance visible
' oAppWord.Visible = False 'une instance invisible
'
' MsgBox "Ouvrez le gestionnaire des tâches, une fois cliqué sur 'OK' les instances Word invisibles doivent disparaitre"
' FermerProcessAvecFenetreInvisible "microsoft word"
End Sub |
Partager