Private Sub Command1_Click()
shell abc.exe
End Sub
我想为应用程序abc另开辟一个进程以及如何关闭此进程, 可是我不知道该如何处理,还请各位大侠帮帮忙。

解决方案 »

  1.   

    关注,这方面的VB可能不好实现,VB控制进程的功能是很弱的很容易出错
      

  2.   

    首先不能用shell方法来启动应用程序。
    要用创建进程的方法CreateProcess来启动进程。
    函数原型
    BOOL CreateProcess(
      LPCTSTR lpApplicationName,
                             // pointer to name of executable module
      LPTSTR lpCommandLine,  // pointer to command line string
      LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes
      LPSECURITY_ATTRIBUTES lpThreadAttributes,   // thread security attributes
      BOOL bInheritHandles,  // handle inheritance flag
      DWORD dwCreationFlags, // creation flags
      LPVOID lpEnvironment,  // pointer to new environment block
      LPCTSTR lpCurrentDirectory,   // pointer to current directory name
      LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO
      LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION
    );
     并且可以获得进程的进程句柄。取得了进程句柄,就可以对这个进程进行操作了。关闭进程可以用
    BOOL TerminateProcess(
      HANDLE hProcess, // handle to the process
      UINT uExitCode   // exit code for the process
    );
     他只需要一个进程句柄就可以了。
     
      

  3.   

    用API函数:
       1。Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
       该函数创建一个进程
       2。Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
       该函数结束一个进程
       例如:创建由“mExePath”变量指定的EXE文件的进程。返回的进程句柄保存在变量mGobProcessHandle1 中,结束进程时用到。
       mGobProcessHandle1 = OpenProcess(PROCESS_QUERY_INFORMATION, 1, Shell(mExePath))
      结束进程如下:
       TerminateProcess mGobFormHandle1, 0
      

  4.   

    这个不能用shell方法来实现
    要用进程控制的方法
    启动进程时用
    BOOL CreateProcess(
      LPCTSTR lpApplicationName,
                             // pointer to name of executable module
      LPTSTR lpCommandLine,  // pointer to command line string
      LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes
      LPSECURITY_ATTRIBUTES lpThreadAttributes,   // thread security attributes
      BOOL bInheritHandles,  // handle inheritance flag
      DWORD dwCreationFlags, // creation flags
      LPVOID lpEnvironment,  // pointer to new environment block
      LPCTSTR lpCurrentDirectory,   // pointer to current directory name
      LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO
      LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION
    );
     然后可以获得启动进程的进程句柄。
    拥有了进程句柄可以做很多的事。关闭进程时可以用
    BOOL TerminateProcess(
      HANDLE hProcess, // handle to the process
      UINT uExitCode   // exit code for the process
    );
     只要拥有进程的句柄就可以了。
      

  5.   

    Option Explicit      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 Long
             hStdInput As Long
             hStdOutput As Long
             hStdError As Long
          End Type      Private Declare Function CreateProcess Lib "kernel32" _
             Alias "CreateProcessA" _
             (ByVal lpApplicationName As String, _
             ByVal lpCommandLine As String, _
             lpProcessAttributes As Any, _
             lpThreadAttributes As Any, _
             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 OpenProcess Lib "kernel32.dll" _
             (ByVal dwAccess As Long, _
             ByVal fInherit As Integer, _
             ByVal hObject As Long) As Long      Private Declare Function TerminateProcess Lib "kernel32" _
             (ByVal hProcess As Long, _
             ByVal uExitCode As Long) As Long      Private Declare Function CloseHandle Lib "kernel32" _
             (ByVal hObject As Long) As Long      Const SYNCHRONIZE = 1048576
          Const NORMAL_PRIORITY_CLASS = &H20&      Private Sub Form_Click()
             Dim pInfo As PROCESS_INFORMATION
             Dim sInfo As STARTUPINFO
             Dim sNull As String
             Dim lSuccess As Long
             Dim lRetValue As Long         sInfo.cb = Len(sInfo)
             lSuccess = CreateProcess(sNull, _
                                     "Calc.exe", _
                                     ByVal 0&, _
                                     ByVal 0&, _
                                     1&, _
                                     NORMAL_PRIORITY_CLASS, _
                                     ByVal 0&, _
                                     sNull, _
                                     sInfo, _
                                     pInfo)         MsgBox "Calculator has been launched!"         lRetValue = TerminateProcess(pInfo.hProcess, 0&)
             lRetValue = CloseHandle(pInfo.hThread)
             lRetValue = CloseHandle(pInfo.hProcess)         MsgBox "Calculator has terminated!"
          End Sub