si je fais du pas à pas (F8) pas de problème: la fenêtre Dos s'ouvre le code
s'exécute et la fenêtre Dos se ferme.
Par contre si j'exécute le code normalement, la fenêtre Dos s'ouvre et se
referme aussi vite et n'exécute pas mon fichier .bat.
si je fais du pas à pas (F8) pas de problème: la fenêtre Dos s'ouvre le code s'exécute et la fenêtre Dos se ferme. Par contre si j'exécute le code normalement, la fenêtre Dos s'ouvre et se referme aussi vite et n'exécute pas mon fichier .bat.
Ce qu'il faut, me semble t-il, c'est attendre la fin de l'exécution du batch ou de la commande. Pour cela, tu peux utiliser les APIs :
Private Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type
Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _ hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ lpApplicationName As String, ByVal lpCommandLine As String, ByVal _ lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As _ PROCESS_INFORMATION) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function GetExitCodeProcess Lib "kernel32" _ (ByVal hProcess As Long, lpExitCode As Long) As Long
retval = ExecCmd("toto.bat") MsgBox "Le processus est terminé, code de sortie : " & retval End Sub
Public Function ExecCmd(CmdLine As String) As Long Dim proc As PROCESS_INFORMATION Dim si As STARTUPINFO Dim ret As Long
' Initialisez la structure STARTUPINFO : si.cb = Len(si) ' Démarrez l'application Shell : ret = CreateProcessA(vbNullString, CmdLine, 0&, 0&, 1&, _ NORMAL_PRIORITY_CLASS, 0&, vbNullString, si, proc) ' Attendre la fin de l'application Shell : ret = WaitForSingleObject(proc.hProcess, INFINITE) GetExitCodeProcess proc.hProcess, ret CloseHandle proc.hThread CloseHandle proc.hProcess ExecCmd = ret End Function
'--------------------------------------------------------------------- ' ou utiliser Windows Script Host Object Model (Référence à ajouter au ' ' projet : wshom.ocx) '---------------------------------------------------------------------
Private Sub Command1_Click() Dim retval As Long
retval = WshRun("toto.bat") MsgBox "Le processus est terminé, code de sortie : " & retval End Sub
Private Function WshRun(CmdLine As String) As Long Dim wsh As WshShell Dim ret As Long
Set wsh = New WshShell ret = wsh.Run(CmdLine, 5, True) WshRun = ret Set wsh = Nothing End Function
si je fais du pas à pas (F8) pas de problème: la fenêtre Dos s'ouvre le code
s'exécute et la fenêtre Dos se ferme.
Par contre si j'exécute le code normalement, la fenêtre Dos s'ouvre et se
referme aussi vite et n'exécute pas mon fichier .bat.
Ce qu'il faut, me semble t-il, c'est attendre la fin de l'exécution du
batch ou de la commande. Pour cela, tu peux utiliser les APIs :
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
retval = ExecCmd("toto.bat")
MsgBox "Le processus est terminé, code de sortie : " & retval
End Sub
Public Function ExecCmd(CmdLine As String) As Long
Dim proc As PROCESS_INFORMATION
Dim si As STARTUPINFO
Dim ret As Long
' Initialisez la structure STARTUPINFO :
si.cb = Len(si)
' Démarrez l'application Shell :
ret = CreateProcessA(vbNullString, CmdLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, si, proc)
' Attendre la fin de l'application Shell :
ret = WaitForSingleObject(proc.hProcess, INFINITE)
GetExitCodeProcess proc.hProcess, ret
CloseHandle proc.hThread
CloseHandle proc.hProcess
ExecCmd = ret
End Function
'---------------------------------------------------------------------
' ou utiliser Windows Script Host Object Model (Référence à ajouter au '
' projet : wshom.ocx)
'---------------------------------------------------------------------
Private Sub Command1_Click()
Dim retval As Long
retval = WshRun("toto.bat")
MsgBox "Le processus est terminé, code de sortie : " & retval
End Sub
Private Function WshRun(CmdLine As String) As Long
Dim wsh As WshShell
Dim ret As Long
Set wsh = New WshShell
ret = wsh.Run(CmdLine, 5, True)
WshRun = ret
Set wsh = Nothing
End Function
si je fais du pas à pas (F8) pas de problème: la fenêtre Dos s'ouvre le code s'exécute et la fenêtre Dos se ferme. Par contre si j'exécute le code normalement, la fenêtre Dos s'ouvre et se referme aussi vite et n'exécute pas mon fichier .bat.
Ce qu'il faut, me semble t-il, c'est attendre la fin de l'exécution du batch ou de la commande. Pour cela, tu peux utiliser les APIs :
Private Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type
Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _ hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ lpApplicationName As String, ByVal lpCommandLine As String, ByVal _ lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As _ PROCESS_INFORMATION) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function GetExitCodeProcess Lib "kernel32" _ (ByVal hProcess As Long, lpExitCode As Long) As Long
retval = ExecCmd("toto.bat") MsgBox "Le processus est terminé, code de sortie : " & retval End Sub
Public Function ExecCmd(CmdLine As String) As Long Dim proc As PROCESS_INFORMATION Dim si As STARTUPINFO Dim ret As Long
' Initialisez la structure STARTUPINFO : si.cb = Len(si) ' Démarrez l'application Shell : ret = CreateProcessA(vbNullString, CmdLine, 0&, 0&, 1&, _ NORMAL_PRIORITY_CLASS, 0&, vbNullString, si, proc) ' Attendre la fin de l'application Shell : ret = WaitForSingleObject(proc.hProcess, INFINITE) GetExitCodeProcess proc.hProcess, ret CloseHandle proc.hThread CloseHandle proc.hProcess ExecCmd = ret End Function
'--------------------------------------------------------------------- ' ou utiliser Windows Script Host Object Model (Référence à ajouter au ' ' projet : wshom.ocx) '---------------------------------------------------------------------
Private Sub Command1_Click() Dim retval As Long
retval = WshRun("toto.bat") MsgBox "Le processus est terminé, code de sortie : " & retval End Sub
Private Function WshRun(CmdLine As String) As Long Dim wsh As WshShell Dim ret As Long
Set wsh = New WshShell ret = wsh.Run(CmdLine, 5, True) WshRun = ret Set wsh = Nothing End Function
-- Cordialement,
Jacques.
Ivan Pequeño
Merci aux deux Jacques
J'avais exactement le meme Pb en faisant du Ftp de et vers un Host IBM 3270 je pense que celle-ci est la vrai solution
Merci encore
Merci aux deux Jacques
J'avais exactement le meme Pb
en faisant du Ftp de et vers un Host IBM 3270
je pense que celle-ci est la vrai solution