Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

VB et status de fin vers une ligne de commande CMD

6 réponses
Avatar
abo
je voudrais que mon prog VB retourne un status particulier vers le shell dos
qui l'as lancé ..

ex
toto.exe
if errorlevel 1 goto..
if errorlevel 0 goto..

mon prog semble toujours renvoyer 0 ..
comment proceder y a t'il une commande en VB pour renvoyer 1 si le prog
s'est mal terminé..
merci

6 réponses

Avatar
ng
Salut,

Tu peux essayer ceci :

'Dans un module :

Option Explicit
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As
Long, lpExitCode As Long) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As
Long, ByVal uExitCode As Long) As Long
Public Const STILL_ACTIVE = &H103
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Function Shell2(ByVal sExeName As String, Optional ByVal
vWindowsStyle As VbAppWinStyle, Optional ByVal bAttendFinExe As Boolean False, Optional ByVal iMaxExeDelai As Long = -1) As Long
Dim IdProc As Long, hIdproc As Long, dTps As Double, retval As Long
IdProc = Shell(sExeName, vWindowsStyle)
If bAttendFinExe Then
hIdproc = OpenProcess(PROCESS_QUERY_INFORMATION, False, IdProc)
dTps = GetTickCount
Do
GetExitCodeProcess hIdproc, retval
DoEvents
If iMaxExeDelai > 0 And (GetTickCount - dTps) >= iMaxExeDelai Then
Call TerminateProcess(hIdproc, retval)
retval = 0
End If
Loop While retval = STILL_ACTIVE
CloseHandle hIdproc
End If
Shell2 = IdProc
End Function

Public Function AppPath() As String
If Right$(App.Path, 1) = "" Then AppPath = App.Path Else AppPath = App.Path
& ""
End Function

Public Function GetCmd2(sCmd As String) As String
Dim k As Integer, sChe As String, sChe2 As String, sBuff As String
sChe = AppPath & "TmpCmd.bat"
sChe2 = AppPath & "Recup.txt"
k = FreeFile
Open sChe For Output As #k
'Print #k, sCmd & " > """ & sChe2 & """"
Print #k, Replace(sCmd, "%OUT_FILE%", """" & sChe2 & """", , ,
vbTextCompare)
Close #k
Call Shell2(sChe, vbHide, True)
k = FreeFile
Open sChe2 For Binary As #k
sBuff = Space$(LOF(k))
Get #k, , sBuff
Close #k
GetCmd2 = sBuff: sBuff = ""
On Error Resume Next
Call Kill(sChe)
Call Kill(sChe2)
End Function


'Ensuite n'importe ou, exemple d'utilisation de GetCmd2 :

MsgBox GetCmd2("echo salut > %OUT_FILE%") 'renvoit salut

MsgBox GetCmd2("ipconfig > %OUT_FILE%") 'renvoit la config ip de windows

'renvoit 1 ou 0
MsgBox GetCmd2("echo off" & vbCrLf & _
"cls" & vbCrLf & _
"notepad.exe" & vbCrLf & _
"if errorlevel 1 goto Erreur" & vbCrLf & _
"if errorlevel 0 goto FinOk" & vbCrLf & _
":Erreur" & vbCrLf & _
"echo 0 > %OUT_FILE%" & vbCrLf & _
"goto Fin" & vbCrLf & _
":FinOk" & vbCrLf & _
"echo 1 > %OUT_FILE%" & vbCrLf & _
"goto Fin" & vbCrLf & _
":Fin")

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/


"abo" a écrit dans le message de news:
c07hoo$2k0$
je voudrais que mon prog VB retourne un status particulier vers le shell


dos
qui l'as lancé ..

ex
toto.exe
if errorlevel 1 goto..
if errorlevel 0 goto..

mon prog semble toujours renvoyer 0 ..
comment proceder y a t'il une commande en VB pour renvoyer 1 si le prog
s'est mal terminé..
merci




Avatar
abo
ben après avoir un peu recherché il y a bcp plus simple !

Private Declare Function WinExitProcess Lib "kernel32" _
Alias "ExitProcess" ( _
ByVal uExitCode As Long _
) As Long

call WinExitProcess (lStatusRet)

ou LstatusRet = la valeur que l'on veut transmettre à errorLevel

néanmoins ->merci pour la réponse
cdt


"ng" a écrit dans le message news:

Salut,

Tu peux essayer ceci :

'Dans un module :

Option Explicit
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess


As
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess


As
Long, lpExitCode As Long) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As
Long, ByVal uExitCode As Long) As Long
Public Const STILL_ACTIVE = &H103
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Function Shell2(ByVal sExeName As String, Optional ByVal
vWindowsStyle As VbAppWinStyle, Optional ByVal bAttendFinExe As Boolean > False, Optional ByVal iMaxExeDelai As Long = -1) As Long
Dim IdProc As Long, hIdproc As Long, dTps As Double, retval As Long
IdProc = Shell(sExeName, vWindowsStyle)
If bAttendFinExe Then
hIdproc = OpenProcess(PROCESS_QUERY_INFORMATION, False, IdProc)
dTps = GetTickCount
Do
GetExitCodeProcess hIdproc, retval
DoEvents
If iMaxExeDelai > 0 And (GetTickCount - dTps) >= iMaxExeDelai Then
Call TerminateProcess(hIdproc, retval)
retval = 0
End If
Loop While retval = STILL_ACTIVE
CloseHandle hIdproc
End If
Shell2 = IdProc
End Function

Public Function AppPath() As String
If Right$(App.Path, 1) = "" Then AppPath = App.Path Else AppPath App.Path
& ""
End Function

Public Function GetCmd2(sCmd As String) As String
Dim k As Integer, sChe As String, sChe2 As String, sBuff As String
sChe = AppPath & "TmpCmd.bat"
sChe2 = AppPath & "Recup.txt"
k = FreeFile
Open sChe For Output As #k
'Print #k, sCmd & " > """ & sChe2 & """"
Print #k, Replace(sCmd, "%OUT_FILE%", """" & sChe2 & """", , ,
vbTextCompare)
Close #k
Call Shell2(sChe, vbHide, True)
k = FreeFile
Open sChe2 For Binary As #k
sBuff = Space$(LOF(k))
Get #k, , sBuff
Close #k
GetCmd2 = sBuff: sBuff = ""
On Error Resume Next
Call Kill(sChe)
Call Kill(sChe2)
End Function


'Ensuite n'importe ou, exemple d'utilisation de GetCmd2 :

MsgBox GetCmd2("echo salut > %OUT_FILE%") 'renvoit salut

MsgBox GetCmd2("ipconfig > %OUT_FILE%") 'renvoit la config ip de windows

'renvoit 1 ou 0
MsgBox GetCmd2("echo off" & vbCrLf & _
"cls" & vbCrLf & _
"notepad.exe" & vbCrLf & _
"if errorlevel 1 goto Erreur" & vbCrLf & _
"if errorlevel 0 goto FinOk" & vbCrLf & _
":Erreur" & vbCrLf & _
"echo 0 > %OUT_FILE%" & vbCrLf & _
"goto Fin" & vbCrLf & _
":FinOk" & vbCrLf & _
"echo 1 > %OUT_FILE%" & vbCrLf & _
"goto Fin" & vbCrLf & _
":Fin")

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/


"abo" a écrit dans le message de news:
c07hoo$2k0$
> je voudrais que mon prog VB retourne un status particulier vers le shell
dos
> qui l'as lancé ..
>
> ex
> toto.exe
> if errorlevel 1 goto..
> if errorlevel 0 goto..
>
> mon prog semble toujours renvoyer 0 ..
> comment proceder y a t'il une commande en VB pour renvoyer 1 si le prog
> s'est mal terminé..
> merci
>
>




Avatar
ng
ah ok c'est cela que tu voulais !!! Je croyais que tu arrivais deja un
retourner ta valeur mais que tu voulais la recupérer depuis une autre appli.

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/


"abo" a écrit dans le message de news:
c0a8pt$g49$
ben après avoir un peu recherché il y a bcp plus simple !

Private Declare Function WinExitProcess Lib "kernel32" _
Alias "ExitProcess" ( _
ByVal uExitCode As Long _
) As Long

call WinExitProcess (lStatusRet)

ou LstatusRet = la valeur que l'on veut transmettre à errorLevel

néanmoins ->merci pour la réponse
cdt


"ng" a écrit dans le message news:

> Salut,
>
> Tu peux essayer ceci :
>
> 'Dans un module :
>
> Option Explicit
> Public Declare Function OpenProcess Lib "kernel32" (ByVal


dwDesiredAccess
As
> Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
> Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As


Long)
> As Long
> Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal


hProcess
As
> Long, lpExitCode As Long) As Long
> Public Declare Function GetTickCount Lib "kernel32" () As Long
> Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess


As
> Long, ByVal uExitCode As Long) As Long
> Public Const STILL_ACTIVE = &H103
> Public Const PROCESS_QUERY_INFORMATION = &H400
> Public Function Shell2(ByVal sExeName As String, Optional ByVal
> vWindowsStyle As VbAppWinStyle, Optional ByVal bAttendFinExe As Boolean


> > False, Optional ByVal iMaxExeDelai As Long = -1) As Long
> Dim IdProc As Long, hIdproc As Long, dTps As Double, retval As Long
> IdProc = Shell(sExeName, vWindowsStyle)
> If bAttendFinExe Then
> hIdproc = OpenProcess(PROCESS_QUERY_INFORMATION, False, IdProc)
> dTps = GetTickCount
> Do
> GetExitCodeProcess hIdproc, retval
> DoEvents
> If iMaxExeDelai > 0 And (GetTickCount - dTps) >= iMaxExeDelai


Then
> Call TerminateProcess(hIdproc, retval)
> retval = 0
> End If
> Loop While retval = STILL_ACTIVE
> CloseHandle hIdproc
> End If
> Shell2 = IdProc
> End Function
>
> Public Function AppPath() As String
> If Right$(App.Path, 1) = "" Then AppPath = App.Path Else AppPath > App.Path
> & ""
> End Function
>
> Public Function GetCmd2(sCmd As String) As String
> Dim k As Integer, sChe As String, sChe2 As String, sBuff As String
> sChe = AppPath & "TmpCmd.bat"
> sChe2 = AppPath & "Recup.txt"
> k = FreeFile
> Open sChe For Output As #k
> 'Print #k, sCmd & " > """ & sChe2 & """"
> Print #k, Replace(sCmd, "%OUT_FILE%", """" & sChe2 & """", , ,
> vbTextCompare)
> Close #k
> Call Shell2(sChe, vbHide, True)
> k = FreeFile
> Open sChe2 For Binary As #k
> sBuff = Space$(LOF(k))
> Get #k, , sBuff
> Close #k
> GetCmd2 = sBuff: sBuff = ""
> On Error Resume Next
> Call Kill(sChe)
> Call Kill(sChe2)
> End Function
>
>
> 'Ensuite n'importe ou, exemple d'utilisation de GetCmd2 :
>
> MsgBox GetCmd2("echo salut > %OUT_FILE%") 'renvoit salut
>
> MsgBox GetCmd2("ipconfig > %OUT_FILE%") 'renvoit la config ip de windows
>
> 'renvoit 1 ou 0
> MsgBox GetCmd2("echo off" & vbCrLf & _
> "cls" & vbCrLf & _
> "notepad.exe" & vbCrLf & _
> "if errorlevel 1 goto Erreur" & vbCrLf & _
> "if errorlevel 0 goto FinOk" & vbCrLf & _
> ":Erreur" & vbCrLf & _
> "echo 0 > %OUT_FILE%" & vbCrLf & _
> "goto Fin" & vbCrLf & _
> ":FinOk" & vbCrLf & _
> "echo 1 > %OUT_FILE%" & vbCrLf & _
> "goto Fin" & vbCrLf & _
> ":Fin")
>
> --
> Nicolas G.
> FAQ VB : http://faq.vb.free.fr
> API Guide : http://www.allapi.net
> Google Groups : http://groups.google.fr/
> MZ-Tools : http://www.mztools.com/
>
>
> "abo" a écrit dans le message de news:
> c07hoo$2k0$
> > je voudrais que mon prog VB retourne un status particulier vers le


shell
> dos
> > qui l'as lancé ..
> >
> > ex
> > toto.exe
> > if errorlevel 1 goto..
> > if errorlevel 0 goto..
> >
> > mon prog semble toujours renvoyer 0 ..
> > comment proceder y a t'il une commande en VB pour renvoyer 1 si le


prog
> > s'est mal terminé..
> > merci
> >
> >
>
>




Avatar
Zoury
Salut Nicolas! :O)

Call TerminateProcess(hIdproc, retval)



TerminateProcess ne libère pas les ressources.. c'est le jumeau de
l'abominable fonction End.. (j'suis pratiquement sur que cette dernière
appelle cet API) :O/

tiré de la MSDN :

"The TerminateProcess function is used to unconditionally cause a process to
exit. Use it only in extreme circumstances. The state of global data
maintained by dynamic-link libraries (DLLs) may be compromised if
TerminateProcess is used rather than ExitProcess."


Comme l'a découvert l'API ExitProcess est la bonne méthode à employer pour
terminer un processus et renvoyer une valeur.

tiré de la MSDN :

"ExitProcess is the preferred method of ending a process. This function
provides a clean process shutdown. This includes calling the entry-point
function of all attached dynamic-link libraries (DLLs) with a value
indicating that the process is detaching from the DLL. If a process
terminates by calling TerminateProcess, the DLLs that the process is
attached to are not notified of the process termination."


--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

Merci de poster les réponses au groupe afin d'en faire profiter à tous
Avatar
Zoury
Salut Abo! :O)

Je te conseille cette déclaration qui n'emploi pas d'alias pour des raisons
de compatibilité et surtout parce que c'est le nom réel de la fonction alors
autant l'apprendre tout de suite.. ;O)

Private Declare Function ExitProcess _
Lib "kernel32" _
( _
ByVal uExitCode As Long _
) As Long


--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

Merci de poster les réponses au groupe afin d'en faire profiter à tous
Avatar
ng
Ok au temps pour moi mais de toute facon dans le code que j'ai fournis
précédemment elle n'est employée que si le delai d'exécution est dépassé. Si
on ne définit pas de délai alors pas de problème

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/


"Zoury" <yanick_lefebvre at hotmail dot com> a écrit dans le message de
news: #rYGSe#
Salut Nicolas! :O)

> Call TerminateProcess(hIdproc, retval)

TerminateProcess ne libère pas les ressources.. c'est le jumeau de
l'abominable fonction End.. (j'suis pratiquement sur que cette dernière
appelle cet API) :O/

tiré de la MSDN :

"The TerminateProcess function is used to unconditionally cause a process


to
exit. Use it only in extreme circumstances. The state of global data
maintained by dynamic-link libraries (DLLs) may be compromised if
TerminateProcess is used rather than ExitProcess."


Comme l'a découvert l'API ExitProcess est la bonne méthode à employer pour
terminer un processus et renvoyer une valeur.

tiré de la MSDN :

"ExitProcess is the preferred method of ending a process. This function
provides a clean process shutdown. This includes calling the entry-point
function of all attached dynamic-link libraries (DLLs) with a value
indicating that the process is detaching from the DLL. If a process
terminates by calling TerminateProcess, the DLLs that the process is
attached to are not notified of the process termination."


--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

Merci de poster les réponses au groupe afin d'en faire profiter à tous