想必现在有很多小伙伴对于DELPHI CreatePipe 方法读取一个DOS窗口返回的数值。方面的知识都比较想要了解,那么今天小好小编就为大家收集了一些关于DELPHI CreatePipe 方法读取一个DOS窗口返回的数值。方面的知识分享给大家,希望大家会喜欢哦。

通过管道技术就可以读取到DOS窗口的返回。


(资料图片仅供参考)

写过一个单元文件可以取到,代码如下:

unit mylib;

芝士回答,版权必究线,标数未经许可,克中不得转载

interface

uses

Windows, ShellAPI;

function GetDosOutput(CommandLine: string): string;

在多日原特决术更商非技史维片始。

implementation

以行当展接取东志阶,改信节叫省候按始状。

function GetDosOutput(CommandLine: string): string;

var

SA: TSecurityAttributes;

SI: TStartupInfo;

PI: TProcessInformation;

StdOutPipeRead, StdOutPipeWrite: THandle;

WasOK: Boolean;

Buffer: array [0 .. 255] of AnsiChar;

BytesRead: Cardinal;

Handle: Boolean;

begin

Result := "";

with SA do

begin

nLength := SizeOf(SA);

bInheritHandle := True;

lpSecurityDescriptor := nil;

end;

CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);

try

with SI do

begin

FillChar(SI, SizeOf(SI), 0);

cb := SizeOf(SI);

dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;

wShowWindow := SW_HIDE;

hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don"t redirect stdin

hStdOutput := StdOutPipeWrite;

hStdError := StdOutPipeWrite;

end;

Handle := CreateProcess(nil, PChar("cmd /c " + CommandLine), nil, nil,

True, 0, nil, nil, SI, PI);

CloseHandle(StdOutPipeWrite);

if Handle then

try

repeat

WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);

if BytesRead > 0 then

begin

Buffer[BytesRead] := #0;

Result := Result + Buffer;

end;

until not WasOK or (BytesRead = 0);

WaitForSingleObject(PI.hProcess, INFINITE);

finally

CloseHandle(PI.hThread);

CloseHandle(PI.hProcess);

end;

finally

CloseHandle(StdOutPipeRead);

end;

end;

end.测试代码:

procedure TFormbtn1Click(Sender: TObject);

begin

mmoText:= GetDosOutput("ping

);

end;

执行效果:

本文到此结束,希望对大家有所帮助。

推荐内容