[urgent] Executer un programme et attendre la fin de son execution avant de fair

jfk003 Messages postés 45 Date d'inscription mardi 29 octobre 2002 Statut Membre Dernière intervention 16 septembre 2005 - 14 sept. 2005 à 21:42
jfk003 Messages postés 45 Date d'inscription mardi 29 octobre 2002 Statut Membre Dernière intervention 16 septembre 2005 - 16 sept. 2005 à 23:56
Bonjour,



Je m'excuse de mettre un vieux tag urgent devant la question, c'est
souvent mal vu mais pour le coup c'est plutot urgent justement !



J'execute un programme ms dos depuis mon programme vb, et je sais
pertinnement que ce programme a bien besoin de 10 secondes pour
réaliser sa tache. Seulement voila, vb ne semble pas attendre que mon
programme dos (lancé avec shell(nomduprog, vbhide) ) ai finit sa tâche,
il passe directement aux autres lignes de codes de la procédure !



J'aimerais donc que le programme vb attende que l'execution du
programme dos se soit achevée avant de faire autre chose ; pouvez vous
m'aider ?



Merci d'avance.

7 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
14 sept. 2005 à 22:18
Regarde:
ShellExecuteEx
WaitForSingleObject

question souvent traitée ici.

ciao...
BruNews, MVP VC++
0
jfk003 Messages postés 45 Date d'inscription mardi 29 octobre 2002 Statut Membre Dernière intervention 16 septembre 2005
14 sept. 2005 à 23:37
Merci beaucoup.



J'ignorais que c'était souvent discuté ici, et je ne savais vraiment
pas quoi taper dans le moteur de recherche (j'avais testé avec
"execution" comme mot clé, j'étais loin de me douter qu'il fallait
taper "WaitForSingleObject")



Bref j'ai trouvé je t'en remercie.
0
jfk003 Messages postés 45 Date d'inscription mardi 29 octobre 2002 Statut Membre Dernière intervention 16 septembre 2005
15 sept. 2005 à 00:01
hum en réalité j'ai une dernière question ! voici le code que j'ai trouvé pour réaliser ce que je demandais :




Option Explicit



Public Const NORMAL_PRIORITY_CLASS = &H20&

Public Const INFINITE = -1&



Public 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 Long

cbReserved2 As Long

lpReserved2 As Long

hStdInput As Long

hStdOutput As Long

hStdError As Long

End Type



Public Type PROCESS_INFORMATION

hProcess As Long

hThread As Long

dwProcessId As Long

dwThreadID As Long

End Type



'Note: the number of line continuations in

'the next declare may exceed VB's maximum; the API is

'presented this way here only for clarity.

Public Declare Function CreateProcessA Lib "kernel32" _

(ByVal lpAppName As Long, _

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 Long, _

lpStartupInfo As STARTUPINFO, _

lpProcessInformation As PROCESS_INFORMATION) As Long



Public Declare Function WaitForSingleObject Lib "kernel32" _

(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long



Public Declare Function CloseHandle Lib "kernel32" _

(ByVal hObject As Long) As Long



Public Sub RunProcess(cmdline As String)

Dim proc As PROCESS_INFORMATION

Dim start As STARTUPINFO



Dim r As Long

Call CreateProcessA(0&, cmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

'Wait for the application to finish

Call WaitForSingleObject(proc.hProcess, INFINITE)

'Close the handle to the process

Call CloseHandle(proc.hProcess)

'Close the handle to the thread created

Call CloseHandle(proc.hThread)

End Sub





Le problème, c'est qu'une fenetre dos s'ouvre à chaque fois. J'ai
cherché un peu partout mais pas trouvé ! Comment la mettre en "hidden"?
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
15 sept. 2005 à 00:16
Il faut régler les champs de STARTUPINFO, dwFlags et wShowWindow.
Il manque aussi les champs cb à régler sur la longueur des structures dans la fonction RunProcess().

Laisse tomber tout ceci, c'est un peu compliqué pour du VB et tu ne trouveras que des exemples erronés comme celui ci.
Passe par ShellExecuteEx qui encapsule l'appel CreateProcess, ce sera nettement plus simple.

ciao...
BruNews, MVP VC++
0

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

Posez votre question
crenaud76 Messages postés 4172 Date d'inscription mercredi 30 juillet 2003 Statut Membre Dernière intervention 9 juin 2006 28
15 sept. 2005 à 11:06
J'ai une source ici même qui le fait ... http://www.vbfrance.com/code.aspx?ID=8551

Christophe
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
15 sept. 2005 à 11:13
Dim sei As SHELLEXECUTEINFO
remplir champs de sei

if ShellExecuteEx(sei) Then
WaitForSingleObject sei.hProcess, INFINITE
CloseHandle sei.hProcess
End If

ciao...
BruNews, MVP VC++
0
jfk003 Messages postés 45 Date d'inscription mardi 29 octobre 2002 Statut Membre Dernière intervention 16 septembre 2005
16 sept. 2005 à 23:56
Merci pour vos réponses, ça m'a permi de trouver la solution !



Pour executer en hidden, il suffit de rajouter :



Dim STARTF_USESHOWWINDOW As Long

STARTF_USESHOWWINDOW = &H1

start.dwFlags = STARTF_USESHOWWINDOW

start.wShowWindow = SW_HIDE



En dessous des déclarations dans la sub RunProcess !
0
Rejoignez-nous