在Delphi中调用Dos程序后会出现一个窗体,通过对WinExeC或CreateProgress中的参数可以屏蔽这个窗口的出现,但是怎样输出这个窗体的信息到一个文件中呢?
在Win98下,我先生成了一个内容为"ExeName.exe > Dest.txt"的Bat文件,然后运行这个Bat文件就可以把ExeName.exe输出到窗口的信息获取到Dest.txt中.但是在Win2000/XP中这个办法很慢而且有时会出错.在Win2000/XP中可以直接用WinExeC执行命令行"ExeName.exe > Dest.txt",可以达到同样的效果,但是在Win98下根本不行.
请问各位高人:怎样可以有一个在Win9X和Win2000/XP中都很好的办法呢?我猜是应该调用API函数CreateProgress或其他
在Win98下,我先生成了一个内容为"ExeName.exe > Dest.txt"的Bat文件,然后运行这个Bat文件就可以把ExeName.exe输出到窗口的信息获取到Dest.txt中.但是在Win2000/XP中这个办法很慢而且有时会出错.在Win2000/XP中可以直接用WinExeC执行命令行"ExeName.exe > Dest.txt",可以达到同样的效果,但是在Win98下根本不行.
请问各位高人:怎样可以有一个在Win9X和Win2000/XP中都很好的办法呢?我猜是应该调用API函数CreateProgress或其他
exe >data.txt
我不知道你们说的是什么意思,是在Bat文件中写 "ExeName.exe > Dest.txt",还是在Winexec中调用.如果是在WinExec中调用的话,正如我在提问中说的,Win98下确实不行(ExeName自己还有参数要传递),因为Win98的Dos是独立的,用WinExec来这样调用,等于给Dos程序ExeName传送了一个参数,所以不行.但是在Win2000/XP中,Dos是一个虚拟机,用WinExec调用,相当于给Dos虚拟机程序传递了一个参数,然后Dos虚拟机再截取参数,执行程序,所以在Win2000/XP中可以.
如果是在Bat文件中写的话,Win2000/XP会很慢而且不稳定,原因可能是因为它们和Dos兼容的不好.
我再试一试,看看对不对.
各位高人如果还有办法,还望多多指教,最好给个例子(ExeName.exe自己也有参数,比如:Masm c:\cc.asm /Fl 等),在下洗耳恭听.
http://www.csdn.net/develop/article/18/18338.shtm
var
hReadPipe,hWritePipe:THandle;
si:STARTUPINFO;
lsa:SECURITY_ATTRIBUTES;
pi:PROCESS_INFORMATION;
mDosScreen:String;
cchReadBuffer:DWORD;
ph:PChar;
fname:PChar;
i,j:integer;
begin
fname:=allocmem(255);
ph:=AllocMem(5000);
lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor :=nil;
lsa.bInheritHandle :=True; if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
begin
ShowMessage('Can not create pipe!');
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;
StrPCopy(fname,EditFilename.text); if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then
begin
ShowMessage('can not create process');
FreeMem(ph);
FreeMem(fname);
Exit;
end; while(true) do
begin
if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
if cchReadBuffer<>0 then
begin
if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
ph[cchReadbuffer]:=chr(0);
Memo1.Lines.Add(ph);
end
else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
Sleep(100);
end; ph[cchReadBuffer]:=chr(0);
Memo1.Lines.Add(ph);
CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
FreeMem(ph);
FreeMem(fname);
end;