如何在Delphi中运行命令行程序?

29

我需要从 Delphi 软件中执行一个 Windows 的 "find" 命令。 我尝试使用 ShellExecute 命令,但好像不起作用。 在 C 中,我会使用 system 过程,但是在这里...我不知道该怎么办。 我想要做的事情就像这样:

System('find "320" in.txt > out.txt');

编辑:感谢回答 :) 我试图将“Find”作为可执行文件运行,而不是作为cmd.exe的参数。


2
ShellExecute 应该可以工作。它在做什么,或者没有做什么,或者其他什么问题? - Mason Wheeler
2个回答

35

使用ShellExecute()的示例:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(0, nil, 'cmd.exe', '/C find "320" in.txt > out.txt', nil, SW_HIDE);
  Sleep(1000);
  Memo1.Lines.LoadFromFile('out.txt');
end;
请注意,使用CreateProcess()而不是ShellExecute()可以更好地控制进程。
理想情况下,您还应该在辅助线程中调用此函数,并调用WaitForSingleObject()等待进程完成。示例中的Sleep()只是等待ShellExecute()启动的程序完成的一种方法 - ShellExecute()不会这样做。如果它这样做,您就不能简单地打开一个notepad实例来编辑文件,ShellExecute()将阻塞您的父应用程序,直到编辑器被关闭为止。

这句话应该是 Memo1.Lines.LoadFromFile('out.txt');,对吧? - IanH
如果窗口被隐藏了(SW_HIDE),为什么我们执行ShellExecute后要放一个Sleep(1000)? - gramm
我正在处理100MB的文本文件,所以我会选择使用WaitForSingleObject选项。谢谢! :) - gramm
2
ShellExecute()会启动应用程序,但不会等待其完成 - 否则您无法真正使用它来打开标准GUI程序。在小型文本文件上运行find可能并不重要,但对于较长时间的执行任务,您必须确保它们在处理out.txt中的结果之前已经完成。这就是为什么使用CreateProcess()并等待进程句柄是更好的方法。 - mghie
1
或者使用ShellExecuteEx,它可以与等待函数一起使用。 - Gerry Coll

21

变体1(使用“高级”CreateProcess):

这将运行一个“DOS”程序并检索其输出:

function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;  { Run a DOS program and retrieve its output dynamically while it is running. }
var
  SecAtrrs: TSecurityAttributes;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  pCommandLine: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir: string;
  Handle: Boolean;
begin
  Result := '';
  with SecAtrrs do begin
    nLength := SizeOf(SecAtrrs);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SecAtrrs, 0);
  try
    with StartupInfo do
    begin
      FillChar(StartupInfo, SizeOf(StartupInfo), 0);
      cb := SizeOf(StartupInfo);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), StartupInfo, ProcessInfo);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := windows.ReadFile(StdOutPipeRead, pCommandLine, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            pCommandLine[BytesRead] := #0;
            Result := Result + pCommandLine;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
      finally
        CloseHandle(ProcessInfo.hThread);
        CloseHandle(ProcessInfo.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;

变量2:

实时捕获控制台输出,并将其显示在TMemo中:

procedure CaptureConsoleOutput(const ACommand, AParameters: String; AMemo: TMemo);
 const
   CReadBuffer = 2400;
 var
   saSecurity: TSecurityAttributes;
   hRead: THandle;
   hWrite: THandle;
   suiStartup: TStartupInfo;
   piProcess: TProcessInformation;
   pBuffer: array[0..CReadBuffer] of AnsiChar;      <----- update
   dRead: DWord;
   dRunning: DWord;
 begin
   saSecurity.nLength := SizeOf(TSecurityAttributes);
   saSecurity.bInheritHandle := True;  
   saSecurity.lpSecurityDescriptor := nil; 

   if CreatePipe(hRead, hWrite, @saSecurity, 0) then
   begin    
     FillChar(suiStartup, SizeOf(TStartupInfo), #0);
     suiStartup.cb := SizeOf(TStartupInfo);
     suiStartup.hStdInput := hRead;
     suiStartup.hStdOutput := hWrite;
     suiStartup.hStdError := hWrite;
     suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;    
     suiStartup.wShowWindow := SW_HIDE; 

     if CreateProcess(nil, PChar(ACommand + ' ' + AParameters), @saSecurity,
       @saSecurity, True, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup, piProcess)
       then
     begin
       repeat
         dRunning  := WaitForSingleObject(piProcess.hProcess, 100);        
         Application.ProcessMessages(); 
         repeat
           dRead := 0;
           ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil);          
           pBuffer[dRead] := #0; 

           OemToAnsi(pBuffer, pBuffer);
           AMemo.Lines.Add(String(pBuffer));
         until (dRead < CReadBuffer);      
       until (dRunning <> WAIT_TIMEOUT);
       CloseHandle(piProcess.hProcess);
       CloseHandle(piProcess.hThread);    
     end; 

     CloseHandle(hRead);
     CloseHandle(hWrite);
   end;
end;

来源:delphi.wikia.com


1
对于变量1,在较新版本的Delphi(例如10.3)中,需要在以“WasOK:=windows.ReadFile”开头的行中删除“windows.”前缀,否则编译将失败。 - gurpal2000
3
实际上,最好使用完整的限定符:"winapi.windows.readfile"。 - Gabriel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接