MECalculator Sample: Demonstrates Using .NET Components in Native C++ Code
This documentation is archived and is not being maintained.

MECalculator Sample: Demonstrates Using .NET Components in Native C++ Code

Visual Studio .NET 2003

MECalculator illustrates the use of .NET components by native clients through COM with a type library automatically generated from .NET metadata. The application is implemented as a Windows console application. It uses a .NET assembly that contains two managed classes: MECalcSrvClass and MECalcSrvClassEx. Using the Regasm.exe utility, the assembly is registered as a COM server that exposes these classes as COM classes.

Building and Running the Sample

To build this sample

  1. In the Visual Studio IDE, load the solution file MECalculator.sln.
  2. In Solution Explorer, right-click the MECalculator solution.
  3. On the shortcut menu, click Build Solution.

To run the example

  1. In Solution Explorer, right-click the CalcClient project.
  2. On the Debug menu, click Start.
    Note   To step into MECalcSrv.dll, you need to specify Mixed in the Debugging property page for the CalcClient project.

Project Description

Both classes use .NET interoperability attributes from the System::Runtime::InteropServices namespace to specify how the COM classes and interfaces should be exposed.

MECalcSrvClass uses the ProgIdAttribute attribute to indicate the ProgID that should be used for this class and GuidAttribute for the class ID (CLSID). Otherwise, a new CLSID will be generated every time that Regasm.exe is started. Also, ClassInterfaceAttribute is used to instruct Regasm.exe to try to wrap the functions from this class in a dual interface.

MECalcSrvClassEx will inherit a managed interface (IMECalcItf) that uses .NET attributes to specify the interface ID (IID) and the way that the interface should be exposed to COM (in this case, also as a dual interface). This way, a predefined IID can be used for the interface and not one that is generated by Regasm.exe.

When you run CalcClient.exe, it will instantiate both classes and call several functions from the interfaces exposed to COM.

Note   In both cases, the .NET properties will be exposed as COM properties.

Classes and Keywords

This sample demonstrates the following classes and keywords:

System::Runtime::InteropServices::ProgIdAttribute; System::Runtime::InteropServices::GuidAttribute; System::Runtime::InteropServices::ClassInterfaceAttribute; System::Runtime::InteropServices::InterfaceTypeAttribute; System::Runtime::InteropServices::ClassInterfaceType; System::Runtime::InteropServices::ComInterfaceType

See Also

Managed Extensions for C++ Samples

© 2016 Microsoft