BUG: "A reference to '<typeLibrary>' could not be added" error message when you try to add a project reference to a COM DLL (828986)
The information in this article applies to:
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
- Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2003), Professional Edition
- Microsoft Visual Studio .NET (2003), Academic Edition
- Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2002), Professional Edition
- Microsoft Visual Studio .NET (2002), Academic Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
SYMPTOMSWhen you try to add a project reference to a Component
Object Model (COM) DLL in Microsoft Visual Studio .NET, you may receive an
error message that is similar to the following: A reference to 'C:\MyCOMDLL\Debug\MyCOMDLL.dll' could not be
added. Converting the type library to a .NET assembly failed. Could not load
type MyCOMDLLLib.MyClassClass from assembly Interop.MyCOMDLLLib,
Version=1.0.0.0. Additionally, when you try to convert the type
definitions that are found in a COM DLL by using Microsoft Type Library
Importer (Tlbimp.exe) from a command prompt, you may receive an error message
that is similar to the following: TlbImp error:
System.TypeLoadException - Could not load type MyCOMDLLLib.MyClassClass from
assembly MyCOMDLLLib, Version=1.0.0.0.
CAUSEYou may notice the behavior that is mentioned in the
"Symptoms" section when the following conditions are true:
- Your DLL contains a class (such as MyClass) that implements an interface (such as IMyClass) that in turn derives from a base interface (such asIBaseClass).
- The IMyClass interface contains a method (such as Test) that has the same name as a property that the IBaseClass interface has.
You notice this behavior because of the mechanism that
Tlbimp.exe uses to disambiguate member names. Under the previous conditions,
when the Microsoft .NET Framework tries to create a method implementation to
associate the Test method of the MyClass class with the corresponding interface method, the .NET Framework
does not know the name of the corresponding interface method. Therefore,
Tlbimp.exe generates a System.TypeLoadException error, and then you receive the
error message that is mentioned in the "Symptoms" section. When you
try to add a project reference to a COM DLL, Visual Studio .NET internally runs
Tlbimp.exe and then handles any generated exceptions. Therefore, under the
previous conditions, Visual Studio .NET handles the generated
System.TypeLoadException, and then you receive the error message that is
mentioned in the "Symptoms" section. WORKAROUNDTo work around the behavior that is mentioned in the
"Symptoms" section, modify your COM DLL so that the method in the derived
interface ( IMyClass) and the property in the base interface ( IBaseClass) have different names. To do this, follow these steps. Note The following procedure is based on the sample that is mentioned
in the "More Information" section. Therefore, the code and the file names in
this procedure may differ from your code and from your file names.
- Quit Visual Studio .NET after you receive an error
message.
- Start Visual Studio .NET, and then open the MyCOMDLL
solution.
- Locate the following code in the MyCOMDLL.idl file:
[id(2), helpstring("method Test")] HRESULT Test([in] CHAR Param1); - Replace the code that you located in the previous step with
the following code:
[id(2), helpstring("method ModifiedTest")] HRESULT ModifiedTest([in] CHAR Param1); - Locate the following code in the MyClass.h file:
STDMETHOD(Test)(CHAR Param1); - Replace the code that you located in the previous step with
the following code:
STDMETHOD(ModifiedTest)(CHAR Param1); - Locate the following code in the MyClass.cpp file:
STDMETHODIMP CMyClass::Test(CHAR Param1) - Replace the code that you located in the previous step with
the following code:
STDMETHODIMP CMyClass::ModifiedTest(CHAR Param1) - On the Build menu, click Build
MyCOMDLL to build the MyCOMDLL.dll file.
- In the Solution Explorer window, click
ConsoleApplication1.
Try to add a project reference
to the MyCOMDLL.dll file that you built in step 7. Notice that you do not
receive the error message that is mentioned in the "Symptoms"
section. - From a Visual Studio .NET command prompt, change the
directory path of the directory where the MyCOMDLL.dll file exists, and then
run the following command:
Tlbimp
MyCOMDLL.dll
Notice that you do not receive the error
message that is mentioned in the "Symptoms" section.
The previous steps demonstrate how to work around the behavior
that is mentioned in the "Symptoms" section by changing the name of the method
in the IMyClass interface. You can also work around this behavior by similarly
changing the name of the property in the IBaseClass interface. STATUS
Microsoft has confirmed that this is a bug in the Microsoft .NET Framework 1.1 and in the Microsoft .NET Framework 1.0.
REFERENCESFor additional information about adding and about removing
project references, visit the following Microsoft Developer Network (MSDN) Web
site: For additional information about Type Library Importer
(Tlbimp.exe), visit the following MSDN Web site: For additional information about the TypeLoadException class, visit the following MSDN Web site: For additional information about interface definitions and about
type libraries, visit the following MSDN Web site:
Modification Type: | Minor | Last Reviewed: | 8/26/2005 |
---|
Keywords: | kbvs2002sp1sweep kbInheritance kbIDL kbIDEProject kbDLL kbCOMInterop kbMiscTools kbinterop kbProgramming kbcode kbSample kberrmsg kbbug KB828986 kbAudDeveloper |
---|
|