您的位置:首页 > 编程语言 > Delphi

delphi cmd(4个例子都是通过管道取得)

2016-03-30 20:34 495 查看
[delphi] view plain copy







//K8执行DOS并返回结果

function RunDosCommand(Command: string): string;

var

hReadPipe: THandle;

hWritePipe: THandle;

SI: TStartUpInfo;

PI: TProcessInformation;

SA: TSecurityAttributes;

// SD : TSecurityDescriptor;

BytesRead: DWORD;

Dest: array[0..1023] of char;

CmdLine: array[0..512] of char;

TmpList: TStringList;

Avail, ExitCode, wrResult: DWORD;

osVer: TOSVERSIONINFO;

tmpstr: string;

begin

osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);

GetVersionEX(osVer);

if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then

begin

// InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);

// SetSecurityDescriptorDacl(@SD, True, nil, False);

SA.nLength := SizeOf(SA);

SA.lpSecurityDescriptor := nil; //@SD;

SA.bInheritHandle := True;

CreatePipe(hReadPipe, hWritePipe, @SA, 0);

end

else

CreatePipe(hReadPipe, hWritePipe, nil, 1024);

try

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

SI.cb := SizeOf(TStartUpInfo);

SI.wShowWindow := SW_HIDE;

SI.dwFlags := STARTF_USESHOWWINDOW;

SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;

SI.hStdOutput := hWritePipe;

SI.hStdError := hWritePipe;

StrPCopy(CmdLine, Command);

if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then

begin

ExitCode := 0;

while ExitCode = 0 do

begin

wrResult := WaitForSingleObject(PI.hProcess, 500);

// if PeekNamedPipe(hReadPipe, nil, 0, nil, @Avail, nil) then

if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then

begin

if Avail > 0 then

begin

TmpList := TStringList.Create;

try

FillChar(Dest, SizeOf(Dest), 0);

ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);

TmpStr := Copy(Dest, 0, BytesRead - 1);

TmpList.Text := TmpStr;

Result := tmpstr;

finally

TmpList.Free;

end;

end;

end;

if wrResult <> WAIT_TIMEOUT then ExitCode := 1;

end;

GetExitCodeProcess(PI.hProcess, ExitCode);

CloseHandle(PI.hProcess);

CloseHandle(PI.hThread);

end;

finally

CloseHandle(hReadPipe);

CloseHandle(hWritePipe);

end;

end;

//002

function RunCommand(const cmd: string): string;

var

hReadPipe,hWritePipe:THandle;

si:STARTUPINFO;

lsa:SECURITY_ATTRIBUTES;

pi:PROCESS_INFORMATION;

cchReadBuffer:DWORD;

pOutStr, pCMD:PChar;

res, strCMD:string;

begin

strcmd := 'cmd.exe /k ' + cmd;

pOutStr := AllocMem(5000);

lsa.nLength := SizeOf(SECURITY_ATTRIBUTES);

lsa.lpSecurityDescriptor := nil;

lsa.bInheritHandle := True;

if not CreatePipe(hReadPipe, hWritePipe, @lsa, 0) then Exit;

FillChar(si, SizeOf(STARTUPINFO), 0);

si.cb:=sizeof(STARTUPINFO);

si.dwFlags:=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);

si.wShowWindow:=SW_HIDE;

si.hStdOutput:=hWritePipe;

if not CreateProcess(nil, PChar(strCMD), nil, nil, true, 0, nil, nil, si, pi) then Exit;

while(true) do

begin

if not PeekNamedPipe(hReadPipe, pOutStr, 1, @cchReadBuffer, nil, nil) then break;

if cchReadBuffer <> 0 then

begin

if not ReadFile(hReadPipe, pOutStr^, 4096, cchReadBuffer, nil) then break;

pOutStr[cchReadbuffer]:=chr(0);

//if @Show <> nil then Show(pOutStr);

res := res + pOutStr;

end else if(WaitForSingleObject(pi.hProcess ,0) = WAIT_OBJECT_0) then break;

Sleep(10);

Application.ProcessMessages;

end;

pOutStr[cchReadBuffer]:=chr(0);

CloseHandle(hReadPipe);

CloseHandle(pi.hThread);

CloseHandle(pi.hProcess);

CloseHandle(hWritePipe);

FreeMem(pOutStr);

Result := res;

end;

//003

procedure CmdExecAndView(FileName: string; memo: TMemo);

procedure _AddInfo(mmInfo:TMemo; S: string; var line: string);

var

i, p: Integer;

begin

if mmInfo.Lines.Count > 800 then

mmInfo.Lines.Clear;

//去掉 \r

for i := 0 to Length(S) - 1 do

if S[i] = #13 then S[i] := ' ';

line := line + S;

// \n 断行

p := Pos(#10, line);

if p > 0 then

begin

// \n 前面的加入一行,后面的留到下次

mmInfo.Lines.Add(Copy(line, 1, p - 1));

line := Copy(line, p + 1, Length(line) - p);

end;

end;

var

hReadPipe, hWritePipe: THandle;

si: STARTUPINFO;

lsa: SECURITY_ATTRIBUTES;

pi: PROCESS_INFORMATION;

cchReadBuffer: DWORD;

ph: PChar;

fname: PChar;

line: string;

begin

fname := allocmem(1024);

ph := AllocMem(1024);

lsa.nLength := sizeof(SECURITY_ATTRIBUTES);

lsa.lpSecurityDescriptor := nil;

lsa.bInheritHandle := True;

if CreatePipe(hReadPipe, hWritePipe, @lsa, 0) = false then

Exit;

fillchar(si, sizeof(STARTUPINFO), 0);

si.cb := sizeof(STARTUPINFO);

si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);

si.wShowWindow := SW_HIDE;

si.hStdOutput := hWritePipe;

si.hStdError := hWritePipe;

StrPCopy(fname, FileName);

if CreateProcess(nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then

begin

FreeMem(ph);

FreeMem(fname);

Exit;

end;

CloseHandle(hWritePipe);

while (true) do

begin

if not PeekNamedPipe(hReadPipe, ph, 1, @cchReadBuffer, nil, nil) then break;

if cchReadBuffer <> 0 then

begin

if ReadFile(hReadPipe, ph^, 512, cchReadBuffer, nil) = false then break;

ph[cchReadbuffer] := chr(0);

_AddInfo(memo, ph, line);

end

else if (WaitForSingleObject(pi.hProcess, 0) = WAIT_OBJECT_0) then break;

Application.ProcessMessages;

Sleep(200);

end;

ph[cchReadBuffer] := chr(0);

_AddInfo(memo, ph, line);

CloseHandle(hReadPipe);

CloseHandle(pi.hThread);

CloseHandle(pi.hProcess);

FreeMem(ph);

FreeMem(fname);

end;

//004

var

hReadPipe,hWritePipe:THandle;

si:STARTUPINFO;

lsa:SECURITY_ATTRIBUTES;

pi:PROCESS_INFORMATION;

mDosScreen:String;

cchReadBuffer:DWORD;

ph:PChar;

fname:PChar;

i,j:integer;

begin

fname:=allocmem(255);

ph:=AllocMem(5000);

lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);

lsa.lpSecurityDescriptor :=nil;

lsa.bInheritHandle :=True;

if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then

begin

ShowMessage('Can not create pipe!');

exit;

end;

fillchar(si,sizeof(STARTUPINFO),0);

si.cb :=sizeof(STARTUPINFO);

si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);

si.wShowWindow :=SW_HIDE;

si.hStdOutput :=hWritePipe;

StrPCopy(fname,EditFilename.text);

if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then

begin

ShowMessage('can not create process');

FreeMem(ph);

FreeMem(fname);

Exit;

end;

while(true) do

begin

if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;

if cchReadBuffer<>0 then

begin

if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;

ph[cchReadbuffer]:=chr(0);

Memo1.Lines.Add(ph);

end

else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;

Sleep(100);

end;

ph[cchReadBuffer]:=chr(0);

Memo1.Lines.Add(WideCharToString(ph));

CloseHandle(hReadPipe);

CloseHandle(pi.hThread);

CloseHandle(pi.hProcess);

CloseHandle(hWritePipe);

FreeMem(ph);

FreeMem(fname);

end;
http://blog.csdn.net/earbao/article/details/21160033
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: