TYPE_E_CANTLOADLIBRARY - COM interop via MTA thread

1

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!

c#
com-interop
asked on Stack Overflow May 15, 2012 by pizza247 • edited May 23, 2017 by Community

1 Answer

2

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

answered on Stack Overflow May 15, 2012 by Ben

User contributions licensed under CC BY-SA 3.0