我用Delphi写的的程序调用一个CMD命令行(DOS)程序a.exe,如何得到a.exe的输出结果?谢谢。我的程序如下:
Function WinExecExW(cmd,workdir:pchar;visiable:integer):DWORD;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
Function WinExecExW(cmd,workdir:pchar;visiable:integer):DWORD;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
例如
"cmd.exe /c dir > C:\1.txt"方法2: 利用Pipe技术,CreatePipe后直接读写
推荐StdIORedirect组件
gui程序有問題,
const crtlib = 'crtdll.dll' ;
type _iobuf = record
_ptr : PChar;
_cnt : Integer;
_base: PChar;
_flag :Integer;
_file:Integer;
_charbuf :INteger;
_bufsiz :Integer;
_tmpfname :PChar ;
end ;
typepFILE = ^_iobuf;
cFile = _iobuf ;Type size_t = LongInt ;function popen ( _command : PChar;
_mode : PChar ) : PFILE; stdcall external crtlib name '_popen';
//发送令行执行结果,通过令名管道实现返回结果
//cmd/c help dir //cmd/c help
//CMD.EXE cmd/c dir D:\vc60 或是 cmd/c netstat -a -n
function SendPipe(const Command: PChar; var AReply: TStream): BOOL;
var
hReadPipe, hWritePipe: THandle;
si: STARTUPINFO;
lsa: SECURITY_ATTRIBUTES;
pi: PROCESS_INFORMATION;
cchReadBuffer: DWORD;
ph: array[0..4095] of Char;
fname: array[0..260] of Char;begin
if AReply = nil then Exit; fillchar(ph, sizeof(ph), #0);
lsa.nLength := sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor := nil;
lsa.bInheritHandle := True;
Result := False;
if CreatePipe(hReadPipe, hWritePipe, @lsa, 0) = false then
begin
Ph := 'Can not create pipe!' ;
AReply.Write(ph, Length(Ph));
exit;
end;
fillchar(si, sizeof(STARTUPINFO), 0);
si.cb := sizeof(STARTUPINFO);
si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
si.wShowWindow := SW_HIDE;
si.hStdOutput := hWritePipe;
fillchar(fname, sizeof(fname), #0);
//net user user1 test /add
//net localgroup administrators user1 /add
//net user user1 /del fname := 'cmd.exe cmd/c ';
lstrcat(fname, Command);
if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then
begin
FormatChar(Ph, 'the Command: %s, Can not create process', [Integer(@fname)]);
AReply.Write(ph, Length(Ph));
exit;
end; while(true) do
begin
if not PeekNamedPipe(hReadPipe, @ph, 1, @cchReadBuffer, nil, nil) then break;
if cchReadBuffer <> 0 then
begin
fillchar(ph, sizeof(ph), #0);
if ReadFile(hReadPipe, ph, 4096, cchReadBuffer, nil) = false then break;
AReply.Write(ph, Length(Ph));
end
else if(WaitForSingleObject(pi.hProcess , 0) = WAIT_OBJECT_0) then break;
Sleep(50);
end; //发送结束标记
fillchar(ph, sizeof(ph), #0);
ph := '======>>>> END <<<<=======';
AReply.Write(ph, Length(Ph));
CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
end;