Delphi调用DOS并输出结果集
我百度了一下,可是只有代码并不是很清楚,到底是怎么实现的还不知道,希望来者可以写上必要的注释!很感谢...
我百度了一下,可是只有代码并不是很清楚,到底是怎么实现的还不知道,希望来者可以写上必要的注释!很感谢
展开
3个回答
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1:TMemo;
procedure FormClose(Sender:TObject; var Action:TCloseAction);
procedure FormCreate(Sender:TObject);
procedure ress(Sender:TObject; var Key:Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1 :TForm1;
ReadOut, WriteOut :THandle;
ReadIn, WriteIn :THandle;
ProcessInfo :TProcessInformation;
implementation
{$R *.dfm}
procedure InitConsole; {设置 console 启动属性}
var
Secu :TSecurityAttributes;
start :TStartUpInfo;
buf :array[0..MAX_PATH] of Char;
begin // MAX_PATH
with Secu do
begin
nlength := SizeOf(TSecurityAttributes);
binherithandle := true;
lpsecuritydescriptor := nil;
end;
Createpipe(ReadOut, WriteOut, @Secu, 0); {创建一个命名管道用来捕获console程序的输出}
Createpipe(ReadIn, WriteIn, @Secu, 0); {创建第二个命名管道用来捕获console程序的输入}
GetEnvironmentVariable('comspec', buf, SizeOf(buf));
ZeroMemory(@start, SizeOf(start));
start.cb := SizeOf(start);
start.hStdOutput := WriteOut;
start.hStdInput := ReadIn;
start.hStdError := WriteOut;
start.dwFlags := STARTF_USESTDHANDLES +
STARTF_USESHOWWINDOW;
start.wShowWindow := SW_HIDE;
CreateProcess(nil, buf,
@Secu, @Secu, true,
NORMAL_PRIORITY_CLASS,
nil, nil, start, ProcessInfo);
end;
function ReadFromPipe:string;
var
buf :array[0..1024] of Char;
BytesRead :DWord;
begin
Result := '';
while
BytesRead > 0 do
begin //循环读取数据。
if ReadFile(ReadOut, buf, SizeOf(buf), BytesRead, nil) then
begin
Result := Result + Copy(buf, 1, BytesRead);
WaitForSingleObject(ProcessInfo.hProcess, 10);
// Form1.Memo1.Lines.Add('a ' + inttostr(BytesRead));
end
else
break;
PeekNamedPipe(ReadOut, nil, 0, nil, @BytesRead, nil); //检查管道里是否有数据。
// Form1.Memo1.Lines.Add('b ' + inttostr(BytesRead));
end;
end;
procedure WriteCMD(Value:string);
var
len :integer;
BytesWrite :DWord;
Buffer :PChar;
begin
len := Length(Value) + 2;
Buffer := PChar(Value + #13#10); //命令与内容不出现在一行。
WriteFile(WriteIn, Buffer[0], len, BytesWrite, nil);
end;
procedure CloseConsole;
begin
TerminateProcess(ProcessInfo.hProcess, 0);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadIn);
CloseHandle(WriteIn);
CloseHandle(ReadOut);
CloseHandle(WriteOut);
end;
procedure TForm1.FormClose(Sender:TObject; var Action:TCloseAction);
begin
CloseConsole;
end;
procedure TForm1.FormCreate(Sender:TObject);
begin
InitConsole;
end;
procedure TForm1.ress(Sender:TObject; var Key:Char);
var
s, ss :string;
i, j :integer;
begin
if Key = #13 then
begin
i := Memo1.Lines.Count - 1;
s := Memo1.Lines[i];
delete(s, 1, pos('>', s));
WriteCMD(TRIM(s));
//WaitForSingleObject(ProcessInfo.hProcess, 10000);//WAIT_TIMEOUT);
sleep(1);
// ss := TRIM(ReadFromPipe);
// for j = (Memo1.Lines.Count-1) downto 0 do
// begin
// if Memo1.Lines[j] = '' then Memo1.Lines.delete(j);
// end;
Memo1.Lines.Add(TRIM(ReadFromPipe));
end;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1:TMemo;
procedure FormClose(Sender:TObject; var Action:TCloseAction);
procedure FormCreate(Sender:TObject);
procedure ress(Sender:TObject; var Key:Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1 :TForm1;
ReadOut, WriteOut :THandle;
ReadIn, WriteIn :THandle;
ProcessInfo :TProcessInformation;
implementation
{$R *.dfm}
procedure InitConsole; {设置 console 启动属性}
var
Secu :TSecurityAttributes;
start :TStartUpInfo;
buf :array[0..MAX_PATH] of Char;
begin // MAX_PATH
with Secu do
begin
nlength := SizeOf(TSecurityAttributes);
binherithandle := true;
lpsecuritydescriptor := nil;
end;
Createpipe(ReadOut, WriteOut, @Secu, 0); {创建一个命名管道用来捕获console程序的输出}
Createpipe(ReadIn, WriteIn, @Secu, 0); {创建第二个命名管道用来捕获console程序的输入}
GetEnvironmentVariable('comspec', buf, SizeOf(buf));
ZeroMemory(@start, SizeOf(start));
start.cb := SizeOf(start);
start.hStdOutput := WriteOut;
start.hStdInput := ReadIn;
start.hStdError := WriteOut;
start.dwFlags := STARTF_USESTDHANDLES +
STARTF_USESHOWWINDOW;
start.wShowWindow := SW_HIDE;
CreateProcess(nil, buf,
@Secu, @Secu, true,
NORMAL_PRIORITY_CLASS,
nil, nil, start, ProcessInfo);
end;
function ReadFromPipe:string;
var
buf :array[0..1024] of Char;
BytesRead :DWord;
begin
Result := '';
while
BytesRead > 0 do
begin //循环读取数据。
if ReadFile(ReadOut, buf, SizeOf(buf), BytesRead, nil) then
begin
Result := Result + Copy(buf, 1, BytesRead);
WaitForSingleObject(ProcessInfo.hProcess, 10);
// Form1.Memo1.Lines.Add('a ' + inttostr(BytesRead));
end
else
break;
PeekNamedPipe(ReadOut, nil, 0, nil, @BytesRead, nil); //检查管道里是否有数据。
// Form1.Memo1.Lines.Add('b ' + inttostr(BytesRead));
end;
end;
procedure WriteCMD(Value:string);
var
len :integer;
BytesWrite :DWord;
Buffer :PChar;
begin
len := Length(Value) + 2;
Buffer := PChar(Value + #13#10); //命令与内容不出现在一行。
WriteFile(WriteIn, Buffer[0], len, BytesWrite, nil);
end;
procedure CloseConsole;
begin
TerminateProcess(ProcessInfo.hProcess, 0);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadIn);
CloseHandle(WriteIn);
CloseHandle(ReadOut);
CloseHandle(WriteOut);
end;
procedure TForm1.FormClose(Sender:TObject; var Action:TCloseAction);
begin
CloseConsole;
end;
procedure TForm1.FormCreate(Sender:TObject);
begin
InitConsole;
end;
procedure TForm1.ress(Sender:TObject; var Key:Char);
var
s, ss :string;
i, j :integer;
begin
if Key = #13 then
begin
i := Memo1.Lines.Count - 1;
s := Memo1.Lines[i];
delete(s, 1, pos('>', s));
WriteCMD(TRIM(s));
//WaitForSingleObject(ProcessInfo.hProcess, 10000);//WAIT_TIMEOUT);
sleep(1);
// ss := TRIM(ReadFromPipe);
// for j = (Memo1.Lines.Count-1) downto 0 do
// begin
// if Memo1.Lines[j] = '' then Memo1.Lines.delete(j);
// end;
Memo1.Lines.Add(TRIM(ReadFromPipe));
end;
end;
end.
参考资料: http://hi.baidu.com/e%5F%5Fjin/blog/item/6582301759edd8084a90a79c.html
展开全部
Delphi中调用DOS程序并获取DOS程序的输出结果?
很简单。
执行dos程序时,加上输出改向就行了。
如:dos程序abc.exe ,调用执行时这样写: abc.exe > c:\kk.txt
则执行完abc.exe 后,会在C:\中生成一个kk.txt文件,并将其输出结果写到该文件中。然后在Delphi中读取该文件中的内容。
很简单。
执行dos程序时,加上输出改向就行了。
如:dos程序abc.exe ,调用执行时这样写: abc.exe > c:\kk.txt
则执行完abc.exe 后,会在C:\中生成一个kk.txt文件,并将其输出结果写到该文件中。然后在Delphi中读取该文件中的内容。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询