Option Explicit Private Declare Function TerminateProcess Lib "kernel32" (ByVal monprocessid As Long, ByVal uExitCode As Long) As Long 'API de fermeture de Process Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwmonappid As Long) As Long 'Ouverture de Process Private monappid As Long Private Sub ouvrir_Click() monappid = Shell("notepad.exe d:\essai.txt", 1) ' mettre un fichier existant chez toi End Sub Private Sub fermer_Click() Dim monprocessid As Long monprocessid = OpenProcess(1, False, monappid) TerminateProcess monprocessid, 4 End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Const MAX_PATH = 260 Private Const INVALID_HANDLE_VALUE As Long = -1 Private Const TH32CS_SNAPPROCESS As Long = &H2 Private Const PROCESS_TERMINATE As Long = &H1 Private Const PROCESS_VM_READ As Long = &H10 Private Const PROCESS_QUERY_INFORMATION As Long = &H400 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 * MAX_PATH End Type Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long Private Declare Function Process32First Lib "kernel32.dll" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32.dll" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) 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 EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef lpcbNeeded As Long) As Long Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExW" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As Long, ByVal nSize As Long) As Long Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Function KillProcessByPath(ByVal Path As String) As Boolean Dim ok As Boolean Dim hSnapshot As Long Dim ppe As PROCESSENTRY32 Dim hProcess As Long Dim hModule As Long Dim buffer As String Dim bufferSize As Long Path = VBA.StrConv(Path, vbLowerCase) ppe.dwSize = Len(ppe) buffer = VBA.String$(MAX_PATH, VBA.Chr$(0&)) hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) If Not hSnapshot = INVALID_HANDLE_VALUE Then If Process32First(hSnapshot, ppe) Then Do hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ Or PROCESS_TERMINATE, 0&, ppe.th32ProcessID) If hProcess = 840 Then Stop If Not hProcess = 0& Then If Not EnumProcessModules(hProcess, hModule, 0&, 0&) = 0& Then bufferSize = GetModuleFileNameEx(hProcess, hModule, StrPtr(buffer), MAX_PATH) If bufferSize > 0& Then buffer = VBA.StrConv(buffer, vbLowerCase) ok VBA.Left$(buffer, bufferSize) Path If ok Then KillProcessByPath Not TerminateProcess(hProcess, 0&) 0& End If End If End If Call CloseHandle(hProcess) End If Loop While Not ok And Process32Next(hSnapshot, ppe) End If Call CloseHandle(hSnapshot) End If End Function
'------------------------------------------------------------------------------------'
' '
' Load et Unload de Process '
' '
'------------------------------------------------------------------------------------'
' Call AppelApplication("Openw", "C:\Directorie\Projet.exe") ' Exécuter un Projet '
' Call AppelApplication("Close" ' Pour arrêter le Projet'
'------------------------------------------------------------------------------------'
Function AppelApplication(Parm As String, Optional Application As String)
Static Hprocess As Long
Static Codret As Long
Static ProcessId As Long
Dim ZCommand As String
Dim J1 As Integer
' On regarde si l'on passe un paramètre au programme
ZCommand = ""
J1 = InStr(LCase(Application), ".exe")
If J1 > 0 Then
If Len(Application) > J1 + 4 Then
ZCommand = Mid(Application, J1 + 5)
Application = Mid(Application, 1, J1 + 3)
End If
End If
' 1) Open Application ("Open OpenW") ------------------------
If Parm "Open" Or Parm "OpenW" Then
' On affiche un Fichier (OpenW ne marche Pas ! )
If Application <> COMPILATEUR And InStr(LCase(Application), ".exe") = 0 Then
Call ShellExecuteA(0, "", Application, "", "", 1)
Exit Function
End If
' On exécute un .EXE
ProcessId = Shell(Application & " " & ZCommand, 1)
Hprocess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
AppelApplication = Hprocess
' Wait tant que le process appelé est actif
If Parm = "OpenW" Then
Do
Call GetExitCodeProcess(Hprocess, Codret)
DoEvents
Loop While Codret = STATUS_PENDING
End If
Exit Function
End If
' 2) Close Application ------------------------
Hprocess = OpenProcess(1, False, ProcessId) <---------------------------
Call TerminateProcess(Hprocess, Codret)
Call CloseHandle(Hprocess)
End Function
Option Explicit 'Flags ShellExecuteEx Private Const SEE_MASK_NOCLOSEPROCESS = &H40 Private Const SEE_MASK_FLAG_NO_UI = &H400 'Constantes ERREUR ShellExecuteEx Private Const SE_ERR_FNF As Byte = 2 Private Const SE_ERR_PNF As Byte = 3 Private Const SE_ERR_ACCESSDENIED As Byte = 5 Private Const SE_ERR_OOM As Byte = 8 Private Const SE_ERR_SHARE As Byte = 26 Private Const SE_ERR_ASSOCINCOMPLETE As Byte = 27 Private Const SE_ERR_DDETIMEOUT As Byte = 28 Private Const SE_ERR_DDEFAIL As Byte = 29 Private Const SE_ERR_DDEBUSY As Byte = 30 Private Const SE_ERR_NOASSOC As Byte = 31 Private Const SE_ERR_DLLNOTFOUND As Byte = 32 'Constantes AFFICHAGE ShellExecuteEx Private Const SW_SHOWNORMAL = 1 Private Const SW_SHOW = 5 Private Const SW_SHOWDEFAULT = 10 Private Type SHELLEXECUTEINFO cbSize As Long fMask As Long hWnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End Type Private Declare Function ShellExecuteEx Lib "shell32.dll" (SEI As SHELLEXECUTEINFO) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private toto As Long ' *********************************************************** ' * ' * Lance le programme par défaut associé à un fichier (en fonction de son ' * extension ) et retourne le hWnd de la fênetre du programme lançé. ' * ' *********************************************************** Public Function OpenProgram(ByRef Filename As String, ByRef OwnerhWnd As Long) As Long Dim SEI As SHELLEXECUTEINFO On Error GoTo ErrorHandler With SEI .cbSize = Len(SEI) .fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_NO_UI .hWnd = OwnerhWnd .lpVerb = "open" .lpFile = Filename .lpParameters = vbNullChar .lpDirectory = vbNullChar .nShow = SW_SHOW .hInstApp = OwnerhWnd End With OpenProgram = ShellExecuteEx(SEI) If SEI.hInstApp <= 32 Then 'Erreurs OpenProgram = 0 Select Case SEI.hInstApp Case SE_ERR_FNF OpenProgram = SEI.hProcess Case SE_ERR_PNF MsgBox "Le chemin du fichier à ouvrir est incorrect.", vbExclamation Case SE_ERR_ACCESSDENIED MsgBox "Accès au fichier refusé.", vbExclamation Case SE_ERR_OOM MsgBox "Mémoire insuffisante.", vbExclamation Case SE_ERR_DLLNOTFOUND MsgBox "Dynamic-link library non trouvé.", vbExclamation Case SE_ERR_SHARE MsgBox "Le fichier est déjà ouvert.", vbExclamation Case SE_ERR_ASSOCINCOMPLETE MsgBox "Information d'association du fichier incomplète.", vbExclamation Case SE_ERR_DDETIMEOUT MsgBox "Opération DDE dépassée.", vbExclamation Case SE_ERR_DDEFAIL MsgBox "Opération DDE echouée.", vbExclamation Case SE_ERR_DDEBUSY MsgBox "Opération DDE occupée.", vbExclamation Case SE_ERR_NOASSOC 'Ouvrir avec... Call Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & Filename, vbNormalFocus) End Select Else 'Retourne le hWnd du programme lançé par ShellExecuteEx OpenProgram = SEI.hProcess toto = OpenProgram End If Exit Function ErrorHandler: OpenProgram = 0 End Function ' *********************************************************** ' * ' * Ferme un programme à partir du hWnd de sa fenêtre. ' * ' *********************************************************** Public Function CloseProgram(hWnd As Long) As Boolean Dim lExitCode As Long If hWnd = 0 Then Exit Function End If CloseProgram = CBool(TerminateProcess(hWnd, lExitCode)) CloseHandle hWnd DoEvents Sleep (100) End Function Private Sub Command1_Click() '====================================décommente selon ton choix <<<<<<<<<<<<<<<<<<<<<<<<<<<<===================================== OpenProgram "d:\essai.txt", 0 'OpenProgram "calc.exe", 0 End Sub Private Sub Command2_Click() CloseProgram toto End Sub Private Sub Form_Load() End Sub