i have a average knowledge in COM and will like to understand how COM helps in data transfer. Assuming there are two processes, Process-A and Process-B and both of them wants share some data with each other, of course there are many RPC mechanisms but i would like to use COM.
- you cannot create a COM dll because then it would become specific to process and cannot be used
- can we create a Single ton COM EXE server and wrap the structure in COM CoClass and expose it members as properties and then ...no idea how to ?
Above is my understanding, can anyone of you help me clear my understanding on this topic? basically i would like to share a data structure between two process using COM
Updated: when one object call method of another object (passing information in parameters) we say that first object sends message to second one. Usually this happen s within one process address space. COM allows object in one process to call method of object in another process - thus enabling interprocess communication.
COM is huge topic and it is not possible to explain it in format of overflow answer. What I will try to do is to demonstrate simplest example of local out of process COM server and COM client (as short as possible) with help of Visual Studio ATL wizards(as far as you mentioned ATL in tags) that will generate most of the code and this gives the possibility to test COM approach and investigate boilerplate sources. But for better understanding I recommend to find inproc COM server implementation without ATL - just with C++.
Creating Structure provider:
In idl file you will have something like that - I provide this as checkpoint that all steps are done correctly:
So, you have two processes running simultaneously: server that provides access to structure and client that has access to the same structure (you can run more client processes in parallel. All clients access the same server process - can be considered as singleton - but it's possible to spawn separate process with each activation). Client can change and get values of this structure (as was required). Under the hood client has access to proxy of coclass in its own address space and COM runtime support all interprocess communication. Those proxy/stubs (in the form of C/C++ sources) are generated by MIDL compiler from interface idl file mentioned above. As far as you focused on transfering Data between two process you should know that there are three types of COM marshaling: custom, standard and universal. In this example universal is sufficient because I use only VARIANT compatible types as method parameters. To pass arbitrary types you should use standard marshaling (with help of proxy/stub dlls, generated in separate project on first step when creating COM server. name of project is the name of project with server with suffix PS). Disadvantage of standard marshaling - you should deploy those PS dlls with your COM server.
COM 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:
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:
Note: implementation of the cache is left to the reader, but it could reuse another of COM+ services: the COM+ Shared Property Manager