Kill Process Sous Win2k d'une app lancée avec shell()

soulheaven Messages postés 23 Date d'inscription dimanche 30 décembre 2001 Statut Membre Dernière intervention 11 avril 2006 - 25 juin 2002 à 18:08
cs_trouduc Messages postés 56 Date d'inscription jeudi 26 décembre 2002 Statut Membre Dernière intervention 28 septembre 2005 - 25 août 2005 à 22:22
voilà je cherches à fermer une application que g lancé avec shell()!!!

j'utilises la fonction SendMessage mais ça marche pas sous win2k!! il faudrait que ça marche pour moi, sur n'importe quel os!! xp,win2k, et win9x

merci!!

8 réponses

Derrick soft Messages postés 97 Date d'inscription jeudi 10 mai 2001 Statut Membre Dernière intervention 20 juin 2005
25 juin 2002 à 23:29
Bonjour,

Ce code fonctionne sous 2000/XP :

Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) 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 Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Type LUID
LowPart As Long
HighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'Purpose : Terminates a process given a process ID or a the handle to a form.
'Inputs : [lProcessID] The process ID (or PID) to terminate.
' [lHwndWindow] Any window handle belonging to the application.
'Outputs : Returns True on success.
'Author : Andrew Baker
'Date : 28/04/2001
'Notes : In WIN NT, click the "Processes" tab in the "Task Manager"
' to see the process ID (or PID) for an application.
' Must specify either lHwndWindow or lProcessID.
' Equivalent to pressing Alt+Ctrl+Del then "End Task"

Function ProcessTerminate(Optional lProcessID As Long, Optional lHwndWindow As Long) As Boolean
Dim lhwndProcess As Long
Dim lExitCode As Long
Dim lRetVal As Long
Dim lhThisProc As Long
Dim lhTokenHandle As Long
Dim tLuid As LUID
Dim tTokenPriv As TOKEN_PRIVILEGES, tTokenPrivNew As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
Const PROCESS_ALL_ACCESS &H1F0FFF, PROCESS_TERMINATE &H1 Const ANYSIZE_ARRAY 1, TOKEN_ADJUST_PRIVILEGES &H20 Const TOKEN_QUERY &H8, SE_DEBUG_NAME As String "SeDebugPrivilege" Const SE_PRIVILEGE_ENABLED &H2, PROCESS_TERMINATE &H1

On Error Resume Next
If lHwndWindow Then
'Get the process ID from the window handle
lRetVal = GetWindowThreadProcessId(lHwndWindow, lProcessID)
End If

If lProcessID Then
'Give Kill permissions to this process
lhThisProc = GetCurrentProcess

OpenProcessToken lhThisProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, lhTokenHandle
LookupPrivilegeValue "", SE_DEBUG_NAME, tLuid
'Set the number of privileges to be change
tTokenPriv.PrivilegeCount = 1
tTokenPriv.TheLuid = tLuid
tTokenPriv.Attributes = SE_PRIVILEGE_ENABLED
'Enable the kill privilege in the access token of this process
AdjustTokenPrivileges lhTokenHandle, False, tTokenPriv, Len(tTokenPrivNew), tTokenPrivNew, lBufferNeeded

'Open the process to kill
lhwndProcess = OpenProcess(PROCESS_TERMINATE, 0, lProcessID)

If lhwndProcess Then
'Obtained process handle, kill the process
ProcessTerminate = CBool(TerminateProcess(lhwndProcess, lExitCode))
Call CloseHandle(lhwndProcess)
End If
End If
On Error GoTo 0
End Function

'Example of how to close excel using VBA or VB referencing Excel type library
Sub TestVBA()
Dim lHwnd As Long
'Make Excel's caption unique, use in VBA
Application.Caption = "TEST EXCEL"
'VB CODE
'Find Excel's window handle
lHwnd = FindWindow("XLMAIN", Application.Caption)
'Terminate the process
ProcessTerminate , lHwnd
End Sub

'Example of how to close excel using VB
Sub TestVB()
Dim lHwnd As Long
'Find Excel's window handle
lHwnd = FindWindow("XLMAIN", vbNullString)
'Terminate the process
ProcessTerminate , lHwnd
End Sub

Cordialement
0
Derrick soft Messages postés 97 Date d'inscription jeudi 10 mai 2001 Statut Membre Dernière intervention 20 juin 2005
25 juin 2002 à 23:46
Oups,

J'ai oublier de dire que pour killer une applis il connaître le nom de la classe principal de l'applis et nom le nom du process, par exemple pour Visio le om de la classe est VISIOA et pour Word ce doit être OpusApp (ou un truc comme ça). Personnellement j'utilise Spy++ de visual studio pour les trouvé.

Cordialement
0
soulheaven Messages postés 23 Date d'inscription dimanche 30 décembre 2001 Statut Membre Dernière intervention 11 avril 2006
30 juin 2002 à 17:10
merci mais c pas tout à fait ça que je cherchais enfin merci quand meme

le truc c que la fonction shell normalement doit te renvoyer le processid de ton programme lancé!!

je voulais juste savoir comment le fermer sans connaitre le nom!!

merci
0
Derrick soft Messages postés 97 Date d'inscription jeudi 10 mai 2001 Statut Membre Dernière intervention 20 juin 2005
1 juil. 2002 à 08:58
Bonjour,

Le problème c'est que sous Win2K c'est le seul moyen de fermé une applis même lancer avec shell
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_madhatter Messages postés 9 Date d'inscription mardi 11 mars 2003 Statut Membre Dernière intervention 26 juin 2003
5 juin 2003 à 11:44
Je cherche a faire la meme chose que toi, cad fermer un programme ouvert avec shell, j'aimerai savoir si tu as trouvé depuis le temps. Fait moi parvenir la source si t'as trouvé (madhatter@tchatche.com).
Merci
0
dedebatou Messages postés 13 Date d'inscription jeudi 19 juin 2003 Statut Membre Dernière intervention 13 septembre 2007
28 août 2003 à 12:16
Bonjour,
Ce code semble être celui dont j'ai besoin. En effet j'ai deux programmes dévellopés en VB5, l'un est l'interface utilisateur, l'autre est le calculateur. Pour lancer le calculateur à partir de l'interface pas de problèmes, mais pour arrêter le calculateur, là c'est beaucoup plus compliqué.
J'ai fais fonctionner le code de démo pour fermer un document exel, ça marche sans problèmes. Mais pour mon exécutable VB je ne connais pas le nom de la classe principale.

Si quelqu'un pouvait me renseigner...

Merci d'avance
0
cs_trouduc Messages postés 56 Date d'inscription jeudi 26 décembre 2002 Statut Membre Dernière intervention 28 septembre 2005
25 août 2005 à 22:20
copier ca

Dim IDProg As Integer
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess 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 dwProcessID As Long) As Long 'Ouverture de Process


Private Sub Command1_Click()
Dim hProcess, Termine&


If Command1.Caption = "Start" Then
Command1.Caption = "Stop"
IDProg = Shell("notepad.exe", vbMinimizedFocus)
Else
Command1.Caption = "Start"
hProcess = OpenProcess(1, False, IDProg)
Termine& = TerminateProcess(hProcess, 4)
End If
End Sub


Private Sub Form_Load()
Command1.Caption = "Start"
End Sub
0
cs_trouduc Messages postés 56 Date d'inscription jeudi 26 décembre 2002 Statut Membre Dernière intervention 28 septembre 2005
25 août 2005 à 22:22
'copier tout ca dans une form et ajouter un bouton

Dim IDProg As Integer
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess 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 dwProcessID As Long) As Long 'Ouverture de Process


Private Sub Command1_Click()
Dim hProcess, Termine&


If Command1.Caption = "Start" Then
Command1.Caption = "Stop"
IDProg = Shell("notepad.exe", vbMinimizedFocus)
Else
Command1.Caption = "Start"
hProcess = OpenProcess(1, False, IDProg)
Termine& = TerminateProcess(hProcess, 4)
End If
End Sub


Private Sub Form_Load()
Command1.Caption = "Start"
End Sub
0
Rejoignez-nous