2

I have a simple x86-targeting .NET 3.5 console program that calls methods on COM objects in an ActiveX DLL via interop. In my registry, the ActiveX DLL has a ThreadingModel of Apartment. When the .NET program is running in an STA thread, everything works fine. In an MTA thread, some of the COM methods run fine, others give me:

System.InvalidCastException: Unable to cast COM object of type 'MyComTypeClass' to interface type '_MyComType'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{[omitted]}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

If I switch the ActiveX DLL's ThreadingModel to Free or Both, the .NET program works in an MTA thread, but I want to keep the ThreadingModel at Apartment.

This same .NET program with the same ActiveX DLL and interop assembly works fine on multiple other machines (WinXP 32-bit, Win7 64) using both threading models. The exception only occurs on one PC (Win7 64) and only in an MTA thread. Anyone know why?

Similar questions have been asked without solutions, hoping 3rd time's a charm:

COM Exception - TYPE_E_CANTLOADLIBRARY?

TYPE_E_CANTLOADLIBRARY when using a COM object on a separate thread on Windows 2003 x64 only

Thanks!

Community
  • 1
  • 1
pizza247
  • 70
  • 1
  • 6
  • Since you seem to be asking why, and not how to make it all better, see [Explaining STA and MTA](http://stackoverflow.com/questions/127188/could-you-explain-sta-and-mta). I think the **"once initialized in an apartment, is part of that apartment for the duration of it's runtime"** bit answers this. – Joshua Drake May 15 '12 at 21:31
  • You could also take a look at [Interop Marshaling](http://msdn.microsoft.com/en-us/library/eaw10et3.aspx) to get more information about **Marshaling and COM Apartments** specifically how _the COM marshaler gets involved when using MTA_, which likely explains your cast issue. – Joshua Drake May 15 '12 at 21:39

1 Answers1

3

Using an STA object from an MTA thread requires marshalling. Standard marshalling requires a type library. The error you are getting means the type library could not be loaded.

So I would guess that the type library is not registered correctly. Try unregistering and re-registering the DLL, otherwise try registering the type library directly using REGTLB.exe

Ben
  • 34,935
  • 6
  • 74
  • 113
  • Thanks for answering. I un/re-registered the DLL and registered the type library directly. I see the interface and typelib in the registry with expected IIDs and I can open the typelib in OLE/COM Object Viewer without issue. Still doesn't work! – pizza247 May 15 '12 at 23:35
  • 3
    That's not the only config, the starting point is HKCR\Interface where it looks up the ProxyStubClsId and TypeLib keys. Use SysInternals' ProcMon to see it searching these keys. – Hans Passant May 16 '12 at 00:09
  • 1
    Fixed with help from ProcMon. Thanks for the tip. The ActiveX DLL references a 3rd-party component whose type library was incorrectly registered, and this typelib is required because I'm marshalling between threading models, as Ben said. – pizza247 May 16 '12 at 15:37