This documentation is archived and is not being maintained.

QStat Sample: Wraps a COM Object to Expose Business Logic

The QStat sample shows how to create a DLL that wraps access to a COM object and exposes its functionality to .NET Framework clients using C++.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

To build the sample

  1. In the Visual Studio IDE, load the solution file QStat.sln.

  2. In Solution Explorer, right-click the QStat solution.

  3. On the shortcut menu, click Build Solution.

To run the sample

  1. In Solution Explorer, right-click the netClient project.

  2. On the Debug menu, click Start.

Once the clients have started, a series of messages are printed to the console, notifying you of calls into the business logic.


To get more information on the interaction between components, step through the code by either setting breakpoints or using the Debug menu to step into or over the sample code.

The focus of the sample is on accessing the business logic from both COM and .NET Framework clients and not on implementation of the business logic itself, so the business logic is relatively uninteresting. In fact, the business logic in this case does simple arithmetic (geometric, harmonic, and quadratic mean calculations), returning the result to the client.

It is a better programming practice to separate business logic from the scaffolding that exposes that business logic (COM in this case). However, this sample assumes that the business logic has been implemented directly within the COM object that will be consumed and that there is no access to the underlying business logic except by going through this COM object.

The sample creates a wrapper DLL that consumes the COM object via #import and exposes a .NET Framework class to be consumed by .NET Framework clients. This is done by defining a native C++ class, nativeMeanCalc, which contains a COM smart pointer data member brought in through #import. A .NET Framework class is defined that contains a pointer to the native C++ class. The .NET Framework class, netMeanCalc, contains a pointer to a nativeMeanCalc object, which completes the pathway to the destination COM object. netMeanCalc is the public class that the .NET Framework clients will consume.

There are two client projects:

  • nativeClient is a COM client and is implemented in native C++.

  • netClient is written using the new syntax for C++.

This sample demonstrates the following keywords:

__gc; #using; #import; try; catch; _com_error; Console::WriteLine