// WinExecAndWait32能满足你的要求,要是只是想判断,可以用第二个函数.适当修改一下就可以了.<br><br>FUNCTION WinExecAndWait32(FileName: STRING; Visibility: integer): longint;<br>VAR<br> zAppName: ARRAY[0..512] OF char;<br> zCurDir: ARRAY[0..255] OF char;<br> WorkDir: STRING;<br> StartupInfo: TStartupInfo;<br> ProcessInfo: TProcessInformation;<br>// LPDWORD: lpExitCode;<br>BEGIN<br> StrPCopy(zAppName, FileName);<br> GetDir(0, WorkDir);<br> StrPCopy(zCurDir, WorkDir);<br> FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br> StartupInfo.cb := Sizeof(StartupInfo);<br> StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br> StartupInfo.wShowWindow := Visibility;<br> IF NOT CreateProcess(NIL,<br> zAppName, { pointer to command line string }<br> NIL, { pointer to process security attributes}<br> NIL, { pointer to thread security attributes }<br> false, { handle inheritance flag }<br> CREATE_NEW_CONSOLE OR { creation flags }<br> NORMAL_PRIORITY_CLASS,<br> NIL, { pointer to new environment block }<br> NIL, { pointer to current directory name }<br> StartupInfo, { pointer to STARTUPINFO }<br> ProcessInfo) THEN<br> Result := -1 { pointer to PROCESS_INF }<br> ELSE<br> BEGIN<br> WaitforSingleObject(ProcessInfo.hProcess, INFINITE);<br> // GetExitCodeProcess(ProcessInfo.hProcess, nil);<br> END;<br>END;<br><br>function TfrmMain.Execute(ComLine: string): boolean;<br>var<br> lpAppName: pchar;<br> lpTitle: Pchar;<br> StartInfo: TStartupInfo;<br> FProcessInfo: TProcessInformation;<br> Maxpath: integer;<br>begin<br> if (Length(ComLine) + 2) > 255 then<br> begin<br> // SetError(-1, 'Command Line Too Long!');<br> Result := false;<br> exit;<br> end;<br>// Size := FileSize(F);<br>// MaxPath:=sizeof(lpAppName);<br> GetMem(lpAppName, MaxPath);<br> GetMem(lpTitle, MaxPath);<br><br> StrPCopy(lpAppName, ComLine);<br> StrPCopy(lpTitle, ComLine);<br> //init StartInformation<br> StartInfo.cb := sizeof(TStartupInfo);<br> StartInfo.lpReserved := nil;<br> StartInfo.lpDesktop := nil;<br> StartInfo.lpTitle := lpTitle;<br> StartInfo.dwFillAttribute := 0;<br> StartInfo.cbReserved2 := 0;<br> StartInfo.lpReserved2 := nil;<br> //这个参数控制Create Window形态<br> //STARTF_USESHOWWINDOW 指定这个标志位,指示用ShowWindow的参数建立窗口<br> StartInfo.dwFlags := STARTF_USESHOWWINDOW;<br> // StartInfo.wShowWindow := FWinStyle;<br><br> //CreateProcess 在Windows.pas中的一个原形<br> //function CreateProcessA(lpApplicationName: PAnsiChar; lpCommandLine: PAnsiChar;<br> //lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;<br> //bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer;<br> //lpCurrentDirectory: PAnsiChar; const lpStartupInfo: TStartupInfo;<br> //var lpProcessInformation: TProcessInformation): BOOL; stdcall;<br><br> //Clear ProcessInfo Structure<br> FProcessInfo.hProcess := 0;<br> FProcessInfo.hThread := 0;<br> FProcessInfo.dwProcessId := 0;<br> FProcessInfo.dwThreadId := 0;<br><br> //Create process<br> if CreateProcess(nil,<br> lpAppName,<br> nil,<br> nil,<br> False,<br> 0,<br> nil,<br> nil,<br> StartInfo,<br> FProcessInfo) then<br> begin //Command Executed<br> //为进程建立可查询的属性。<br> FAppHandle := OpenProcess(PROCESS_QUERY_INFORMATION, False, FProcessInfo.dwProcessId);<br> //建立可查询的属性后可用于GetExitCodeProcess,在Delphi中查询外部程序是<br> //否还在运行,这个函数在W95,W98,NT40下均可用.<br> //注意:FAppHandle要改成Form的变量,以便其它函数可以使用.<br> // GetExitCodeProcess(FAppHandle,FAppState);<br> // if FAppState<>STILL_ACTIVE then<br> // 外部程序已结束<br> // else<br> // 外部程序还在运行<br> end<br> else //false Create Process;<br> begin<br> // SetError(-2, 'Can not create process!');<br> Result := false;<br> end;<br><br> FreeMem(lpAppName);<br> FreeMem(lpTitle);<br>end;<br><br><br>procedure TfrmMain.Button3Click(Sender: TObject);<br>var<br> FAppState: DWORD;<br>begin<br> GetExitCodeProcess(FAppHandle, FAppState);<br> if FAppState <> STILL_ACTIVE then<br> caption := ' 外部程序已结束'<br> else<br> caption := ' 外部程序还在运行';<br> showmessage(inttostr(FAppHandle));<br>end;<br>