Program examples compiled using Visual C++ 6.0 compiler on Windows XP Pro machine with Service Pack 2. The Excel version is Excel 2003/Office 11. Topics and sub topics for this tutorial are listed below. Don’t forget to read Tenouk’s small disclaimer. The supplementary notes for this tutorial are mymfc29C.xls, automation, variant and COlevariant class.
The MYMFC29C SDI Automation Component - EXE Example: with User Interface
This Automation component example illustrates the use of a document component class in an SDI application in which a new process is started for each object. This component program demonstrates an indexed property plus a method that constructs a new COM object.
The first Automation component example you saw, MYMFC29A, didn't have a user interface. The global class factory constructed a CBank object that did the component's work. What if you want your EXE component to have a window? If you've bought into the MFC document-view architecture, you'll want the document, view, and frame, with all the benefits they provide.
Suppose you created a regular MFC application and then added a COM-creatable class such as CBank. How do you attach the CBank object to the document and view? From a CBank class member function, you could navigate through the application object and main frame to the current document or view, but you'd have a tough time in an MDI application if you encountered several component objects and several documents. There is a better way. You make the document class the creatable class, and you have the full support of AppWizard for this task. This is true for both MDI and SDI applications.
The MDI MSDN’s Autoclik example demonstrates how COM triggers the construction of new document, view, and child frame objects each time an Automation client creates a new component object. Because the MYMFC29C example is an SDI program, Windows starts a new process each time the client creates an object. Immediately after the program starts, COM, with the help of the MFC application framework, constructs not only the Automation-aware document but also the view and the main frame window. Now is a good time to experiment with the MYMFC29C application, which was first generated by AppWizard with the Automation option checked. It's a Windows-based alarm clock program designed to be manipulated from an Automation client such as Excel. MYMFC29C has the following properties and methods.
Here are the steps for building and running an already completed MYMFC29C.
From Visual C++, open the workspace mymfc29C.dsw or any directory that you have designated for your project. Build the project to produce the mymfc29C.exe file in the project's Debug subdirectory.
Run the program once to register it. The program is designed to be executed either as a standalone application or as an Automation component. When you run it from Windows or from Visual C++, it updates the Registry automatically and displays the face of a clock with the characters XII, III, VI, and IX at the 12, 3, 6, and 9 o'clock positions. Exit the program.
Next, load the mymfc29C.xls Excel workbook file. The worksheet should look something like the one shown here.
Figure 1: mymfc29C.xls, a client, to test MYMFC29C component.
Click the Load Clock button, and then double-click the Set Alarm button. There could be a long delay after you click the Load Clock button, depending on your system. The clock should appear as shown below, with the letter A indicating the alarm setting.
Figure 2: Alarm is set through a client.
If you've started the component program from the debugger, you can watch the Debug window to see when InitInstance() is called and when the document object is constructed.
MYMFC29C From Scratch
To experience a real programming, the following are steps to build MYMFC29C from scratch. Have a good practice! This is an SDI application with Automation support.
Figure 3: Visual C++ new project dialog.
Select the Single Document.
Figure 4: MYMFC29C – AppWizard step 1 of 6, SDI application.
Figure 5: MYMFC29C – AppWizard step 2 of 6.
Don’t forget to select Automation option.
Figure 6: MYMFC29C – AppWizard step 3 of 6.
Figure 7: MYMFC29C – AppWizard step 4 of 6.
Click the Advanced button. Here, we just use the default settings for Localized strings. Take note that the Doc type name has the maximum six characters.
Figure 8: MYMFC29C – AppWizard default Localized strings.
Figure 9: MYMFC29C – AppWizard step 5 of 6.
Figure 10: MYMFC29C – AppWizard step 6 of 6.
Figure 11: MYMFC29C project summary.
Let create our own icon for this project. Before that, disable the IDR_MFC29CTYPE icon by enclosing it in double quotes.
Figure 12: Disabling the default icon.
You can verify it through the View Resource Symbols menu.
Figure 13: Viewing resource symbols.
Figure 14: MYMFC29C resource symbols.
Modify the icon for both the Standard (32 x 32) and Small (16 x 16).
Figure 15: Standard icon.
Figure 16: Small icon.
Create a new CAlarm class using ClassWizard.
Figure 17: Creating and adding new class to a project using ClassWizard.
Don’t forget to select the Automation support option.
Figure 18: Entering CAlarm class information.
Let do a clean-up by rebuilding the ClassWizard database. Delete the CLW file under the project directory.
Figure 19: Deleting the ClassWizard database file.
Then launch ClassWizard, the following prompt will be displayed. Just click the Yes button for the following prompt.
Figure 20: Building ClassWizard database prompt.
And click the OK button for the following dialog.
Figure 21: ClassWizard database building files inclusion dialog.
Figure 22: CAlarm class in ClassWizard.
Add property to CAlarm without notification function. Select the Automation tab of the ClassWizard. Then click the Add Property button.
Figure 23: Adding property and method through Automation page of ClassWizard.
Add Time property, fill up as shown below.
Figure 24: Adding Time property.
In Alarm.h, replace:
Modify the protected constructor to become public (or you can use friend keyword) and add the following parameter.
In Alarm.cpp change:
Then, modify the CAlarm constructor as shown below.
TRACE("Entering CAlarm ctor -- vaTime = %f\n", (double) time);
m_time = time;
Add the following properties and methods for the CMymfc29CDoc class through the Automation tab of the ClassWizard.
DATE property that holds a COM DATE (m_time)
Indexed VARIANT property for the four figures on the clock face (m_strFigure[ ])
Method that invalidates the view window and brings the main frame window to the top (Refresh)
Method that displays the application's main window (ShowWin)
Method that creates a CAlarm object and returns its IDispatch pointer (CreateAlarm)
Launch ClassWizard and click the Automation tab, select CMymfc29CDoc in Class name combo box and click Add Property button and fill in the information as shown in the following Figures.
Figure 25: Adding property through Automation page of ClassWizard.
Fill up the property information as shown in the following Figures.
Figure 26: Time property.
Figure 27: Figure property.
Click the Add Method button and add methods as shown in the following Figures.
Figure 28: RefreshWin method.
Figure 29: ShowWin method.
Figure 30: CreateAlarm method.
Figure 31: The added properties and methods to CMymfc29Doc class.
Add the SaveModified() message handler to CMymfc29CDoc class.
Figure 32: Adding SaveModified() message handler to CMymfc29CDoc class.
Further reading and digging:
MSDN What's New (MFC Feature Pack) - feature pack.
DCOM at MSDN.
COM+ at MSDN.
COM at MSDN.