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 interrogate the returned string after calling the executable? Failing that, I might have to write out the string to a file in the executable and read that in the calling program after the call has finished. That seems lame though.
How does the executable return a string? The most I know an executable can return is just a number, the exit code.
Assuming that you want to capture what the exe is writing to the standard output, you can start it with
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.
You could use shared memory to communicate between the two applications.
You can use the Win32 CreateFileMapping function, and perhaps use a mutex to synchronise calls to the memory mapped file.
The following code creates the file.
the following code closes the mapfile
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: