begin process at 2012 02 13 07:36:06
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

API

 > LANCER UN EXECUTABLE ET ATTENDRE SA FIN

LANCER UN EXECUTABLE ET ATTENDRE SA FIN


 Information sur la source

Note :
9 / 10 - par 3 personnes
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :API Niveau :Initié Date de création :24/04/2003 Date de mise à jour :24/04/2003 00:46:20 Vu :10 218

Auteur : Crazyht

Ecrire un message privé
Site perso
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (5)
Ajouter un commentaire et/ou une note


 Description

Coller cette source dans un module de code VB6, la fonction LanceApp permet de lancer un executable et d'attendre quelle se termine. Une fois cette tache finie, elle retourne le code exit de l'appli lancée

Source

  • Public Const INFINITE = &HFFFF
  • Public Const STARTF_USESHOWWINDOW = &H1
  • Public Enum enSW
  • SW_HIDE = 0
  • SW_NORMAL = 1
  • SW_MAXIMIZE = 3
  • SW_MINIMIZE = 6
  • End Enum
  • Public Type PROCESS_INFORMATION
  • hProcess As Long
  • hThread As Long
  • dwProcessId As Long
  • dwThreadId As Long
  • End Type
  • 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 Integer
  • cbReserved2 As Integer
  • lpReserved2 As Byte
  • hStdInput As Long
  • hStdOutput As Long
  • hStdError As Long
  • End Type
  • Public Type SECURITY_ATTRIBUTES
  • nLength As Long
  • lpSecurityDescriptor As Long
  • bInheritHandle As Long
  • End Type
  • Public Enum enPriority_Class
  • NORMAL_PRIORITY_CLASS = &H20
  • IDLE_PRIORITY_CLASS = &H40
  • HIGH_PRIORITY_CLASS = &H80
  • End Enum
  • Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
  • Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  • Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
  • Public Function LanceApp(Appli As String, Arg As String, Timeout As Long, ByVal StartSize As enSW, ByVal Priority_Class As enPriority_Class) As Long
  • LanceApp = 259
  • Dim pclass As Long
  • Dim Ret As Long
  • Dim cmdline As String
  • Dim sinfo As STARTUPINFO
  • Dim pinfo As PROCESS_INFORMATION
  • Dim sec1 As SECURITY_ATTRIBUTES
  • Dim sec2 As SECURITY_ATTRIBUTES
  • Dim hMutex As Long
  • sec1.nLength = Len(sec1)
  • sec2.nLength = Len(sec2)
  • sinfo.cb = Len(sinfo)
  • sinfo.dwFlags = STARTF_USESHOWWINDOW
  • sinfo.wShowWindow = StartSize
  • pclass = Priority_Class
  • cmdline = Appli
  • If (Len(Trim$(Arg)) > 0) Then
  • cmdline = cmdline & " " & Arg
  • End If
  • Debug.Print " cmdLine : " & cmdline
  • If CreateProcess(vbNullString, cmdline, sec1, sec2, False, pclass, 0&, CurDir$(), sinfo, pinfo) Then
  • Debug.Print " CreateProcess OK"
  • Ret = 259
  • While (Ret = 259)
  • WaitForSingleObject pinfo.hProcess, Timeout
  • DoEvents
  • If (GetExitCodeProcess(pinfo.hProcess, Ret) = 1) Then
  • Debug.Print " GetExitCodeProcess OK"
  • LanceApp = Ret
  • Else
  • Debug.Print " GetExitCodeProcess KO"
  • End If
  • Wend
  • Else
  • Debug.Print " CreateProcess KO : " & Err.LastDllError
  • End If
  • End Function
Public Const INFINITE = &HFFFF
Public Const STARTF_USESHOWWINDOW = &H1

Public Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum

Public Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

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 Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Public Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Public Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long


Public Function LanceApp(Appli As String, Arg As String, Timeout As Long, ByVal StartSize As enSW, ByVal Priority_Class As enPriority_Class) As Long
    LanceApp = 259
    Dim pclass As Long
    Dim Ret As Long
    Dim cmdline As String
    Dim sinfo As STARTUPINFO
    Dim pinfo As PROCESS_INFORMATION
    Dim sec1 As SECURITY_ATTRIBUTES
    Dim sec2 As SECURITY_ATTRIBUTES
    Dim hMutex As Long
    
    sec1.nLength = Len(sec1)
    sec2.nLength = Len(sec2)
    sinfo.cb = Len(sinfo)
    sinfo.dwFlags = STARTF_USESHOWWINDOW
    sinfo.wShowWindow = StartSize
    pclass = Priority_Class
    
    cmdline = Appli
    If (Len(Trim$(Arg)) > 0) Then
        cmdline = cmdline & " " & Arg
    End If
        
    Debug.Print " cmdLine : " & cmdline

    If CreateProcess(vbNullString, cmdline, sec1, sec2, False, pclass, 0&, CurDir$(), sinfo, pinfo) Then
        Debug.Print " CreateProcess OK"
        Ret = 259
        While (Ret = 259)
            WaitForSingleObject pinfo.hProcess, Timeout
            DoEvents
            If (GetExitCodeProcess(pinfo.hProcess, Ret) = 1) Then
                Debug.Print " GetExitCodeProcess OK"
                LanceApp = Ret
            Else
                Debug.Print " GetExitCodeProcess KO"
            End If
        Wend
    Else
        Debug.Print " CreateProcess KO : " & Err.LastDllError
    End If


End Function

 Conclusion

Utilisation :
LanceApp(App.path & "\monExe.exe", "arg1 arg2", INFINITE, SW_NORMAL, IDLE_PRIORITY_CLASS)

Bonne Prog

@++
Crazyht


 Sources du même auteur

Source avec Zip Source .NET (Dotnet) DEMO BACKGROUNDWORKER & PING
Source avec Zip CLASSE SOMME MD5 EN VB6
MSN LOGGER
EST-ON EN MODE DEBUG (VB6)
Source avec Zip Source avec une capture Source .NET (Dotnet) CONTROLE DE SAISIE DES IP EN VB.NET

 Sources de la même categorie

Source avec Zip Source .NET (Dotnet) .NET DEPENDENCY VIEWER : ARBRE DES DÉPENDANCES D'UN ASSEMBLY... par ShareVB
Source avec Zip Source .NET (Dotnet) UTILITAIRE SKYDRIVE par MasterShadows
Source avec Zip ROTATION RAPIDE D'IMAGE par trex70
Source avec Zip Source avec une capture ENUMERATION DES PORTS TCP ET IDENTIFCATION DU PROCESS (PID) ... par Renfield
Source avec Zip Source avec une capture MOUSE SPEED AND WEIGHT : RETOUR DE FORCE VIRTUEL ! par ScSami

Commentaires et avis

Commentaire de deedje le 01/08/2003 17:12:05

Voilà une source qu'il suffit de copier-coller et qurtout qui marche et encore mieux qui fait ce que j'essaie de faire depuis quelques temps déjà...

Cependant, je ne mets "que" 8/10 parce que Y'en a marre des codes pas commentés : comment voulez-vous qu'on apprenne s'il n'y a pas un minimum de commentaire sur lesquels s'appuyer ?!

Merci

Commentaire de DeadlyPredator le 29/05/2004 07:18:13

BEN LÀ. ATTEND TOI PAS À AVOIR UN CODE SERVI SUR UN PLATEAU D'ARGENT. IL FAUT FAIRE DES EFFORTS POUR COMPRENDRE. Si il y a toujours des commentaires, on apprend pas à chercher la réponse soi-même.

Commentaire de Pilpot le 15/11/2004 20:05:06

LanceApp(App.path & "\hello.exe", "arg1 arg2", INFINITE, SW_NORMAL, IDLE_PRIORITY_CLASS)
Tout d'abord merci pour ton code mais il ne marche pas chez moi. Je suis vraiment un N00b en vb... Il me mets "Expected : =" ou "Syntax Error" !

Meric pour votre aide !!

Commentaire de Crazyht le 15/11/2004 23:08:15 administrateur CS

Avec quelle version de VB essaies tu d'utiliser la méthode ?

Commentaire de Pilpot le 16/11/2004 17:44:14

avec VB6 sp6

+++

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,671 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales