- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
Designing an MFC Automation Server
This section shows you how to create your own automation server with MFC. You'll use MFC AppWizard and ClassWizard to quickly and easily create the necessary class and methods for the demonstration.
The example is an in-process DLL server that exposes a class called SimpleMFC. You'll create four methods—Add, Subtract, Multiply, and Divide—to see how an automation server built with MFC works. The first three methods accept two long numbers and return a long (the result of the calculation). The fourth method also accepts two long values but instead returns a double precision number.
Using MFC AppWizard to Create Automation Servers
Choose File | New from the Developer Studio menu, and then choose MFC AppWizard (DLL). In the Project Name edit box, type MFCAuto.
AppWizard presents a single-step process to create the server. Enable the Automation check box and click Finish (see Figure 27.14).
Figure 27.14 Step 1 of 1.
A Closer Look at the MFCAuto Classes
The AppWizard used to generate this project is very simple. The only code it generated is the required CLSID (because you enabled the Automation check box) and the registration of the DLL in the InitInstance() method, as Listing 27.15 shows.
Example 27.15. Registration Code for the Automation Server
BOOL CMFCAutoApp::InitInstance()
{
// Register all OLE server (factories) as running. This enables the
// OLE libraries to create objects from other applications.
COleObjectFactory::RegisterAll();
return TRUE;
}
Adding Customization to the MFCAuto Sample Skeleton
To make the server useable, you'll need to create a class derived from CCmdTarget. This is the root automation class provided by MFC.
Now do the following:
- In the workspace window, select the MFCAuto project, right-click, and choose New Class.
- In the Name text box, type CSimpleMFC.
- In the Base Class combo box, select CCmdTarget.
- For automation type, select Createable by Type ID and make sure that the text in the text box says MFCAuto.SimpleMFC.
Figure 27.15 shows the values for the new class.
Figure 27.15 Adding the CSimpleMFC class.
Notice the addition of a new entry in the workspace window. The CSimpleMFC node is the C++ class you just created. The project's Object Definition Language (ODL) file also has been modified to include information about the new SimpleMFC COM class. The ODL file will be used to create a type library to expose a SimpleMFC class to other applications. To see this ODL file (shown in Listing 27.16), double-click the MFCAuto.ODL icon on the workspace File tab. Included in this file is the UUID for the class. After we finish coding this example, it will include our user-defined methods Add, Subtract, Multiply, and Divide.
Example 27.16. Object Definition Language (ODL) for MFCAuto
// MFCAuto.odl : type library source for MFCAuto.dll
// This file will be processed by the MIDL compiler to produce the
// type library (MFCAuto.tlb).
[ uuid(CF5B9E04-72A3-11D3-B515-CC43834E4167), version(1.0) ]
library MFCAuto
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
// Primary dispatch interface for CSimpleMFC
[ uuid(CF5B9E11-72A3-11D3-B515-CC43834E4167) ]
dispinterface ISimpleMFC
{
properties:
// NOTE - ClassWizard will maintain property information here.
// Use extreme caution when editing this section.
//{ { AFX_ODL_PROP(CSimpleMFC)
//} } AFX_ODL_PROP
methods:
// NOTE - ClassWizard will maintain method information here.
// Use extreme caution when editing this section.
//{ { AFX_ODL_METHOD(CSimpleMFC)
//} } AFX_ODL_METHOD
};
// Class information for CSimpleMFC
[ uuid(CF5B9E12-72A3-11D3-B515-CC43834E4167) ]
coclass SimpleMFC
{
[default] dispinterface ISimpleMFC;
};
//{ { AFX_APPEND_ODL} }
//} } AFX_APPEND_ODL} }
};
The CSimpleMFC class that was generated is fairly simple; however, note the OnFinalRelease() method generated as a result of choosing automation support. As the code documentation states, this notification is called when the last instance of this class is deleted. You can optionally implement this code to perform special cleanup.
Adding Methods to the MFCAuto Example
Listing 27.17 shows the prototypes for the four methods you will be adding. This will come in handy in just a moment.
Example 27.17. CSimpleMFC Instance Method Prototypes
long Add(long First, long Second); long Subtract(long First, long Second); long Multiply(long First, long Second); double Divide(long First, long Second);
Now, add these prototypes with these steps:
- Open ClassWizard by choosing View | ClassWizard, or by pressing Ctrl+W.
- Select the Automation tab in the MFC ClassWizard dialog box. In the Class Name combo box, select CSimpleMFC.
- Click the Add Method button.
- In the External Name combo box, type Add (the Internal Name text box is filled in automatically).
- In the Return Type combo box, select Long.
- Add the first and second methods in the Parameter List list box, using Long as the type for each.
Repeat these steps for each of the four methods and select Double as the return type for the Divide method. You can use Figure 27.16 as a guideline.
Figure 27.16 Using ClassWizard to add methods to MFCAuto.
Now, change the implementation code for these methods, as Listing 27.18 shows.
Example 27.18. Implementing the CSimpleMFC Instance Methods
/////////////////////////////////////////////////////////////////////////////
// CSimpleMFC message handlers
long CSimpleMFC::Add(long First, long Second)
{
return First + Second;
}
long CSimpleMFC::Subtract(long First, long Second)
{
return First - Second;
}
long CSimpleMFC::Multiply(long First, long Second)
{
return First * Second;
}
double CSimpleMFC::Divide(long First, long Second)
{
return (double)First / (double)Second;
}
Take another look at the declaration for the CSimpleMFC class shown in Listing 27.19. ClassWizard has added prototypes for the methods to the ODL file. When this class is registered, the methods automatically are visible as part of the SimpleMFC COM class.
Example 27.19. ClassWizard Changes to the ODL File for New Methods
methods:
// NOTE - ClassWizard will maintain method information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_METHOD(CSimpleMFC)
[id(1)] long Add(long First, long Second);
[id(2)] long Subtract(long First, long Second);
[id(3)] long Multiply(long First, long Second);
[id(4)] double Divide(long First, long Second);
//}}AFX_ODL_METHOD
Testing the MFCAuto Example
Compile the server example and then choose Tools | Register Control to register this automation server with the operating system. You can test the example using any application that can create automation servers.
For the test, I've included a file that VBScript tests that can be run against the MFCAuto example. This script, Test.vbs, runs under the Windows scripting host and calls the automation interfaces in the MFCAuto server. Listing 27.20 provides the contents of Test.vbs.
Example 27.20. An Automation Test File Written in VBScript
' Test.vbs
' VBScript example of an Automation test file.
' This file tests the MFCAuto example.
' The scripting host shell object is used to supply
' interaction with the user.
dim Result, AutoObj, WshShell
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set AutoObj = CreateObject("MFCAuto.SimpleMFC")
Result = AutoObj.Add(1, 2)
WshShell.Popup "1 + 2 = " + cstr(Result)
Result = AutoObj.Subtract(10, 5)
WshShell.Popup "10 - 5 = " + cstr(Result)
Result = AutoObj.Multiply(23, 47)
WshShell.Popup "23 x 47 = " + cstr(Result)
Result = AutoObj.Divide(10, 3)
WshShell.Popup "10 / 3 = " + cstr(Result)
The Test.vbs script file is located in the same directory as the MFCAuto project. To run the script file, double-click the file's icon using Windows Explorer. If the MFCAuto project is registered properly, the script displays a series of dialog boxes containing calculation results.
Summary | Next Section

Account Sign In
View your cart