5

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.

  1. you cannot create a COM dll because then it would become specific to process and cannot be used
  2. 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

4
  • You simply write a method or a property setter that takes an argument, like an array, and shazam, you have data transfer. Jun 28, 2015 at 7:43
  • so which method i should use(dll or exe server), if i use a COM exe server and then should i make the CoClass Single ton ? if yes, assuming that Process-A instantiate the CoClass and sets the property and Process-B when Instantiates the same component/CoClass will get the same object as it is singleton and then use the "get" property to read the data that was set by Process-A ..is it? Jun 29, 2015 at 6:15
  • Yes. You need exe process to share data between two client process and each client activation request should reuse existed coclass - inproc dll will use its local data within each client process.
    – Vlad
    Jun 29, 2015 at 16:42
  • Also pay attention to user accounts that activate shared object - if you will run two clients under accounts with the same credentials then one server process will be launched. If one client under admin and other under regular user - two separate process will be launched.
    – Vlad
    Jun 29, 2015 at 17:32

2 Answers 2

0

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++.

  1. Creating Structure provider:

    • Create new ATL project with name COMStructProvider (select ATL project template in Visual C++). In wizard select “Executable” application type (not DLL). Other option by default. Wizard will generate project files.
    • Select Project -> Add class -> ATL simple object -> Add. In short name field type arbitrary name, for example MyStruct. Click “Finish”. This will add header and implementation file for MyStruct coclass. Also MyStruct.rgs will be added to help register your coclass in registry. Now you have minimal COM server and can build solution but to do it you need to run VS as administrator (because it will register your server in registry), otherwise registration fail.
    • Add two data members to CMyStruct(VS prepend class with C by default) class: private: std::string m_name; int m_age;
    • During previous steps wizard created interface IMyStruct (you can see it in idl file). Now we want to add methods to this interface: getters and setters to our two private data members. Select Class View tab, select IMyStruct interface (derived from IDispatch), select in context menu “add method”. For example method name getAge with parameter LONG*, parameter attributes “out” and parameter name: age (click Add to add parameter). This adds new method to idl file and to header and impl file. Repeat adding of methods for setAge (in parameter of type LONG), getName (out, BSTR* ), setName(in, BSTR). Name of parameters doesn’t matter.

In idl file you will have something like that - I provide this as checkpoint that all steps are done correctly:

import "oaidl.idl";
import "ocidl.idl";

[
    object,
    uuid(AA2DA48C-CD1E-4479-83D4-4E61A5F188CB),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IMyStruct : IDispatch{
    [id(1)] HRESULT getAge([out] LONG* age);
    [id(2)] HRESULT setAge([in] LONG age);
    [id(3)] HRESULT getName([out] BSTR* name);
    [id(4)] HRESULT setName([in] BSTR name);
};
[
    uuid(E7A47886-D580-4853-80AE-F10FC69E8D73),
    version(1.0),
]
library COMStructProviderLib
{
    importlib("stdole2.tlb");
    [
        uuid(CC51EFFE-C8F4-40FA-AEA3-EB6D1D89926E)      
    ]
    coclass MyStruct
    {
        [default] interface IMyStruct;
    };
};
  • Add implementation:

STDMETHODIMP CMyStruct::getAge(LONG* age)
{
	*age = m_age;
	return S_OK;
}


STDMETHODIMP CMyStruct::setAge(LONG age)
{
	m_age = age;
	return S_OK;
}


STDMETHODIMP CMyStruct::getName(BSTR* name)
{
	*name = SysAllocString(m_name.c_str());
	return S_OK;
}


STDMETHODIMP CMyStruct::setName(BSTR name)
{
	m_name.assign(name);
	return S_OK;
}

  1. Creating client. Add new Win32 Console application project MyStructClient to solution (executable). Add following code:

#include <iostream>
// check for correct path to tlb library. Will create tlh, tli files that provide smart pointers, etc.
#import "..\\COMStructProvider\\Debug\\COMStructProvider.tlb" no_namespace named_guid

using namespace std;

int main()
{
    // initialize COM runtime
	CoInitialize(NULL);
	{
        // smart pointer simplifies work, will invoke CoCreateInstance to activate COM server
		IMyStructPtr spMyStruct(__uuidof(MyStruct));
		BSTR name = SysAllocString(L"John");
		spMyStruct->setName(name);
		SysFreeString(name);

		BSTR retreivedName;
		spMyStruct->getName(&retreivedName);
		wcout << "name " << retreivedName << endl;
		SysFreeString(retreivedName);

		spMyStruct->setAge(5);

		long age = 0;
		spMyStruct->getAge(&age);

		cout << "age " << age << endl;
	}
	CoUninitialize();

	return 0;
}

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.

12
  • i sincerely appreciate you taking time out in replying to the question in details but this not an answer, what you have demonstrated is to use the COM exe server and share data between them. but what about data transfer between two processes, how can process-A and Process-B use this COM exe server to share data ? Jun 29, 2015 at 6:06
  • The short and correct answer was given in comment to your question by Hans Passant. IPC in COM achieved by passing data via arguments in method invocations.As you mentioned that "your knowledge is average" I expanded answer to give you opportunity to implement your own server and client with minimal efforts and play with them. I'm confident that my ATL approach is very useful and better for first time - approach with only C++ without ATL wizards will lead to many errors, it requires a lot of code to be provided in answer. The C++ approach is more appropriate to Code Project forum.
    – Vlad
    Jun 29, 2015 at 12:31
  • 1
    it is not about communicating with COM server and Client , my question is can we have two client application that will use the COM Exe Server as intermediary to transfer data between them, if yes then how ? Jun 29, 2015 at 12:38
  • Please read my answer - I wrote that you can launch as many clients as you wish. If first client set age to 5 then second client can read the same value 5. They will exchange data via shared structure in server.
    – Vlad
    Jun 29, 2015 at 12:41
  • 2
    you need to create a COM exe server not a DLL and make the CoClass Singleton, now your two process between which you want to exchange data can instantiate the COM class set some properties and the other process can get those properties May 21, 2019 at 8:49
0

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:

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:

enter image description here

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':

enter image description here

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.