e创建匿名管道
DELPHI CreatePipe 方法读取一个DOS窗口返回的数值。
通过管道技术就可以读取到磁盘操作系统窗口的返回。
写过一个单元文件可以取到,代码如下:
unitmylib
连接
Windows,ShellAPI
functiongetdos输出(命令行:字符串):字符串;
履行
functiongetdos输出(命令行:字符串):字符串;
sa :安全性属性
SI:TStartupInfo
pi :过程信息
StdOutPipeRead,StdOutPipeWrite:THandle句柄
WasOK:Boolean
Buffer:array[0.255]ofAnsiChar;
字节读取:基数
手柄:布尔型
开始
结果:="";
怀萨多
开始
nlength :=SizeOf(SA);
bInheritHandle:=True
lpSecurityDescriptor:=nil
结束;
CreatePipe(StdOutPipeRead,StdOutPipeWrite,@SA,0);
尝试
withSIdo
开始
FillChar(SI,SizeOf(SI),0);
CB :=SizeOf(SI);
dw flags :=STARTF _ USESHOWWINDOWorSTARTF _ USESTDHANDLES;
wShowWindow:=SW _ HIDE
hstd输入:=GetStdHandle(STD _ INPUT _ HANDLE);//不要tredirectstdin
hstd output :=stdoutputpiperwrite;
hstd错误:=stdoutpiperwrite
结束;
Handle:=CreateProcess(nil,PChar('cmd/c '命令行),nil,nil,
True,0,nil,nil,SI,PI);
关闭句柄(stdoutpiperwrite);
ifHandlethen
尝试
重复
wasok :=ReadFile(StdOutPipeRead,Buffer,255,BytesRead,nil);
ifBytesRead0then
开始
buffer[bytes read]:=# 0;
结果:=结果缓冲区;
结束;
untilnotWasOKor(读取的字节数=0);
WaitForSingleObject(pi。h过程,无限);
最后
关闭手柄(pi。hthread);
关闭手柄(pi。hprocess);
结束;
最后
关闭句柄(StdOutPipeRead);
结束;
结束。测试代码:
过程t表格1。btn1点击(发送者:对对象);
开始
mmo1 .text :=getdos output(' ping www。百度一下。com’);
执行效果:
过程GetDosRes(que :字符串;var RES :字符串);
常数
CUANTOBUFFER=2000
安全属性:
帕莱尔,PaEscribir手柄
开始:TStartUpInfo
过程信息:过程信息;
缓冲区:Pchar
字节读取:字
宽宽的:字
开始
使用塞古里德
ades dobegin
nlength := SizeOf(TSecurityAttributes);
binherithandle := true;
lpsecuritydescriptor := nil;
end;
if Createpipe(PaLeer,PaEscribir,@Seguridades,0) then
begin
Buffer := AllocMem(CUANTOBUFFER + 1);
try
FillChar(Start,Sizeof(Start),#0);
start.cb := SizeOf(start);
start.hStdOutput := PaEscribir;
start.hStdInput := PaLeer;
start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
start.wShowWindow := SW_HIDE;
if CreateProcess(nil,PChar(Que),@Seguridades,@Seguridades,true,NORMAL_PRIORITY_CLASS,nil, nil,start,ProcessInfo)
then
begin
repeat
CuandoSale := WaitForSingleObject( ProcessInfo.hProcess,100);
Application.ProcessMessages;
until (CuandoSale <> WAIT_TIMEOUT);
Res := '';
repeat
BytesRead := 0;
ReadFile(PaLeer,Buffer[0],CUANTOBUFFER,BytesRead,nil);
Buffer[BytesRead] := #0;
OemToAnsi(Buffer,Buffer);
Res := Res + String(Buffer);
until (BytesRead < CUANTOBUFFER);
end;
finally
FreeMem(Buffer);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(PaLeer);
CloseHandle(PaEscribir);
end;
end;
s :string;
begin
GetDosRes('Ping www.baidu.com',s);
showmessage(s);
end;function AnsiToWide(var aAString: AnsiString): string;
temp: string;
count: Integer;
begin
count := Length(aAString);
SetLength(temp, count);
MultiByteToWideChar(CP_ACP, 0, PAnsiChar(aAString), count, PChar(temp), count);
Result := temp;
function RunProgram(aExecutablePath, aArguments: string): string;
buffer: array[0..4095] of AnsiChar;
bufferStr: AnsiString;
sa: TSecurityAttributes;
hReadPipe, hWritePipe: THandle;
si: TStartupInfo;
pi: TProcessInformation;
dwExitCode: DWORD;
dwSize, dwRead, dwTemp: DWORD;
begin
FillChar(sa, SizeOf(sa), 0);
sa.nLength := SizeOf(sa);
sa.lpSecurityDescriptor := nil;
sa.bInheritHandle := True;
if CreatePipe(hReadPipe, hWritePipe, @sa, SizeOf(sa)) then
begin
FillChar(si, SizeOf(si), 0);
si.cb := SizeOf(si);
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.wShowWindow := SW_HIDE;
si.hStdInput := hReadPipe;
si.hStdOutput := hWritePipe;
si.hStdError := hWritePipe;
if (Length(aArguments) > 0) and (aArguments[1] <> ' ') then
begin
aArguments := ' ' + aArguments;
end;
if CreateProcess(PChar(aExecutablePath), PChar(aArguments), nil, nil, True, 0, nil, nil, si, pi) then
begin
while True do
begin
if GetExitCodeProcess(pi.hProcess, dwExitCode) then
begin
if dwExitCode <> STILL_ACTIVE then Break;
if PeekNamedPipe(hReadPipe, nil, 0, nil, @dwSize, nil) then
begin
dwTemp := 0;
while dwTemp < dwSize do
begin
FillChar(buffer, dwSize + 2, 0);
if ReadFile(hReadPipe, buffer, dwSize, dwRead, nil) then
begin
Inc(dwTemp, dwRead);
bufferStr := buffer;
Result := Result + AnsiToWide(bufferStr);
end
else
Break;
end;
end
else
Break;
Sleep(10);
end
else
Break;
end;
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
end;
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
end;
a = RunProgram('C:\Windows\system32\ping.exe', '-t 192.168.0.1')
delphi2010下通过if WinExec('cmd.exe /c ping 192.168.0.1 >f:\ping.txt',sw_hide)>31 then
begin
Sleep(5000);//等待文件生成
Memo1.Lines.LoadFromFile('f:\ping.txt');
end;有个API函数数可以设置内存共享区,不知道是哪个,你搜一下
或你直接读DOS程序的内存考虑使用dos命令直接将结果写入文件,然后操作文件即可
比如PING xx.xx.xx.xx >>1.txt
评论(0)