Question:

How to open a local .exe file in Visual Basic code?

by  |  earlier

0 LIKES UnLike

What do I type to get my application to open a different .exe file in my computer? I want it to launch a different program.

 Tags:

   Report

1 ANSWERS


  1. Option Explicit

    Public Const STARTF_USESHOWWINDOW = &H1

    Public Const SW_HIDE = 0

    Public Const SW_SHOWNORMAL = 1

    Public Const SW_SHOWMINIMIZED = 2

    Public Const SW_SHOWMAXIMIZED = 3

    Private Const NORMAL_PRIORITY_CLASS = &H20

    Private Const INFINITE = &HFFFF      '  Infinite timeout

    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 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

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    Public Sub ExecCmd(cmdline$, ParamArray arg())

        'Execute a DOS command file and optionally wait for its completion

        'The first optional arg sets whether to wait for completion - default is True (wait)

        'The second optional arg sets the execution window state - default is 0 to hide

        'Any additional parms are ignored

        Dim proc As PROCESS_INFORMATION

        Dim start As STARTUPINFO

        Dim ret&

        Dim CmdLineChk As String

        Dim Wait As Boolean

        On Local Error Resume Next

        CmdLineChk = Dir(cmdline$)

        If CmdLineChk = "" Then

            CmdLineChk = Dir(Left(cmdline$, InStr(cmdline$ & " ", " ") - 1))

            If CmdLineChk = "" Then

                If Not ExistsInPath(Left(cmdline$, InStr(cmdline$ & " ", " ") - 1)) Then

                    MsgBox "Could not execute " & cmdline$ & vbCrLf & vbCrLf & "You may not have access to execute the program.", vbOKOnly + vbCritical, "Error Accessing File"

                    End

                    Exit Sub

                End If

            End If

        End If

        ' Initialize the STARTUPINFO structure:

        start.cb = Len(start)

        start.dwFlags = &H1         'STARTF_USESHOWWINDOW

        Wait = True                                                             ' Set Wait to default (True - do wait)

        If UBound(arg) > -1 Then If IsMissing(arg(0)) Then Else Wait = arg(0)   ' Set Wait to parm value

        start.wShowWindow = SW_HIDE                                                         ' Set window state to default (Hidden)

        If UBound(arg) > 0 Then If IsMissing(arg(1)) Then Else start.wShowWindow = arg(1)   ' Set window state parm value

        ' Start the shelled application:

        ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

        If Wait = True Then

            ' Wait for the shelled application to finish:

            ret& = WaitForSingleObject(proc.hProcess, INFINITE)

            ret& = CloseHandle(proc.hProcess)

        End If

    End Sub

    Public Function ExistsInPath(ByVal sFile As String) As Boolean

        'Returns true if the file can be found in any directory listed in Environ$("PATH")

        Dim sPath, sFilePath, bRslt

        sPath = Environ$("PATH")

        Call Parse("/d<;>", sPath, sFilePath, sPath)

        While bRslt = False And (Len(sFilePath) > 0 Or Len(sPath) > 0)

            If Len(sFilePath) > 0 Then

                If Right$(sFilePath, 1) <> "\" Then sFilePath = sFilePath + "\"

                bRslt = Exists(sFilePath + sFile)

            End If

            If Not bRslt Then Call Parse("/d<;>", sPath, sFilePath, sPath)

        Wend

        ExistsInPath = bRslt

    End Function

    'Will work if pasted into a module.  The sub to call is ExecCmd and it contains comments describing its input parameters.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.