如何用VB程序启动另一个程序?

解决方案 »

  1.   

    不会吧!这么简单的问题!可以使用shell函数啊!
      

  2.   

    你是说在vb中调用另一个应用程序吗?
    用shell函数试试看,比如要启动window自带的计算器程序shell("c:\windwos\calc.exe")
      

  3.   

    呵呵,如果有特殊需求的话,也可以调用api函数CreateProcess ,声明如下:
    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例程:
    'This program needs a common dialog box, named CDBox
    '  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    '   and select Microsoft Common Dialog control)
    Const INFINITE = &HFFFF
    Const STARTF_USESHOWWINDOW = &H1
    Private Enum enSW
        SW_HIDE = 0
        SW_NORMAL = 1
        SW_MAXIMIZE = 3
        SW_MINIMIZE = 6
    End Enum
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    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 Byte
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    Private 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 Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
        Dim pclass As Long
        Dim sinfo As STARTUPINFO
        Dim pinfo As PROCESS_INFORMATION
        'Not used, but needed
        Dim sec1 As SECURITY_ATTRIBUTES
        Dim sec2 As SECURITY_ATTRIBUTES
        'Set the structure size
        sec1.nLength = Len(sec1)
        sec2.nLength = Len(sec2)
        sinfo.cb = Len(sinfo)
        'Set the flags
        sinfo.dwFlags = STARTF_USESHOWWINDOW
        'Set the window's startup position
        sinfo.wShowWindow = start_size
        'Set the priority class
        pclass = Priority_Class
        'Start the program
        If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
        0&, WorkDir, sinfo, pinfo) Then
            'Wait
            WaitForSingleObject pinfo.hProcess, dwMilliseconds
            SuperShell = True
        Else
            SuperShell = False
        End If
    End Function
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'Set the dialog's title
        CDBox.DialogTitle = "Choose an EXEC-File ..."
        'Error when canceled
        CDBox.CancelError = True
        'Set the dialog's filter
        CDBox.Filter = "EXEC-Files (*.exe)|*.exe|All files (*.*)|*.*"
        'Show the 'Open File'-dialog
        CDBox.ShowOpen
        'Execute the program
        SuperShell CDBox.filename, Left$(CDBox.filename, Len(CDBox.filename) - Len(CDBox.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
        End
    End Sub
    或者ShellExecute ,声明如下:
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long比较简单,就不写例子了
      

  4.   

    '如果是需要调用另一个程序,并等待另一个程序执行完成,可以通过API来实现,下面是一个简单的调用 makecab 来压缩文件的例子Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" _
        (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As LongPrivate Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
        (ByVal nBufferLength As Long, ByVal lpBuffer As String) As LongPrivate Declare Function WaitForSingleObject Lib "kernel32" _
       (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" _
       (ByVal hObject As Long) As Long
       
    Private Declare Function OpenProcess Lib "kernel32" _
       (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As LongPrivate Const INFINITE = -1&
    Private Const SYNCHRONIZE = &H100000Private Function GetTempName(ByVal TmpFilePrefix$) As String
       Dim TempFileName As String
       Dim DriveName As String * 256, TmpL&
       TmpL = GetTempPath(256, DriveName) 'App.Path
       GetTempFileName Left(DriveName, TmpL), TmpFilePrefix, 0, DriveName
       TempFileName = Left$(DriveName, InStr(DriveName, Chr(0)) - 1)
       If Dir(TempFileName) <> "" Then Kill TempFileName
       GetTempName = TempFileName
    End FunctionPrivate Function RunMakeCab(ByVal SourceF$, Optional DistinctF$ = "") As String
        Dim iTask As Long, Ret As Long, pHandle As Long, Rtext$
        If SourceF = "" Or Dir(SourceF) = "" Then
            MsgBox "目标文件:" & SourceF & " 不存在!", vbOKOnly + 48
            RunMakeCab = ""
            Exit Function
        End If
        If DistinctF = "" Then DistinctF = GetTempName("Cab_")
        Rtext = GetTempName("Rtxt_")
        iTask = Shell("c:\winnt\system32\makecab.exe /v1 " & SourceF & " " & DistinctF, vbHide) '& " > " & Rtext, vbHide)
        pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
        DoEvents
        Ret = WaitForSingleObject(pHandle, INFINITE)
        DoEvents
        Ret = CloseHandle(pHandle)
        DoEvents
        'MsgBox DistinctF
        If Dir(DistinctF) <> "" Then
            If FileLen(DistinctF) > 0 Then
                RunMakeCab = DistinctF
                Exit Function
            End If
        End If
        RunMakeCab = ""
    End Function
      

  5.   

    这也不算难题
    利用用
    shell就可以
      

  6.   

    运行vb,文件->打开……选择所有文件。找到一个可执行程序,右键点击,在弹出的菜单中选择“运行”。如此,vb就能运行其他程序了。