Jump to content
Sign in to follow this  
dummzeuch

Passing back a string from an external program

Recommended Posts

I have got two programs, both written in Delphi using the VCL. Program1 excecutes Program2 using CreateProcess, passing some parameters. In some circumstances I would like Program2 to return a string to Program1.

 

Since CreateProcess only returns an exit code, which is a number, this requires some additional communication.

 

Converting program2 into a DLL or Package is not an option in this case, neither is integrating the source code of program2 to program1.

 

The simplest way would probably be that program1 passes a file name to program2, program2 creates that file, writes the string to it and exits.

 

It could of course be implemented much more complicated, e.g. via sockets, Windows Messages, Shared Memory or even WriteProcessMemory.

 

Have you done something like this? If yes, which method did you use and what was your experience?

 

 

Share this post


Link to post
1 hour ago, Микола Петрівський said:

Just create global atom and pass it's code to the caller. Or caller can create empty atom, and then check for changes.

From the description it looks to me that atoms are immutable. So how do you propose to return a string from program2 to program1 via a global atom?

Share this post


Link to post

I would like to remind you that I was asking for experience with the IPC methods:

3 hours ago, dummzeuch said:

Have you done something like this? If yes, which method did you use and what was your experience?

For now I have implemented it with passing a file name as described in my original post. Does this have any drawbacks (apart from the obvisous one:

It creates a race condition.

I have mitigated this for now by creating a unique temporary directory to place the file in.

Share this post


Link to post
4 hours ago, dummzeuch said:

I have got two programs, both written in Delphi using the VCL. Program1 excecutes Program2 using CreateProcess, passing some parameters. In some circumstances I would like Program2 to return a string to Program1.

 

Since CreateProcess only returns an exit code, which is a number, this requires some additional communication.

 

Converting program2 into a DLL or Package is not an option in this case, neither is integrating the source code of program2 to program1.

 

The simplest way would probably be that program1 passes a file name to program2, program2 creates that file, writes the string to it and exits.

 

It could of course be implemented much more complicated, e.g. via sockets, Windows Messages, Shared Memory or even WriteProcessMemory.

 

Have you done something like this? If yes, which method did you use and what was your experience?

 

 

A fairly simple method is to pass a window handle to program2 on the command line an then have that program sent its result back in a WM_COPYDATA message to that window. Only caveat: program1 must not wait on program2 to finish in a manner that blocks message processing of the main thread.

  • Like 1

Share this post


Link to post
1 hour ago, dummzeuch said:

It creates a race condition

I don't understand the 'race condition'?

 

I mean you are doing the most basic of all communications, execute an app, wait for it to exit and read the output file.
In one form or another we do this every day 🙂

 

But since you are fishing for experience, I use this as part of a self updating feature.

  1. Get a Temp filename from the OS
  2. Write some data to it
  3. Execute another app waiting for it to complete
  4. Read any Error code it may have added to the file
  5. Delete the file

Other than the New feature where a user can add the Temp folder to the 'Controlled Folder Access' Ransomware Protection this is as basic as it gets.

Share this post


Link to post
19 minutes ago, FredS said:

I don't understand the 'race condition'?

If there are multiple instances of program1 and it always uses the same file name when it starts its own instance of program2. These multiple instances would then all write to the same file.

That's a race condition in my opinion.

Share this post


Link to post
1 minute ago, dummzeuch said:

always uses the same file name

 

OK, getting a temp filename from the OS solves that.

Share this post


Link to post
 
 
 
19 hours ago, dummzeuch said:

If there are multiple instances of program1 and it always uses the same file name when it starts its own instance of program2. These multiple instances would then all write to the same file.

That's a race condition in my opinion.

I'd call that a concurrency issue, rather than a race condition?

SharedFileName := '%temp%\MyFileName.ext.' + GetCurrentProcessId.ToString;

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×