DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

DOSCommand

Executes the given DOS command line CommandLine using the Windows command line processor. Use CmdShow to specifiy whether and how the command window should be displayed by using one of the Windows SW_XXX constants. Pass True to WaitUntilComplete to make the routine halt until the DOS command has completed or pass False to make the routine return immediately the DOS command has started. The optional WorkingDir parameter can be used to specify the working directory to be used by the DOS command. Returns True if the command is executed or False if not.

function DOSCommand(const CommandLine: string; const CmdShow: Integer;
  const WaitUntilComplete: Boolean; const WorkingDir: string = ''): Boolean;
var
 ComSpec: array[0..Pred(Windows.MAX_PATH)] of Char;
 FullCommandLine: string;
 PWorkingDir: PChar;
 SI: Windows.TStartupInfo;
 PI: Windows.TProcessInformation;
begin
  FillChar(SI, SizeOf(SI), #0);
  SI.cb := SizeOf(SI);
  SI.dwFlags := Windows.STARTF_USESHOWWINDOW; // needed to use wShowWindow
  SI.wShowWindow := CmdShow;
  Windows.GetEnvironmentVariable('COMSPEC', ComSpec, SizeOf(ComSpec));
  FullCommandLine := ComSpec + ' /C ' + CommandLine;
  if WorkingDir <> '' then
    PWorkingDir := PChar(WorkingDir)
  else
    PWorkingDir := nil;
  Result := Windows.CreateProcess(
    nil,
    PChar(FullCommandLine),
    nil,
    nil,
    False,
    Windows.NORMAL_PRIORITY_CLASS,
    nil,
    PWorkingDir,
    SI,
    PI
  );
  if Result then
  begin
    if WaitUntilComplete then
      Windows.WaitforSingleObject(PI.hProcess, Windows.INFINITE);
    Windows.CloseHandle(PI.hProcess);
    Windows.CloseHandle(PI.hThread );
  end;
end;

DOSCommandRedirect (TStream overload)

Executes the given DOS command in a hidden window and redirects its output to the given stream. Returns True if the command executes or False otherwise.

function DOSCommandRedirect(const CommandLine: string;
  const OutStream: Classes.TStream): Boolean; overload;
var
  SA: Windows.TSecurityAttributes;
  SI: Windows.TStartupInfo;
  PI: Windows.TProcessInformation;
  ComSpec: array[0..Pred(Windows.MAX_PATH)] of Char;
  FullCommandLine: string;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..8*1024-1] of Char;
  BytesRead: Cardinal;
begin
  Windows.GetEnvironmentVariable('COMSPEC', ComSpec, SizeOf(ComSpec));
  FullCommandLine := ComSpec + ' /C ' + CommandLine;
  with SA do
  begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  Windows.CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := Windows.STARTF_USESHOWWINDOW or Windows.STARTF_USESTDHANDLES;
      wShowWindow := Windows.SW_HIDE;
      // don't redirect stdin
      hStdInput := Windows.GetStdHandle(Windows.STD_INPUT_HANDLE);
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    Result := Windows.CreateProcess(
      nil, PChar(FullCommandLine), nil, nil, True, 0, nil, nil, SI, PI
    );
    Windows.CloseHandle(StdOutPipeWrite);
    if Result then
    try
      repeat
        WasOK := Windows.ReadFile(
          StdOutPipeRead, Buffer, SizeOf(Buffer), BytesRead, nil
        );
        if BytesRead > 0 then
          OutStream.Write(Buffer[0], BytesRead);
      until not WasOK or (BytesRead = 0);
    finally
      Windows.CloseHandle(PI.hThread);
      Windows.CloseHandle(PI.hProcess);
    end;
  finally
    Windows.CloseHandle(StdOutPipeRead);
  end;
end;

DOSCommandRedirect (File overload)

Executes the given DOS command in a hidden window and redirects its output to the given file. Returns True if the command executes or False otherwise.

function DOSCommandRedirect(const CommandLine, OutFile: string): Boolean;
  overload;
var
  FileStream: Classes.TFileStream;
begin
  FileStream := Classes.TFileStream.Create(OutFile, Classes.fmCreate);
  try
    Result := DOSCommandRedirect(CommandLine, FileStream);
  finally
    FileStream.Free;
  end;
end;

View the whole database.

Go to the DelphiDabbler website.