NN – WordPress

September 29, 2008

Registration free COM

Filed under: c++, com — by NN @ 4:59 am

So this will be the first post, not the last, about registration free COM.
Btw co is short of COM. Why ??

The beginning is taken from KK’s blog:Registration free COM .

So what do we do:
1. Create an IDL file describing our interfaces.
2. Compile it with midl to produce for us the definitions of the interface and proxy-stub.
3. The server application uses the generated .c files and just call CoRegisterClassObject in the beginning for the factory and then registers the proxy-stub.
4. The client registers the proxy-stub and then just call naturally CoCreateInstance.
5. It works.

Some to make work.
There is a standard marshalling for standard interfaces: IPersist, IStorage, IClassFactory …
What it tells us ? That we can marshall IClassFactory for free.
So we create a global instance of our ClassFactory implementation and then register it using CoRegisterClassObject.
That’s all what is needed to make class factory in the client ! :)

The only problem that you first create a ClassFactory and then you call manually CreateInstance… Not good.

Again we have a problem.
And the brilliant solution !
If you remember [coclass] in IDL describes some class which implements the interfaces.
What if you tells that coclass implements your interface and ClassFactory too ? Hmm.

[uuid("...")]
coclass A
{
[default] interface IA;
interface IClassFactory
}

This is the trick :)
When we call CoCreateInstance in the client:
CoCreateInstance(CLSID_A, … IID_A..);
It looks for ClassFactory and finds it ! Then it calls CreateInstance of our ClassFactory implementation which creates IA implementation class.
So simple :)

Now, since the creation is the regular COM instance creation you can simply use the class in .Net like this:
A a = new AClass();

That’s all for today.

P.S.
The source code will be supplied later.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress.com