Share via


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 intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

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.

Note

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.

Project Description

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

Classes and Keywords

This sample demonstrates the following keywords:

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

See Also

Other Resources

Interoperability Samples