yxrkt
yxrkt

Reputation: 422

How to share COM objects between 2 processes?

I want Application1.exe to instantiate an instance of its Item class.
I want Application2.exe to call GetPrice() on this object.

I have followed steps 1-7 on the following website:
http://www.codeguru.com/Cpp/COM-Tech/activex/tutorials/article.php/c5567/

This is what I have so far.

Application1's main looks like this:

CoInitialize( NULL );

DWORD dwRegister;
ItemFactory *pFactory = new ItemFactory;
CoRegisterClassObject( CLSID_Item, pFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister );

_getch();
return 0;

Application2's main looks like this:

CoInitialize( NULL );
CoGetClassObject( CLSID_Item, CLSCTX_LOCAL_SERVER, NULL, IID_IItem, (LPVOID *)&pFactory );

My Issue (hopefully my only issue) is that I have no idea how to associate my Item class (or its interface, IItem) with CLSID_Item; this is just some random GUID I defined in another file. I've tried

CoRegisterPSClsid( IID_IItem, CLSID_Item );

After this line, I tried

Item *pItem;
CoCreateInstance( CLSID_Item, NULL, CLSCTX_LOCAL_SERVER, IID_IItem, (LPVOID *)&pItem );

I get an E_NOINTERFACE error.
Should I be creating a factory with CoCreateInstance? Ugh, so confused...

Upvotes: 2

Views: 1026

Answers (1)

bdonlan
bdonlan

Reputation: 231103

In order to use COM across process or thread boundraries, you must tell COM about your interfaces so it can marshal your function arguments/return values between processes. The easiest way to do this is to use an interface predefined in the system, such as IDispatch, but if you want to use your own, you must either register a proxy/stub DLL, or a type library. If you don't do this, then calls to QueryInterface for your custom interface across COM domains will fail with E_NOINTERFACE, as you are seeing.

Upvotes: 1

Related Questions