delphi cmd

//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;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值