A C2871 compiler error occurs when you add a reference to a COM component in managed Visual C++ .NET (820648)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)

SYMPTOMS

You may use Component Object Model (COM) components from a Managed C++ application by using the Add Reference feature in Visual C++ .NET 2003. The Add Reference feature creates an interop assembly. You may use using namespace to import the types that are contained in a namespace. However, when you try to import the namespace, you may receive the following compiler error message:
error C2871: 'COM Component namespace' : a namespace with this name does not exist

CAUSE

The namespace of the COM component is nested under the Interop namespace. Refer the Interop namespace before any COM component namespace. When you use the Add Reference feature, Visual C++ .NET does not put using namespace Interop; in a source file.

RESOLUTION

To resolve the problem, import the Interop namespace, and then import the namespace of the COM component. For example, if you reference the Microsoft Scripting Runtime library, you can use the following code to resolve the problem:
using namespace Interop;
using namespace Scripting;

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET 2003.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, expand Visual C++ Projects, and then click .NET.
  4. Click Console Application (.NET) under Templates.
  5. In the Name text box, type Sample, and then click OK.
  6. In Solution Explorer, right-click Reference, and then click Add Reference.
  7. In the Add Reference dialog box, click the COM tab.
  8. In the Component Name list, click Microsoft Scripting Runtime, and then click OK.
  9. Open the Sample.cpp file, and then add the following code by using the System namespace:
    using namespace Scripting;
  10. Press the CTRL + SHIFT + B key combination to build the solution. You may receive the compilation error that the "Symptom" section describes.

Modification Type:MajorLast Reviewed:1/6/2006
Keywords:kbtshoot kberrmsg kbide kbNameSpace kbCOMInterop kbprb KB820648 kbAudDeveloper kbAudITPRO