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 : Sélectionner tout - Visualiser dans une fenêtre à part
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 : Sélectionner tout - Visualiser dans une fenêtre à part
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 : Sélectionner tout - Visualiser dans une fenêtre à part
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.