A common problem when trying to get your new COM object off the ground is that you can’t seem to be able to get it created. The object comes out of the class factory, and it gets a QueryInterface for IMarshal, and then nothing. What’s going on? This is a sure sign that you didn’t register your CLSID properly; most likely you forgot to set your threading model properly. (And if you fail to specify a threading model at all, then you get the dreaded “main” threading model.) If somebody tries to create a COM object from a thread whose model is incompatible with the threading model of the COM object, then a whole bunch of marshalling stuff kicks in. And if the marshalling stuff isn’t there, then COM can’t use your object. There is a long and very technical article in MSDN on COM threading models which has lots of scary-looking diagrams and tables. In particular, the second scary table in the “In-process servers: (almost) totally dependent on their clients” chapter lists all the combinations of thread models with object threading models, and what COM tries to do in each case. In particular, notice that if you have a (mistakenly marked) “main”-threaded object and somebody on any thread other than the main thread tries to create it, marshalling will try to kick in.
So watch those threading models. The failure modes when you get them wrong are quite baffling.
0 comments