How to use COM to transfer Data between two processes (for IPC - inter process communication)
See the question and my original answer on StackOverflowCOM exe out-of-process servers are notably difficult to write, but Microsoft has created COM+ Component service to ease this out.
It contains a lot of services, but here we're interested by the Application service that allows you to host in-process servers (DLL) in an out-of-process surrogate host.
It's quite simple, just write a standard ATL DLL (or use any other language/framework you like). I recommand using Automation types for the interface so you don't need special proxies, for example with an IDL interface defined like this:
interface ISharedMap : IDispatch{
[id(1)]
HRESULT PutData([in] BSTR key, [in] VARIANT value);
[id(2)]
HRESULT GetData([in] BSTR key, [out, retval] VARIANT *pValue);
};
Then create a new COM+ application, as described here: Creating COM+ Applications, and declare it as a server application. This is what you should see once this is done:
Your DLL will now be hosted automatically in a specific process (the famous dllhost.exe
) which will be started as soon as clients try to connect. By default, the same process will be used for various out-of-process COM clients. It will shutdown after some time, but you can configure the COM+ application in a various ways, for example with the 'Leave running when idle' flag set':
Now you will be able to use your cross process memory cache for all COM clients you'll like, for example, like this from a simple javascript .js code:
var map = new ActiveXObject("SharedMap");
map.PutData("mykey", "mydata")
var data = map.GetData("mykey")
Note: implementation of the cache is left to the reader, but it could reuse another of COM+ services: the COM+ Shared Property Manager