MFCCALC Sample: Demonstrates an Automation Server Application (C++ Samples)

MFCCALC is an Automation server. It implements a simple calculator similar to the CALC application in Windows. It can be driven via Automation by running its CalcDriv project, or it can be run standalone by running its MfcCalc project.

This sample is similar to the MFC samples MFCCALC and CALCDRIV except that it uses the native compiler COM support and it combines the calculator and driver in a single sample.

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.

Building and Running the Sample

To build and run this sample

  1. Open the solution mfccalc.sln.

  2. On the Build menu, click Build Solution.

  3. Set MfcCalc as the startup project (right-click the project node and click Set as StartUp Project).

  4. On the Debug menu, click Start Without Debugging. This will run the calculator standalone. Close the MfcCalc application before continuing.

  5. Now run the calculator using Automation. Set CalcDriv as the startup project and click Start Without Debugging. Type an expression in the MFC Calc Driver dialog box and click the Go button to run the calculator.

Comparison with the MFC Version

For more information, see the corresponding MFC samples MFCCALC and CALCDRIV.

The differences between this sample and the MFC samples:

  • The COleDispatchDriver class is no longer necessary. Instead, #import is used to import a type library.

  • _bstr_t and _variant_t wrapper classes are used to simplify operations of BSTR and VARIANT types.

  • __declspec(property) is used to simplify assignment operations for properties of COM objects.

  • COM error handling is now performed by using the _com_error class.

With the Visual C++ native compiler COM support, the resulting sample code is shorter and more efficient. The MFC-based MFCCALC sample uses dispinterfaces only. Compare this sample with the MFC version to see the differences in source code.

Keywords

This sample demonstrates the following keywords:

dispinterface; #import; _com_ptr_t; _variant_t; _bstr_t; _com_error

See Also

Tasks

MFCCALC Sample: Demonstrates an Automation Server Application

CALCDRIV Sample: Demonstrates an Automation Client Application

Reference

Compiler COM Support

Other Resources

Compiler COM Support Samples