Microsoft KB Archive/310674

From BetaArchive Wiki

Article ID: 310674

Article Last Modified on 5/18/2007



APPLIES TO

  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ 2005 Express Edition



This article was previously published under Q310674

SUMMARY

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.

back to the top

.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:

#using <System.Windows.Forms.dll>
                


back to the top

COM references

Use 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:

  1. Open a command prompt.
  2. Navigate to the Windows System folder.
  3. Type the following command:

    tlbimp shdocvw.dll /out:Interop.shdocvw.dll

  4. Move Interop.shdocvw.dll to your project folder.

This creates an interop assembly for the COM objects in Shdocvw.dll. The resulting file, Interop.shdocvw.dll, can be used with a #using directive. It can then be treated as a managed component. For instructions on automatically copying this dynamic link library (DLL) to the output folder, see the "Using Post-Build Events" section of this article.

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.

back to the top

Project references

Project 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:

  • Copy the assembly to the project folder. -or-


  • Change the project settings to look for the assembly:
    1. Open the project's Property Pages dialog box.
    2. Click the C/C++ folder.


NOTE: In Visual C++ 2005, expand Configuration Properties, and then expand C/C++.

    1. Click the General property page.
    2. Modify the Resolve #using References property to point to the folder that contains the target assembly.

back to the top

Using post-build events

Private 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.

  1. Open the Managed C++ project's Property Pages dialog box.
  2. Click the Build Events folder.

    NOTE: In Visual C++ 2005, expand Configuration Properties, and then expand Build Events.
  3. Click the Post-Build Event property page.
  4. Modify the Command Line property to the following command:

    copy $(<ProjectDir>)mylib.dll $(<TargetDir>)

back to the top

In Visual C++ .NET 2003 or Visual C++ 2005

By 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:

  1. In Solution Explorer, select the project.
  2. On the Project menu, click Add References.


Note In Visual C++ 2005, click References on the Project menu, and then click Add New Reference.

  1. In the Add References dialog box, click the tab that corresponds with the category that you want to add a reference to.


Note In Visual C++ 2005, click the Browse tab in the Add References dialog box.

  1. Click Browse, locate the component that you want on your local drive, and then click OK. The component is added to the Selected Components field.


Note In Visual C++ 2005, locate the component that you want on your local drive.

  1. To add the selected reference to the current tab, click Add.


Note In Visual C++ 2005, click OK to close the dialog box and add the component in the References list box on the Properties Page dialog box of the project.

back to the top

REFERENCES

For more information about the type library importer and Visual C++ .NET, visit the following Microsoft Web sites:

back to the top

Keywords: kbcominterop kbhowtomaster kbmanaged kbnewsgrouplink KB310674