我知道可以用Shell启动程序,怎么才能等待程序结束呢?

解决方案 »

  1.   

    Option Explicit
        
    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
        
    Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessID As Long
      dwThreadID As Long
    End Type
        
    Global Const NORMAL_PRIORITY_CLASS = &H20&
    Global Const INFINITE = -1&
    Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
    Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    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
         
        
    Public Sub ShellAndWait(cmdline$, Optional bWait As Boolean = True)
      Dim NameOfProc As PROCESS_INFORMATION
      Dim NameStart As STARTUPINFO
      Dim X As Long
           
      On Error Resume Next
      NameStart.CB = Len(NameStart)
      X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
      If bWait Then
        X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)  ' INFINITE: 无限等待!
      End If
      X = CloseHandle(NameOfProc.hProcess)
    End Sub
      

  2.   

    不用那么麻烦的,用api的两个函数就可以了。下面是我用delphi写的,你用vb转一下吧,主要是用到了api的ShellExecuteEx(@ShExecInfo);WaitForSingleObject(ShExecInfo.hProcess,INFINITE);这两个过程。ShExecInfo.cbSize:= sizeof(SHELLEXECUTEINFO);
          ShExecInfo.fMask:= SEE_MASK_NOCLOSEPROCESS;
          //ShExecInfo.Wnd:= HWND;
          ShExecInfo.lpVerb:= nil;
          ShExecInfo.lpFile:=Pchar(LocalPath+'\'+ListLocalFile.Items[ListBox1.ItemIndex]);
          //ShExecInfo.lpFile:=('c:\test.ini');
          ShExecInfo.lpParameters:='';
          ShExecInfo.lpDirectory:= nil;
          ShExecInfo.nShow:=SW_SHOW;
          ShExecInfo.hInstApp :=0;
          ShellExecuteEx(@ShExecInfo);
          WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
      

  3.   

    对了,要说明一下,这两个过程对word特别慢,就是说当word编辑结束后,它还要等好久才退出,不知道为什么,对其他程序都没问题。