Question

This is a variation on another Q of mine. I have two applications, X and Y. They have to share a single COM object between them. X will be sending data to this object and Y will respond on events sent from this application. At any moment, X or Y might be terminated, yet the singleton needs to stay alive until both applications are terminated.
It should be possible to create a singleton COM object this way, but what if X starts first and creates it, Y then starts and uses it, then X stops and... What happens to the singleton?
Anyways, has anyone ever tried this with Delphi?

Was it helpful?

Solution

I think you're missing a core point of COM. COM is just a step op from general IPC, by which I mean, a COM process lives in only one process (or thread even, but that's where the apartment story comes in). If you access one COM object from two processes, one process is actually marshalling the calls from and to the object into 'flat data' and posting messages to a hidden window handle to the other process.

To be able to do what you describe you would have to worry what to do with an object when the 'owning process' want to shut down, and has to 'suspend' the object (e.g. by serializing) and ask one of the other processes to 'resume' the object and assume ownership and start running the object.

I would suggest you create a third process, specifically to only run this singleton object you want to use in the other processes. This way you could let normal reference counting and COM activation do its job, and you would be using COM just the way it was supposed to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top