小弟在线快一天了!现在这么多高手在线,帮忙了!(命令参数传递,结果送往一个文件)分不够可以再加! (100分)

J

jbas

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠看看下面的代码,怎么总有问题呢?不能把结果输出到b.txt中,我从a.txt中取数据
procedure Redirect(const aExecuteFile:string;
const aInputFile :string;
const aOutputFile :string);

var
SI: TStartupInfo;
PI: TProcessInformation;
hInput, hOutput : THandle;
WorkDir: string;
bOk: Boolean;
begin

hInput := FileOpen(aInputFile, fmOpenRead);
hOutput:= FileCreate(aOutputFile);

try
// Make child process use StdOutPipeWrite as standard out,
// and make sure it does not show on screen.
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := hInput;
hStdOutput:= hOutput;
hStdError := hOutput;
end;

// launch the command line compiler
WorkDir := ExtractFilePath(aExecuteFile);
bOK := CreateProcess(nil, PChar(aExecuteFile), nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);

try
// if process could be created then handle its output
if not bOK then raise Exception.Create('Could not run ' + aExecuteFile);
WaitForSingleObject(PI.hProcess, INFINITE);

finally
// Close all remaining handles
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;

finally
FileClose(hInput);
FileClose(hOutput);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
redirect('d:/delphi/practice/ping.exe','d:/delphi/practice/a.txt'
,'d:/delphi/practice/b.txt');
end;
 
我这里没有用管道,是否要用到呢?高手们,帮忙呀!
 
uses shellapi;
Shellexecute(...,'d:/delphi/practice/ping.exe > d:/delphi/practice/a.txt',...);
 
大侠理解错我的意思了。
a.txt里面是ping的参数如:localhost,www.delphibbs.com.既实现ping localhost的功能。
b.txt是要保存ping的结果.
我不想要中间文件的产生,请帮忙了!
 
你的读写过程在哪???起码也要用Stream来读吧!
你留下Email,我寄一个Redirect给你 ^_^
 
谢谢大侠了!
jbas@163.com
快熄灯了,谢谢了!hurry!
 
刚刚发送了!去接收吧!
 
大侠,好像还有问题呀!
编译不通过,memo1.Lines.Add(Copy(buf,1,pos(#13#10,Buf)-1)); 这句。
我想要的是这种形式,redirect('ping.exe','a.txt','b.txt')
a.txt里有要读取的数据,如www.delphibbs.com
b.txt输出ping后的结果.
不想生成中间文件,高手们帮帮忙了!
YB_unique大侠,你看我哪里没有写好呀,帮小弟一把,帮人帮到底吗!谢谢了!
小弟对管道不大清楚,有谁能讲讲它的作用,以及能用的地方吗?谢谢了!

unit Unitdos3;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
procedure ReDirect(filename:String);

implementation

{$R *.dfm}
procedure ReDirect(filename:String);
var
StartupInfo:TStartupInfo;
pi:pROCESS_INFORMATION;
Buffer,INBuf:array[0..255] of char;
tmp:String;
InRead,InWrite,OutRead,OutWrite:THandle;
Env:array[0..255] of char;
buf:String;
BytesRead:longInt;
outstream:THandleStream;
InStream :THandleStream;
begin
if not CreatePipe(InRead,InWrite,nil,0) then
Exit;
if not CreatePipe(OutRead,OutWrite,nil,0) then
Exit;
StartupInfo.cb := sizeof(tstartupinfo);
StartupInfo.lpReserved := nil;
StartupInfo.lpDesktop := nil;
StartupInfo.lpTitle := nil;
StartupInfo.dwX := 0;
StartupInfo.dwY := 0;
StartupInfo.dwXSize := 0;
StartupInfo.dwYSize := 0;
StartupInfo.dwXCountChars := 0;
StartupInfo.dwYCountChars := 0;
StartupInfo.dwFillAttribute := 0;
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.cbReserved2 := 0;
StartupInfo.lpReserved2 := nil;
StartupInfo.hStdInput := InRead;
StartupInfo.hStdOutput := OutWrite;
StartupInfo.hStdError := OutWrite;
StartupInfo.wShowWindow := SW_Hide;
FillChar(Env,512,0);
env := 'mart=test'+#0+'Hoi=Hallo'+#0+#0;
if not CreateProcess(pchar(filename),pchar(tmp),nil,nil,false,0,@Env,nil
,StartupInfo,pi) then
begin
CloseHandle( InWrite );
CloseHandle( InRead );
CloseHandle( OutWrite );
CloseHandle( OutRead );
end else
begin
OutStream := THandleStream.Create(OutRead);
InStream := THandleStream.Create(InWrite);
InBuf := 'Lets write something to the program!'+#13#10;
InStream.Write(InBuf,SizeOf(InBuf));
WaitForSingleObject(pi.hProcess,INFINITE);
Buf := '';
while OutStream.Read(Buffer,SizeOf(Buffer)) = SizeOf(Buffer) do
begin
Buf := Buf + Buffer;
end;
while pos(#13#10,buf) > 0 do
begin
memo1.Lines.Add(Copy(buf,1,pos(#13#10,Buf)-1)); //没有通过编译
Delete(Buf,1,pos(#13#10,buf)+1);
end;
CloseHandle(pi.hProcess);
CloseHandle(InRead);
CloseHandle(OutWrite);
CloseHandle( InWrite );
CloseHandle( OutRead );
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
redirect('a.txt');
end;

end.
 
我不想看这个程序。改成这样就通过了。
你想实现什么效果?
是不是从a.txt里读要ping的地址,再将结果存在b.txt中?

另:不必要用管道。
private
procedure ReDirect(filename:String);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.ReDirect(filename:String);
var
 
影 子,大侠说的对,编译没有问题了,可是好像还没有结果!
能帮忙解决一下吗?谢谢了!
 
给你一个调用该函数的范例!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;

type
PMEnv=^TMEnv;
TMEnv=array[0..0] of char;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
StatusBar1: TStatusBar;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
OutStream,InStream:THandleStream;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure StrToEnv(var env:pMEnv; str:String);
var i:Integer;
begin
GetMem(Env,Length(Str)+1);
for i := 0 to Length(Str) do
begin
env := str;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var si:TStartupInfo;
pi:pROCESS_INFORMATION;
Buffer,INBuf:array[0..255] of char;
tmp:String;
InRead,InWrite,OutRead,OutWrite:THandle;
Env:array[0..255] of char;
buf:String;
BytesRead:longInt;
begin
tmp := 'c:/windows/ping.exe';

if not CreatePipe(InRead,InWrite,nil,0) then StatusBar1.Panels[0].text := 'Could not create output pipe!';
if not CreatePipe(OutRead,OutWrite,nil,0) then StatusBar1.Panels[0].text := 'Could not create input pipe!';
Memo1.Lines.Add('Pipe''s created...');
si.cb := sizeof(tstartupinfo);
si.lpReserved := nil;
si.lpDesktop := nil;
si.lpTitle := nil;
si.dwX := 0;
si.dwY := 0;
si.dwXSize := 0;
si.dwYSize := 0;
si.dwXCountChars := 0;
si.dwYCountChars := 0;
si.dwFillAttribute := 0;
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.cbReserved2 := 0;
si.lpReserved2 := nil;
si.hStdInput := InRead;
si.hStdOutput := OutWrite;
si.hStdError := OutWrite;
si.wShowWindow := SW_Hide;
FillChar(Env,512,0);
env := 'mart=test'+#0+'Hoi=Hallo'+#0+#0;
if not CreateProcess(pchar(tmp),pchar(tmp),nil,nil,false,0,@Env,nil,si,pi) then
begin
Memo1.lines.add('CreateProcess() failt');
CloseHandle( InWrite );
CloseHandle( InRead );
CloseHandle( OutWrite );
CloseHandle( OutRead );
end else
begin
OutStream := THandleStream.Create(OutRead);
InStream := THandleStream.Create(InWrite);
InBuf := 'Well, will it work?'+#13#10;
InStream.Write(InBuf,SizeOf(InBuf));

WaitForSingleObject(pi.hProcess,INFINITE);

Buf := '';
while OutStream.Read(Buffer,SizeOf(Buffer)) = SizeOf(Buffer) do
begin
Buf := Buf + Buffer;
end;

While pos(#13#10,buf) > 0 do
begin
Memo1.Lines.Add(Copy(buf,1,pos(#13#10,Buf)-1));
Delete(Buf,1,pos(#13#10,buf)+1);
end;

CloseHandle(pi.hProcess);
CloseHandle(InRead);
CloseHandle(OutWrite);
CloseHandle( InWrite );
CloseHandle( OutRead );
end;
end;

end.
 
YB_unique老大,谢谢关注了!
可是好象运行这段程序时,程序死了。只能ctrl+F2来结束了!
我只是要用来学习的,不是再编某一个程序。在网上也查不出什么东西。所以在此请求帮助
,所以程序能简单点最好了,只要能完成这个功能就行了。谢谢了。
win2000+delphi6
 
不会啊!代码在我这运行良好!(Delphi5.0 + Win98)
 
我可什么也没改呀,在memo中显示pipe's created 就没戏了。555555555.....
大侠有简单明了的代码吗?学习用的。谢谢了!
 
dos命令执行的结果重定向到文件中
例如:
把 ping www.delphibbs.com 的结果放入a.txt
可用 c:> ping www.delphibbs.com > a.txt
 
已经将完整代码寄给你了!
BytesRead:longInt;一句可以不要!
 
free_knight 谢谢关注!
你说的这样是可以的,不过有的命令执行时间很长,只能等运行完,再生成文件,再删除临时文件
那就不大好了。
 
to YB_unique,
不行呀,还是再出现pipe's created后就没有响应了
按下ctrl+alt+del后显示这个程序没有响应。
win2000专业版+delphi6.0企业版
 
我与你的配置一样。
测试了一下,下面两条语句好象没有执行一样。
winexec('ping www.delphibbs.com>e:/b.txt',0);
//shellExecute(handle,'open','ping www.delphibbs.com>e:/b.txt',nil,nil,SW_HIDE);

谁能给我解释吗?
 
要写成这样:
ShellExecute(Handle, 'open', 'ping', 'www.delphibbs.com >> e:/b.txt', nil, 0);
to jbas:
其实最简单的办法就是上面这个用ShellExecute的方式了
当然用Redirect提供的功能也可以完成得很好。
 
顶部