Can I access the string returned by the Delphi CreateProcess command?

I use the Win32 function CreateProcessto call an external executable. The executable file returns a string.

Is there a way to capture and poll the returned string after calling the executable? Otherwise, I may have to write the line to a file in the executable and read that in the calling program after the call is completed. It seems lame though.

+1
source share
4 answers

The Jedi Code Library contains the CreateDOSProcessRedirected function, which goes through the process and provides its input and output file. You can put the required input (if any) into the input file and output the output process (if any) from the output file (after the process is completed).

The way this is implemented in the JCL :

function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile: string): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecAtrrs: TSecurityAttributes;
  hInputFile, hOutputFile: THandle;
begin
  Result := False;
  hInputFile := CreateFile(PChar(InputFile), GENERIC_READ, FILE_SHARE_READ,
    CreateInheritable(SecAtrrs), OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, 0);
  if hInputFile <> INVALID_HANDLE_VALUE then
  begin
    hOutputFile := CreateFile(PChar(OutPutFile), GENERIC_READ or GENERIC_WRITE,
      FILE_SHARE_READ, CreateInheritable(SecAtrrs), CREATE_ALWAYS,
      FILE_ATTRIBUTE_TEMPORARY, 0);
    if hOutputFile <> INVALID_HANDLE_VALUE then
    begin
      FillChar(StartupInfo, SizeOf(StartupInfo), #0);
      StartupInfo.cb := SizeOf(StartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      StartupInfo.wShowWindow := SW_HIDE;
      StartupInfo.hStdOutput := hOutputFile;
      StartupInfo.hStdInput := hInputFile;
      Result := CreateProcess(nil, PChar(CommandLine), nil, nil, True,
        CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo,
        ProcessInfo);
      if Result then
      begin
        WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
        CloseHandle(ProcessInfo.hProcess);
        CloseHandle(ProcessInfo.hThread);
      end;
      CloseHandle(hOutputFile);
    end;
    CloseHandle(hInputFile);
  end;
end;
+8
source

Assuming you want to capture what exe writes to standard output, you can run it with

yourprog.exe > results.txt

This will write the output to results.txt, which you can then read and evaluate.

, : . 7 , WinApi ++, Delphi.

+3

? , , , - , .

+2

.

CreateFileMapping Win32 , , .

.

var
  fMapping : THandle;
  pMapData : Pointer;

fMapping := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
             0, MAPFILESIZE, pchar('MAP NAME GOES HERE'));

PMapData := MapViewOfFile(fMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);

map

if PMapData <> nil then
  UnMapViewOfFile(PMapData);
if fMapping <> 0 then
  CloseHandle(fMapping);
+1

All Articles