OUVRIR / FERMER UNE APPLICATION EXTERNE DEPUIS VOTRE PROG VB.

devsurf Messages postés 148 Date d'inscription dimanche 13 octobre 2002 Statut Membre Dernière intervention 13 novembre 2008 - 7 nov. 2003 à 01:08
UnMage Messages postés 32 Date d'inscription samedi 26 juin 2004 Statut Membre Dernière intervention 15 novembre 2004 - 1 sept. 2004 à 14:38
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/17734-ouvrir-fermer-une-application-externe-depuis-votre-prog-vb

UnMage Messages postés 32 Date d'inscription samedi 26 juin 2004 Statut Membre Dernière intervention 15 novembre 2004
1 sept. 2004 à 14:38
Ouip désolé j'avais mis tout ça vite fait j'étais assez pressé.
cs_YaNn0u Messages postés 48 Date d'inscription lundi 6 janvier 2003 Statut Membre Dernière intervention 27 avril 2005
31 août 2004 à 15:14
Donc voici le code qui marche.. dans un module :

------------------------------
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As Long
Public Declare Function ReadProcessMem Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
-------------------------------------------

dans une form :

Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Function CloseWindowClient(gamewindowtext As String)
Dim hwnd As Long
Dim pid As Long
hwnd = FindWindow(vbNullString, gamewindowtext)
GetWindowThreadProcessId hwnd, pid
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
GetExitCodeProcess hProcess, nRet
Call TerminateProcess(hProcess, nRet)
Call CloseHandle(hProcess)
End Function

Private Sub Command1_Click()
Call CloseWindowClient("Sans titre - Bloc-notes")
End Sub
---------------------------------


WiP
cs_YaNn0u Messages postés 48 Date d'inscription lundi 6 janvier 2003 Statut Membre Dernière intervention 27 avril 2005
31 août 2004 à 14:45
Je râle pas, je l'informe d'un oubli.. Perso ca me gêne pas je l'avais déjà sur un autre code mais c'est pour les autres visiteurs !
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
31 août 2004 à 08:17
vous allez pas raler pour quelques declarations d'API manquantes ?

un peu de recherche , ca fait de mal a personne !

UnMage, dommage que tu aies deux lignes qui ne se rapportent pas a la fonction "generique" de fermeteure d'application, et qu'il n'y aies pas de gestion d'erreur (app non trouvée...) :

Private Function CloseWindowClient(gamewindowtext As String)
Dim hwnd As Long
Dim pid As Long
hwnd = FindWindow(vbNullString, gamewindowtext)
GetWindowThreadProcessId hwnd, pid
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
GetExitCodeProcess hProcess, nRet
Call TerminateProcess(hProcess, nRet)
Call CloseHandle(hProcess)
Unload Main <=====
Unload Browser <====
End Function
cs_YaNn0u Messages postés 48 Date d'inscription lundi 6 janvier 2003 Statut Membre Dernière intervention 27 avril 2005
31 août 2004 à 03:51
il manque findwindow hihi :x
Concretement, peux tu donner un exemple de ta source ?
UnMage Messages postés 32 Date d'inscription samedi 26 juin 2004 Statut Membre Dernière intervention 15 novembre 2004
31 août 2004 à 03:45
Ah oui pardon :
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As Long
Private Declare Function ReadProcessMem Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

Il doit y en avoir en trop mais j'ai la flemme de trier..
cs_YaNn0u Messages postés 48 Date d'inscription lundi 6 janvier 2003 Statut Membre Dernière intervention 27 avril 2005
31 août 2004 à 03:06
marche pas unmage, il faudrait déclarer getwindowthreadprocess..
UnMage Messages postés 32 Date d'inscription samedi 26 juin 2004 Statut Membre Dernière intervention 15 novembre 2004
7 août 2004 à 13:32
Moi j'utilise ça pour fermer un process de mon choix :


Private Const PROCESS_ALL_ACCESS = &H1F0FFF
------------------------------------------------------------------
Private Function CloseWindowClient(gamewindowtext As String)
Dim hwnd As Long
Dim pid As Long
hwnd = FindWindow(vbNullString, gamewindowtext)
GetWindowThreadProcessId hwnd, pid
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
GetExitCodeProcess hProcess, nRet
Call TerminateProcess(hProcess, nRet)
Call CloseHandle(hProcess)
Unload Main
Unload Browser
End Function
-----------------------------------------------------------------------
Module01
-----------------------------------------------------------------------
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject 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


J'ai récupéré TerminateProcess et GetExitCodeProcess de la source çi-dessus.
devsurf Messages postés 148 Date d'inscription dimanche 13 octobre 2002 Statut Membre Dernière intervention 13 novembre 2008
7 nov. 2003 à 01:08
C'est très sympa..
j'ai par contre un problème :
Tout fonctionne bien si l'application EXE n'a pas été lancée avant le demarage de notre code
Exemple :
Demarer word
Demarer notre source (ton prog vb)
A partir dela : Je n'arrive plus à maitriser le process dans ce cas
La fermeturte ne se fait pas , la création est alèatoire.
As tu une idée?
Rejoignez-nous