tags:

views:

47

answers:

2

I wrote a atl COM dll, dual. And used one of its intreace X in a C# GUI proj. when call X.F(), I got an System.InvalidCastException, can not cast namespace.XClass to namespace.IX, QueryInterface failed: BADTYPE. The call was in my own thread.

If i move the code to the GUI thread, it works just fine.

Please help me with that. Thanks a lot.

+2  A: 

COM objects are often STA, which means that they can only be used by the thread that created them

Paul Betts
thank you Paul. I did use the COM obj in thread which created it. But not in the GUIthread. If move all the code to a new thread, it work.
ahpho
Strictly speaking STA objects can also be used from other threads as well but that will require marshalling the interface pointers. If COM is unable to marshall the interface you see error indication as in the question.
sharptooth
+1  A: 

COM requires a proxy/stub to marshal the call from the thread to the thread that created the object. QI will fail if it cannot find it in the HKCR\Interface registry key.

Hans Passant