我想在程序中执行另外一个可执行文件,不知用什么函数?另外哪位有没有监控某个程序是否正在运行,如果没有运行则自动启动的程序或源代码?

解决方案 »

  1.   

    shell 函数可以执行外部可执行文件
      

  2.   

    Shell是VB的函数
    API函数:
    Private 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
    Const SW_SHOWNORMAL = 1
    Private Sub Form_Load()
        ShellExecute Me.hwnd, vbNullString, "c:\1.exe", vbNullString, "", SW_SHOWNORMAL
    End Sub
    ====================
    Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
    Private Sub Form_Load()
        WinExec "Explorer.exe c:\", 10
    End Sub
      

  3.   

    你可以用FindWindow来找到窗口,前提是你必须知道这个程序窗体的类名(可以用Spy++获得)或者是程序标题,如果返回0则没有执行
      

  4.   

    Dim interval As Integer
    Dim winname1, winname2 As String * 256
    Dim apppath As String * 256Private Sub Form_Load()
      Dim nLength As Long
      winname1 = Space$(256)
      winname2 = Space$(256)
      
      nLength = GetPrivateProfileString("", "winname1", "KTVPLAY", winname1, 255, "ClientDll.ini")
      winname1 = Left$(winname1, nLength)
      nLength = GetPrivateProfileString("", "winname2", "Reboot", winname2, 255, "ClientDll.ini")
      winname2 = Left$(winname2, nLength)
      nLength = GetPrivateProfileString("", "AppPath", "D:\abc\abc.exe", apppath, 255, "ClentDll.ini")
      apppath = Left$(apppath, nLength)
      interval = GetPrivateProfileInt("", "timer", 3000, "ClientDll.ini")
      
      Timer1.interval = interval
      
      Label1.Caption = "启动程序中......"
      
      Dim ret As Long
      ret = Shell(apppath, vbMaximizedFocus)执行到这里就提示:无效的过程调用或参数.
    我是新手,大家帮我看看上面读INI文件有没有什么问题
      

  5.   

    你设断点调试过没有或者你试试下面的方式,下面是调试通过的'使用INI的方式存取文件参数
    '在d盘根目录新建 aaa.ini文件'模拟程序
    '模块中
    Option Explicit'-------------------------
    '声明
    Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long'获得设置
    Public Function MyGetSetting(Section As String, KeyName As String, DefaultValue As String) As String    Dim X As Long
        Dim Holder As String * 255    X = GetPrivateProfileString(Section, KeyName, DefaultValue, Holder, 254, "d:\aaa.ini")
        MyGetSetting = Left$(Holder, InStr(Holder, Chr$(0)) - 1)End Function'保存设置
    Public Sub MySetSetting(Section As String, KeyName As String, KeyValue As String)    Dim X As Long    X = WritePrivateProfileString(Section, KeyName, KeyValue, "d:\aaa.ini")End Sub'窗体中
    Private Sub Command1_Click()
        '保存变量
        MySetSetting "服务器名", "服务器名", "Myserver"
        MySetSetting "用户名", "用户名", "sa"
        MySetSetting "密码", "密码", "aaaa"
        MySetSetting "数据库名", "数据库名", "Mydatabase"
        MsgBox "保存成功"
    End SubPrivate Sub Command2_Click()
        '取出变量
        MsgBox MyGetSetting("服务器名", "服务器名", "")
        MsgBox MyGetSetting("用户名", "用户名", "")
        MsgBox MyGetSetting("密码", "密码", "")
        MsgBox MyGetSetting("数据库名", "数据库名", "")
    End Sub
      

  6.   

    shell很清楚了,估计就是ini文件的问题了
      

  7.   

    可以直接使用Shell函数,Shell(app.path & "\abc.exe")
      

  8.   

    将Dim ret As Long改成Dim ret As Double看看.
      

  9.   

    shell或api  shellexecutePrivate 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 hwnd -----------  Long,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框  lpOperation ----  String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它  lpFile ---------  String,想用关联程序打印或打开一个程序名或文件名  lpParameters ---  String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数  lpDirectory ----  String,想使用的完整路径  nShowCmd -------  Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数名Long,非零表示成功,零表示失败。会设置GetLastErrorPrivate Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
    Private Sub Form_Load()
        'Execute explorer.exe
        WinExec "Explorer.exe c:\", 10
    End Sub
      

  10.   

    This example uses the Shell function to run an application specified by the user. ' Specifying 1 as the second argument opens the application in 
    ' normal size and gives it the focus.
    Dim RetVal
    RetVal = Shell("C:\WINDOWS\CALC.EXE", 1)   ' Run Calculator.