Page 1 sur 1

Lancer un executable et de stopper le code LS

MessagePublié: 06 Nov 2007 à 09:04
par Michael DELIQUE
Code : Tout sélectionner
Public Sub RunProcess_API(Byval strCommand As String)
   
   'cette fonction permet de lancer un executable et de stopper le code LS tant que celui-ci n'a pas été fermé
   
%REM
'variables API pour la fonction RunProcess_API
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


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
Declare Function WaitForSingleObject Lib "kernel32" (Byval hHandle As Long, Byval dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" (Byval hObject As Long) As Long

%END REM
   
   'Déclaration variable
   Dim proc As PROCESS_INFORMATION
   Dim start As STARTUPINFO
   
   On Error Goto ErreurRunProcess_API
   
  'Initialize the STARTUPINFO structure by
  'passing to start the size of the STARTUPINFO
  'type. Setting the  .cb member is the only
  'item of the structure needed to launch the program
   start.cb = Len(start)
   
  'Start the application
   Call CreateProcessA(0&, strCommand, 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)
   
   Msgbox "The Shelled process " & strCommand & " has ended."
   
   Exit Sub
ErreurRunProcess_API:
   Msgbox "("+Cstr(Getthreadinfo (1))+" Call by "+Cstr(Getthreadinfo(10))+")"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"

   Exit Sub
End Sub