# 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. This is accomplished by using C++/CLI.

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

# 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 C++/CLI.

# Classes and Keywords

This sample demonstrates the following keywords:

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