Article ID: 310674 - View products that this article applies to.
This article was previously published under Q310674
This article describes the process of adding a reference to another assembly in a Visual C++ project. In other languages, such as Visual C# , you can add a reference through the Add Reference dialog box. This dialog box is not available to Managed C++ applications. There are several tips that make using references in a Managed C++ application easier.
.NET references.NET references point to shared assemblies. For example, the assembly System.Windows.Forms.dll is a standard assembly for accessing the Windows Forms classes. In order to use this assembly in a Managed C++ application, you simply have to reference it with a #using preprocessor directive, as shown here:
COM referencesUse of a COM object in a Managed C++ application involves a design decision. One option is to use unmanaged COM code inside of a managed class. For example, you could decide to use the traditional #import solution. This can be a good option for cases where there are problems using COM Interop.
The second option is to use interop assemblies that wrap the COM object. This is the only method available to other languages like C# , Visual Basic 2005, and Visual Basic .NET. To create an interop assembly for a COM object, use the TLBIMP.exe tool (see "References"). For example, use the following steps to automate Microsoft Internet Explorer from a managed application:
NOTE: The environment variables for Visual C++ must be set in order for TLBIMP.exe to be recognized. If they are not set, you will first have to run ./VC7/BIN/VCVARS32.bat in Visual Studio .NET or ./VC/BIN/VCVARS32.bat in Visual Studio 2005 and Visual C++ 2005 Express Edition.
Project referencesProject references are references to assemblies created by other projects. Again, the #using directive is used to reference these assemblies. However, because these assemblies are not shared, you must take measures to ensure that the compiler can find them. There are two methods for doing this:
Using post-build eventsPrivate assemblies must reside in the same folder as the executable that is using them. When you add a reference in Visual C#, in Visual Basic .NET, or in Visual Basic 2005, it is automatically copied to the output folder. In a Managed C++ application, this step can be automated through the use of post-build events. For example, consider a scenario in which you have an assembly named "MYLIB.dll" in the project folder of your Managed C++ application named "TestApp". The following steps will set up a post-build event that will copy this DLL to the output folder of the TestApp project.
In Visual C++ .NET 2003 or Visual C++ 2005By using Visual C++ .NET 2003 or Visual C++ 2005, you can add a reference by means of the Add Reference dialog box. To add a project reference, follow these steps:
For more information about the type library importer and Visual C++ .NET, visit the following Microsoft Web sites:
Type Library Importer (Tlbimp.exe)http://msdn2.microsoft.com/en-us/library/tt0cf3sx(vs.71).aspx?frame=true
Visual C++ .NET (2002) Support Centerhttp://support.microsoft.com/default.aspx?xmlid=fh%3BEN-US%3Bvcnet
Article ID: 310674 - Last Review: May 18, 2007 - Revision: 3.3
Contact us for more help
Connect with Answer Desk for expert help.