February 20th, 2004

Why do I get a QueryInterface(IID_IMarshal) and then nothing?

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.