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

[delphi] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //K8执行DOS并返回结果  
  2. function RunDosCommand(Command: string): string;  
  3. var  
  4.   hReadPipe: THandle;  
  5.   hWritePipe: THandle;  
  6.   SI: TStartUpInfo;  
  7.   PI: TProcessInformation;  
  8.   SA: TSecurityAttributes;  
  9.   //     SD   :   TSecurityDescriptor;  
  10.   BytesRead: DWORD;  
  11.   Dest: array[0..1023] of char;  
  12.   CmdLine: array[0..512] of char;  
  13.   TmpList: TStringList;  
  14.   Avail, ExitCode, wrResult: DWORD;  
  15.   osVer: TOSVERSIONINFO;  
  16.   tmpstr: string;  
  17. begin  
  18.   osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);  
  19.   GetVersionEX(osVer);  
  20.   
  21.   if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then  
  22.   begin  
  23.   //         InitializeSecurityDescriptor(@SD,   SECURITY_DESCRIPTOR_REVISION);  
  24.   //         SetSecurityDescriptorDacl(@SD,   True,   nil,   False);  
  25.     SA.nLength := SizeOf(SA);  
  26.     SA.lpSecurityDescriptor := nil; //@SD;  
  27.     SA.bInheritHandle := True;  
  28.     CreatePipe(hReadPipe, hWritePipe, @SA, 0);  
  29.   end  
  30.   else  
  31.     CreatePipe(hReadPipe, hWritePipe, nil, 1024);  
  32.   try  
  33.     FillChar(SI, SizeOf(SI), 0);  
  34.     SI.cb := SizeOf(TStartUpInfo);  
  35.     SI.wShowWindow := SW_HIDE;  
  36.     SI.dwFlags := STARTF_USESHOWWINDOW;  
  37.     SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;  
  38.     SI.hStdOutput := hWritePipe;  
  39.     SI.hStdError := hWritePipe;  
  40.     StrPCopy(CmdLine, Command);  
  41.     if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then  
  42.     begin  
  43.       ExitCode := 0;  
  44.       while ExitCode = do  
  45.       begin  
  46.         wrResult := WaitForSingleObject(PI.hProcess, 500);  
  47.   //                 if   PeekNamedPipe(hReadPipe,   nil,   0,   nil,   @Avail,   nil)   then  
  48.         if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then  
  49.         begin  
  50.           if Avail > then  
  51.           begin  
  52.             TmpList := TStringList.Create;  
  53.             try  
  54.               FillChar(Dest, SizeOf(Dest), 0);  
  55.               ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);  
  56.               TmpStr := Copy(Dest, 0, BytesRead - 1);  
  57.               TmpList.Text := TmpStr;  
  58.               Result := tmpstr;  
  59.             finally  
  60.               TmpList.Free;  
  61.             end;  
  62.           end;  
  63.         end;  
  64.         if wrResult <> WAIT_TIMEOUT then ExitCode := 1;  
  65.       end;  
  66.       GetExitCodeProcess(PI.hProcess, ExitCode);  
  67.       CloseHandle(PI.hProcess);  
  68.       CloseHandle(PI.hThread);  
  69.     end;  
  70.   finally  
  71.     CloseHandle(hReadPipe);  
  72.     CloseHandle(hWritePipe);  
  73.   end;  
  74. end;  
  75.   
  76. //002  
  77. function RunCommand(const cmd: string): string;  
  78. var  
  79.   hReadPipe,hWritePipe:THandle;  
  80.   si:STARTUPINFO;  
  81.   lsa:SECURITY_ATTRIBUTES;  
  82.   pi:PROCESS_INFORMATION;  
  83.   cchReadBuffer:DWORD;  
  84.   pOutStr, pCMD:PChar;  
  85.   res, strCMD:string;  
  86. begin  
  87.   strcmd := 'cmd.exe /k ' + cmd;  
  88.   pOutStr := AllocMem(5000);  
  89.   lsa.nLength := SizeOf(SECURITY_ATTRIBUTES);  
  90.   lsa.lpSecurityDescriptor := nil;  
  91.   lsa.bInheritHandle := True;  
  92.   if not CreatePipe(hReadPipe, hWritePipe, @lsa, 0) then Exit;  
  93.   FillChar(si, SizeOf(STARTUPINFO), 0);  
  94.   si.cb:=sizeof(STARTUPINFO);  
  95.   si.dwFlags:=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);  
  96.   si.wShowWindow:=SW_HIDE;  
  97.   si.hStdOutput:=hWritePipe;  
  98.   
  99.   if not CreateProcess(nil, PChar(strCMD), nil, nil, true, 0, nil, nil, si, pi) then Exit;  
  100.   while(true) do  
  101.   begin  
  102.     if not PeekNamedPipe(hReadPipe, pOutStr, 1, @cchReadBuffer, nil, nil) then break;  
  103.     if cchReadBuffer <> then  
  104.     begin  
  105.       if not ReadFile(hReadPipe, pOutStr^, 4096, cchReadBuffer, nil) then break;  
  106.       pOutStr[cchReadbuffer]:=chr(0);  
  107.       //if @Show <> nil then Show(pOutStr);  
  108.       res := res + pOutStr;  
  109.     end else if(WaitForSingleObject(pi.hProcess ,0) = WAIT_OBJECT_0) then break;  
  110.     Sleep(10);  
  111.     Application.ProcessMessages;  
  112.   end;  
  113.   pOutStr[cchReadBuffer]:=chr(0);  
  114.   
  115.   CloseHandle(hReadPipe);  
  116.   CloseHandle(pi.hThread);  
  117.   CloseHandle(pi.hProcess);  
  118.   CloseHandle(hWritePipe);  
  119.   FreeMem(pOutStr);  
  120.   Result := res;  
  121. end;  
  122.   
  123.   
  124. //003  
  125. procedure CmdExecAndView(FileName: string; memo: TMemo);  
  126.   procedure _AddInfo(mmInfo:TMemo; S: string; var line: string);  
  127.   var  
  128.     i, p: Integer;  
  129.   begin  
  130.     if mmInfo.Lines.Count > 800 then  
  131.       mmInfo.Lines.Clear;  
  132.     //去掉    
  133.     for i := to Length(S) - do  
  134.       if S[i] = #13 then S[i] := ' ';  
  135.     line := line + S;  
  136.     //   断行  
  137.     p := Pos(#10, line);  
  138.     if p > then  
  139.     begin  
  140.       //   前面的加入一行,后面的留到下次  
  141.       mmInfo.Lines.Add(Copy(line, 1, p - 1));  
  142.       line := Copy(line, p + 1, Length(line) - p);  
  143.     end;  
  144.   end;  
  145. var  
  146.   hReadPipe, hWritePipe: THandle;  
  147.   si: STARTUPINFO;  
  148.   lsa: SECURITY_ATTRIBUTES;  
  149.   pi: PROCESS_INFORMATION;  
  150.   cchReadBuffer: DWORD;  
  151.   ph: PChar;  
  152.   fname: PChar;  
  153.   line: string;  
  154. begin  
  155.   fname := allocmem(1024);  
  156.   ph := AllocMem(1024);  
  157.   lsa.nLength := sizeof(SECURITY_ATTRIBUTES);  
  158.   lsa.lpSecurityDescriptor := nil;  
  159.   lsa.bInheritHandle := True;  
  160.   if CreatePipe(hReadPipe, hWritePipe, @lsa, 0) = false then  
  161.     Exit;  
  162.   fillchar(si, sizeof(STARTUPINFO), 0);  
  163.   si.cb := sizeof(STARTUPINFO);  
  164.   si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);  
  165.   si.wShowWindow := SW_HIDE;  
  166.   si.hStdOutput := hWritePipe;  
  167.   si.hStdError := hWritePipe;  
  168.   StrPCopy(fname, FileName);  
  169.   if CreateProcess(nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then  
  170.   begin  
  171.     FreeMem(ph);  
  172.     FreeMem(fname);  
  173.     Exit;  
  174.   end;  
  175.   CloseHandle(hWritePipe);  
  176.   while (true) do  
  177.   begin  
  178.     if not PeekNamedPipe(hReadPipe, ph, 1, @cchReadBuffer, nil, nil) then break;  
  179.     if cchReadBuffer <> then  
  180.     begin  
  181.       if ReadFile(hReadPipe, ph^, 512, cchReadBuffer, nil) = false then break;  
  182.       ph[cchReadbuffer] := chr(0);  
  183.       _AddInfo(memo, ph, line);  
  184.     end  
  185.     else if (WaitForSingleObject(pi.hProcess, 0) = WAIT_OBJECT_0) then break;  
  186.     Application.ProcessMessages;  
  187.     Sleep(200);  
  188.   end;  
  189.   ph[cchReadBuffer] := chr(0);  
  190.   _AddInfo(memo, ph, line);  
  191.   CloseHandle(hReadPipe);  
  192.   CloseHandle(pi.hThread);  
  193.   CloseHandle(pi.hProcess);  
  194.   FreeMem(ph);  
  195.   FreeMem(fname);  
  196. end;  
  197. //004  
  198. var  
  199.     hReadPipe,hWritePipe:THandle;  
  200.     si:STARTUPINFO;  
  201.     lsa:SECURITY_ATTRIBUTES;  
  202.     pi:PROCESS_INFORMATION;  
  203.     mDosScreen:String;  
  204.     cchReadBuffer:DWORD;  
  205.     ph:PChar;  
  206.     fname:PChar;  
  207.     i,j:integer;  
  208. begin  
  209.     fname:=allocmem(255);  
  210.     ph:=AllocMem(5000);  
  211.     lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);  
  212.     lsa.lpSecurityDescriptor :=nil;  
  213.     lsa.bInheritHandle :=True;  
  214.   
  215.     if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then  
  216.     begin  
  217.         ShowMessage('Can not create pipe!');  
  218.         exit;  
  219.     end;  
  220.     fillchar(si,sizeof(STARTUPINFO),0);  
  221.     si.cb :=sizeof(STARTUPINFO);  
  222.     si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);  
  223.     si.wShowWindow :=SW_HIDE;  
  224.     si.hStdOutput :=hWritePipe;  
  225.     StrPCopy(fname,EditFilename.text);  
  226.   
  227.     if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False  then  
  228.     begin  
  229.         ShowMessage('can not create process');  
  230.         FreeMem(ph);  
  231.         FreeMem(fname);  
  232.         Exit;  
  233.     end;  
  234.   
  235.     while(true) do  
  236.     begin  
  237.         if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;  
  238.         if cchReadBuffer<>then  
  239.         begin  
  240.             if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;  
  241.             ph[cchReadbuffer]:=chr(0);  
  242.                                 Memo1.Lines.Add(ph);  
  243.         end  
  244.         else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;  
  245.         Sleep(100);  
  246.     end;  
  247.   
  248.     ph[cchReadBuffer]:=chr(0);  
  249.   
  250.   Memo1.Lines.Add(WideCharToString(ph));  
  251.     CloseHandle(hReadPipe);  
  252.     CloseHandle(pi.hThread);  
  253.     CloseHandle(pi.hProcess);  
  254.     CloseHandle(hWritePipe);  
  255.     FreeMem(ph);  
  256.     FreeMem(fname);  
  257. end;  

http://blog.csdn.net/earbao/article/details/21160033

原文地址:https://www.cnblogs.com/findumars/p/5338766.html