當前位置:編程學習大全網 - 網絡軟體 - delphi 運行cmd 返回

delphi 運行cmd 返回

妳是想得到控制臺的信息吧 可以看壹下例程這個涉及到管道的API

procedure?RunDosInMemo(const?DosApp:?string;?AMemo:?TMemo);

const

{設置ReadBuffer的大小}

ReadBuffer?=?2400;

var

Security:?TSecurityAttributes;

ReadPipe,?WritePipe:?THandle;

start:?TStartUpInfo;

ProcessInfo:?TProcessInformation;

Buffer:?PChar;

BytesRead:?DWord;

Buf:?string;

begin

with?Security?do

begin

nlength?:=?SizeOf(TSecurityAttributes);

binherithandle?:=?true;

lpsecuritydes?criptor?:=?nil;

end;

{創建壹個命名管道用來捕獲console程序的輸出}

if?Createpipe(ReadPipe,?WritePipe,?@Security,?0)?then

begin

Buffer?:=?AllocMem(ReadBuffer?+?1);

FillChar(Start,?Sizeof(Start),?#0)

{設置console程序的啟動屬性}

with?start?do

begin

cb?:=?SizeOf(start);

start.lpReserved?:=?nil;

lpDesktop?:=?nil;

lpTitle?:=?nil;

dwX?:=?0;

dwY?:=?0;

dwXSize?:=?0;

dwYSize?:=?0;

dwXCountChars?:=?0;

dwYCountChars?:=?0;

dwFillAttribute?:=?0;

cbReserved2?:=?0;

lpReserved2?:=?nil;

hStdOutput?:=?WritePipe;?//將輸出定向到我們建立的WritePipe上

hStdInput?:=?ReadPipe;?//將輸入定向到我們建立的ReadPipe上

hStdError?:=?WritePipe;//將錯誤輸出定向到我們建立的WritePipe上

dwFlags?:=?STARTF_USESTDHANDLES?or?STARTF_USESHOWWINDOW;

wShowWindow?:=?SW_HIDE;//設置窗口為hide

end;

try

{創建壹個子進程,運行console程序}

if?CreateProcess(nil,?PChar(DosApp),?@Security,?@Security,?true,

NORMAL_PRIORITY_CLASS,

nil,?nil,?start,?ProcessInfo)?then

begin

{等待進程運行結束}

WaitForSingleObject(ProcessInfo.hProcess,?INFINITE);

{關閉輸出...開始沒有關掉它,結果如果沒有輸出的話,程序死掉了。}

CloseHandle(WritePipe);

Buf?:=?'';

{讀取console程序的輸出}

repeat

BytesRead?:=?0;

ReadFile(ReadPipe,?Buffer[0],?ReadBuffer,?BytesRead,?nil);

Buffer[BytesRead]?:=?#0;

OemToAnsi(Buffer,?Buffer);

Buf?:=?Buf?+?string(Buffer);

until?(BytesRead?<?ReadBuffer);

SendDebug(Buf);

{按照換行符進行分割,並在Memo中顯示出來}

while?pos(#10,?Buf)?>?0?do

begin

AMemo.Lines.Add(Copy(Buf,?1,?pos(#10,?Buf)?-?1));

Delete(Buf,?1,?pos(#10,?Buf));

end;

end;

finally

FreeMem(Buffer);

CloseHandle(ProcessInfo.hProcess);

CloseHandle(ProcessInfo.hThread);

CloseHandle(ReadPipe);

end;

end;

end;

  • 上一篇:outlook郵箱發不出去郵件怎麽辦
  • 下一篇:盛夏晚晴天小說裏夏晚晴和莫淩天發生關系了,離婚後?為什麽?
  • copyright 2024編程學習大全網