想必现在有很多小伙伴对于delphicreatepipe方法读取一个dos窗口返回的数值。方面的知识都比较想要了解,那么今天小好小编就为大家收集了一些关于delphicreatepipe方法读取一个dos窗口返回的数值。方面的知识分享给大家,希望大家会喜欢哦。
通过管道技术就可以读取到dos窗口的返回。
写过一个单元文件可以取到,代码如下:
unitmylib;
芝士回答,版权必究线,标数未经许可,克中不得转载
interface
uses
windows,shellapi;
functiongetdosoutput(commandline:string):string;
在多日原特决术更商非技史维片始。
implementation
以行当展接取东志阶,改信节叫省候按始状。
functiongetdosoutput(commandline:string):string;
var
sa:tsecurityattributes;
si:tstartupinfo;
pi:tprocessinformation;
stdoutpiperead,stdoutpipewrite:thandle;
wasok:boolean;
buffer:array[0..255]ofansichar;
bytesread:cardinal;
handle:boolean;
begin
result:='';
withsado
begin
nlength:=sizeof(sa);
binherithandle:=true;
lpsecuritydescriptor:=nil;
end;
createpipe(stdoutpiperead,stdoutpipewrite,@sa,0);
try
withsido
begin
fillchar(si,sizeof(si),0);
cb:=sizeof(si);
dwflags:=startf_useshowwindoworstartf_usestdhandles;
wshowwindow:=sw_hide;
hstdinput:=getstdhandle(std_input_handle);//don'tredirectstdin
hstdoutput:=stdoutpipewrite;
hstderror:=stdoutpipewrite;
end;
handle:=createprocess(nil,pchar('cmd/c'+commandline),nil,nil,
true,0,nil,nil,si,pi);
closehandle(stdoutpipewrite);
ifhandlethen
try
repeat
wasok:=readfile(stdoutpiperead,buffer,255,bytesread,nil);
ifbytesread>0then
begin
buffer[bytesread]:=#0;
result:=result+buffer;
end;
untilnotwasokor(bytesread=0);
waitforsingleobject(pi.hprocess,infinite);
finally
closehandle(pi.hthread);
closehandle(pi.hprocess);
end;
finally
closehandle(stdoutpiperead);
end;
end;
end.测试代码:
proceduretform1.btn1click(sender:tobject);
begin
mmo1.text:=getdosoutput('ping
);
end;
执行效果:
本文到此结束,希望对大家有所帮助。