Attendre la fin d'exécution d'un programme .EXE sous CMD puis retour en Visual Basic 2015
Bonjour,
Je converti un programme VB6 en VB 2015.
Le but est d'attendre la fin d'exécution d'un .exe lancé avec la fonction EexcCmd
En VB6 ce petit programme fonctionne parfaitement :
Code:
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| 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
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret&
End Function
Sub Form_Click()
Dim retval As Long
retval = ExecCmd("notepad.exe")
MsgBox "Process Finished, Exit Code " & retval
End Sub |
La conversion en VB 2015 ne pose pas de problème pour Type STARTUPINFO et PROCESS_INFORMATION:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Option Explicit On
Imports System.IO
Imports System.Text
Imports System.Threading
Public Structure STARTUPINFO
Dim CB As Long
Dim lpReserved, lpDesktop, lpTitle As String
Dim dwX, dwy, dwXSize, dwYSize, dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags, wShowWindow, cbReserved2, lpReserved2, hStdInput, hStdOutput, hStdError As Long
End Structure
Public Structure PROCESS_INFORMATION
Dim hProcess, hThread, dwProcessId, dwThreadID As Long
End Structure |
Le problème se pose pour convertir depuis VB6
Code:
1 2
| Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long |
car la syntaxe ne semble pas la bonne en VB 2015. J'ai l'erreur au débogage : "Instruction non valide dans un espace de noms". Même problème pour les Declare Function suivants.
J'ai du mal à comprendre les explications MSDN : https://msdn.microsoft.com/fr-fr/library/3w4tctcf.aspx
Que faut-il valider dans l'espace de noms importés ?
D'autre part à la lecture de la définition de Declare en VB 2015 il semble qu'il faille ajouter Alias "<alias>" avant la liste d'argument. OK sur le principe mais que mettre dans "<alias>" ?
Autre piste PROCESS.START avec p=Process.Start et if p.HasExited then ... Mais là la littérature est assez muette ! Je ne trouve pas d'exemple concret sur la toile ...
En vous remerciant pour votre aide.
Bon dimanche.
La solution : wait = true
La solution est finalement très simple dans VB 2010, 2012 et suivants. Inutile de retranscrire le projet VB6 précédent => un seul paramètre Wait à mettre à True dans le Shell.
Consulter : Interaction.Shell méthode (String, AppWinStyle, Boolean, Int32) dans
https://msdn.microsoft.com/fr-fr/lib...code-snippet-1
Par défaut, le Shell fonction exécute le programme de façon asynchrone. Cela signifie qu’un programme lancé par le Shell (fonction) ne peut pas s’exécuter complètement avant les instructions qui suivent le Shell (fonction) sont exécutées. Si vous souhaitez attendre pour le programme se termine avant de continuer, définissez Wait à True.
quelques exemples d'écriture :
Code:
Shell("C:\Program Files\Notepad++\notepad++.exe", , True)
ou
Code:
ID = Shell("""C:\Program Files\display.exe"" -a -q", , True, 100000)
ou
Code:
Shell("c:\sbwin\py479usr\pywin64.exe -maxmem 4096000 " + "c:\sbwin\hninp.txt", , True)
Ne pas oublier les deux virgules avant True.
Espérant que cela rendra bien service.
Bon Dimanche