NN – WordPress

October 22, 2008

COM and sessions

Filed under: com — by NN @ 8:24 pm

It turns out that you cannot use COM server running in one session from COM client in other session.

Also you cannot use COM server from other user when you use No registry COM approach.

What can you do ?

One solution is to marshal and unmarshal interface in a global memory.

If you can read russian you can read it here and if you don’t read russian here :)

In short the approach is simple: It creates a global shared memory with access to all, calls CoMarshalInterface in the server side, then in the client side it calls CoUnmarshalInterface and retrieves IClassFactory.

This should work for multiple users and sessions. But requires some code change, you cannot call the regular CoCreateInstance.

 

The other solution is to use Session Monikers, but it works only for Windows 2000 and above, of course you do not have sessions in Windows 9x so don’t panic :)

In this approach you can specify a specific session which you are interested in.

 

The last stage is to make this shitness work.

Enjoy with COM.

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.

Powered by WordPress.com