What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a program compile and run another program
Keywordscompile, run, make and run
CategoriesSoftware Engineering, Miscellany
Shell a command similar to the following to compile the target program.

    "C:\Program Files\Microsoft Visual Studio\Vb98\Vb6.exe" /MAKE "C:\VB Tools\Project1.vbp"

Wait for the compile to finish and then use Shell to execute the result.

The ShellAndWait routine shells a command and waits until it finishes.

The cmdRun_Click event handler composes a command to compile the other project and uses ShellAndWait to do the compile. It then uses ShellAndWait to run the newly created executable.

' Start the indicated program and wait for it
' to finish, hiding while we wait.
Private Sub ShellAndWait(ByVal program_name As String)
Dim process_id As Long
Dim process_handle As Long

    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, vbNormalFocus)
    On Error GoTo 0

    ' Hide.
    Me.Visible = False

    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If

    ' Reappear.
    Me.Visible = True
    Exit Sub

    MsgBox "Error running '" & program_name & _
        "'" & vbCrLf & Err.Description
End Sub

' Start the program.
Private Sub cmdRun_Click()
Const COMPILER = "C:\Program Files\Microsoft Visual " & _
Const PROJECT_NAME = "Target"
Dim project_path As String
Dim project_vbp As String
Dim project_exe As String
Dim cmd As String

    ' Get the project file name.
    project_path = App.Path
    If Right$(project_path, 1) <> "\" Then project_path = _
        project_path & "\"
    project_vbp = project_path & PROJECT_NAME & ".vbp"
    project_exe = project_path & PROJECT_NAME & ".exe"

    ' Compose the compile command.
    cmd = """" & COMPILER & """ /MAKE """ & project_vbp & _

    ' Shell this command and wait for it to finish.
    ShellAndWait cmd

    ' Execute the newly compiled program.
    cmd = """" & project_exe & """"
    ShellAndWait cmd
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.