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
|
'CloseApp Sample 13/11/2009, 13h40 by maxim_um
'for http://www.developpez.net/forums/d829422/autres-langages/general-visual-basic-6-vbscript/vbscript/fermer-proprement-mettre-1er-plan-logiciel/
'Not Warranty
'Enjoy
Option Explicit
Dim oWShell, oWMIService, oAllProcesses, oProcess, iRetErr, sWMC, sAppTitle, sAppFile, sAppName, sComputer
iRetErr=1
sWMC="%({F4})"
sComputer= "."
sAppTitle="calculatrice"
sAppName="calculatrice"
sAppFile="calc.exe"
Set oWShell= WScript.CreateObject("WScript.Shell")
sub SendWMClose ( byval spAppName, byval spAppTitleOrPID)
If (oWShell.AppActivate (spAppTitleOrPID)) Then
'WScript.Sleep 200
oWShell.SendKeys sWMC
oWShell.Popup "Opération terminée",2, spAppName, vbInformation
iRetErr=0
else
oWShell.Popup "Une erreur a eu lieu",3,spAppName, vbCritical
end if
end sub
sub CloseByPID ( byval spAppName, byval spAppFile)
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set oAllProcesses = oWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name =" & "'" & spAppFile & "'")
If (oAllProcesses.Count-1=0) Then
'! Indexation pour objets WMI non disponible.
For Each oProcess In oAllProcesses
SendWMClose spAppName, oProcess.ProcessID
Exit For
Next
'else
'oWShell.Popup "Une erreur a eu lieu",3,spAppName, vbCritical
end IF
end sub
'SendWMClose sAppTitle
CloseByPID sAppName, sAppFile
Set oWShell=Nothing
Set oWMIService=Nothing
WScript.Quit(iRetErr) |
Partager