tags:

views:

114

answers:

4
A: 

Nope, that's impossible. The whole idea of COM is that the COM server is started transparently and only preserves state until you have stopped using its objects. After you've released the COM objects the COM subsystem is free to completely stop the server and there's no way to recreate the same process. The only way a similar result would be possible is to have a COM object with serialization methods that would permit saving the state into a stream and restoring it from a stream. But even then you would have to CoCreateInstance() again, obtain a new COM object interface pointer and call the restore method of that object.

The pointer you get from CoCreateInstance is only valid for the current process, if you save it on disk and restore later it will become invalid.

sharptooth
This seems a rather naive/purest view of COM - maybe the world would be a better place if real COM code only behaved that way.
morechilli
@morechilli: That's the behavior implied by the model. Of course the implementation does what it does.
sharptooth
A: 

CoMarshalInterface (and related APIs) can be used to marshal an interface into another thread, process or different PC on the network. I don't know how long you are allowed to wait before completing the marshaling process, but in principal, if the object you are marshaling an interface to has not been closed the marshaling process can be completed later.

Being able to destroy an OLE object and later restore "the same object" is tied into what are called Monikers, and if you (can) understand those then your OLE/COM Juju is powerful indeed.

Chris Becke
Allegedly, DCOM has a "garbage collection" mechanism that kills the stub for an object after 4 minutes, so that's a timeout to be aware of. Provided the object is hosted in its own process, you _should_ be able to marshal the interface ptr to a stream, write that to disk, and then reconstitute in another process, if only you do it within those 4 minutes.
Kim Gräsman
A: 

If the application registered itself in the Running Object Table, you can use the GetActiveObject function to get a reference to the application object.

IUnknown *pUnknown;

hr = GetActiveObject(clsid, NULL, &pUnknown);
assert(SUCCEEDED(hr));

hr = pUnknown->QueryInterface(IID_IDispatch, (void **)&(iePtr_));
assert(SUCCEEDED(hr));
Jim Huang
A: 

I'd suggest making the wrapper layer long lived rather than transient, therefore it can easily hold a single reference to the third application.

The wrapper can still appear transient to the client code. If you make the wrapper a COM singleton then each time you cocreate it, you will get back the same instance.

To ensure the wrapper lives for the lifetime of your client hold a reference from startup to shutdown. This reference does not need to be wired through to other code. All other code simply creates the singleton every time it wants it.

morechilli