I\'m using the Win32 CreateProcess
function to perform a call to an external executable. The executable returns a string.
Is there a way I can capture and
Assuming that you want to capture what the exe is writing to the standard output, you can start it with
yourprog.exe > results.txt
This will write the output to results.txt
, which you can then read and evaluate.
Or, you can try the method explained in this thread: Create a pipe for the output from your spawned process and read from it. Post number 7 has a source example on how to use the relevant WinApi functions in C++, that should easily translate into Delphi.
Jedi Code Library contains function CreateDOSProcessRedirected which runs a process and provides it with input and output file. You can put required input (if any) into the input file and read process output (if any) from the output file (after the process has completed).
That's how 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;