从Dev C++源代码中Copy出来的例程:<br><br>function RunFile_(Cmd, WorkDir : string; Wait : boolean) : Boolean;<br>var<br> tsi: TStartupInfo;<br> tpi: TProcessInformation;<br> nRead: DWORD;<br> aBuf: Array[0..101] of char;<br> sa: TSecurityAttributes;<br> hOutputReadTmp, hOutputRead, hOutputWrite, hInputWriteTmp, hInputRead,<br> hInputWrite, hErrorWrite: THandle;<br> FOutput: String;<br>begin<br> FOutput := '';<br><br> sa.nLength := SizeOf(TSecurityAttributes);<br> sa.lpSecurityDescriptor := nil;<br> sa.bInheritHandle := True;<br><br> CreatePipe(hOutputReadTmp, hOutputWrite, @sa, 0);<br> DuplicateHandle(GetCurrentProcess(), hOutputWrite, GetCurrentProcess(),<br> @hErrorWrite, 0, true, DUPLICATE_SAME_ACCESS);<br> CreatePipe(hInputRead, hInputWriteTmp, @sa, 0);<br><br> // Create new output read handle and the input write handle. Set<br> // the inheritance properties to FALSE. Otherwise, the child inherits<br> // the these handles; resulting in non-closeable handles to the pipes<br> // being created.<br> DuplicateHandle(GetCurrentProcess(), hOutputReadTmp, GetCurrentProcess(),<br> @hOutputRead, 0, false, DUPLICATE_SAME_ACCESS);<br> DuplicateHandle(GetCurrentProcess(), hInputWriteTmp, GetCurrentProcess(),<br> @hInputWrite, 0, false, DUPLICATE_SAME_ACCESS);<br> CloseHandle(hOutputReadTmp);<br> CloseHandle(hInputWriteTmp);<br><br> FillChar(tsi, SizeOf(TStartupInfo), 0);<br> tsi.cb := SizeOf(TStartupInfo);<br> tsi.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;<br> tsi.hStdInput := hInputRead;<br> tsi.hStdOutput := hOutputWrite;<br> tsi.hStdError := hErrorWrite;<br> tsi.wShowWindow := SW_SHOW;<br><br> CreateProcess(nil, PChar(Cmd), @sa, @sa, true, 0, nil, PChar(WorkDir),<br> tsi, tpi);<br> CloseHandle(hOutputWrite);<br> CloseHandle(hInputRead );<br> CloseHandle(hErrorWrite);<br> Application.ProcessMessages;<br><br> if Wait then<br> repeat<br> if (not ReadFile(hOutputRead, aBuf, 16, nRead, nil)) or (nRead = 0) then<br> begin //命令行的输出被读到 aBuf 中了<br> if GetLastError = ERROR_BROKEN_PIPE then Break<br> else MessageDlg('Pipe read error, could not execute file', mtError, [mbOK], 0);<br> end;<br> aBuf[nRead] := #0;<br> FOutput := FOutput + PChar(@aBuf[0]);<br> Application.ProcessMessages;<br> until False;<br><br> Result := GetExitCodeProcess(tpi.hProcess, nRead) = True;<br>end;<br> 具体的工作原理我也不太清楚,好象关键是利用管道。Dev C++ 中使用了类似的例程调用<br>外部的 Mingw C/C++ 命令行编译器对C/C++程序进行编译。推荐你到 www.bloodshed.net去<br>下载它的Delphi源程序,是基于GPL开放源码的。