The AppWizard added the file
AutoServer.odl’ to our AutoServer project. The file
extension ‘.odl’ stands for Object Description
language. This file contains information about the COM component
CMyMath and its dispinterface IMyMath. We will
analyze this information without going into the specifics of ODL. The file is compiled using MIDL compiler, which generates
the type library for the automation server. The UUID identifies
the type library itself. . The DevStudio can use the compiled
type information in ‘AutoServer.tlb’ to generate
local class IMyMath that mimics the COM class
CMyMath. An automation controller can access MyAdd(
) and MySubtract( ) member functions of the local
IMyMath class instead of talking to the automation
server through IDispatch. The DevStudio-generated
implementation of the local IMyMath class’s
MyAdd( ) and MySubtract( ) functions
communicates with the automation server through IDispatch to
perform the actual task. The programmer in charge of building
the automation controller is thus relieved from the intricacies
of dealing with VARIANTS and IDispatch::Invoke( ) etc.
This article uses 'AutoServer.tlb’ to simplify the task of
using AutoServer. Go through the following steps to build the
controller.
(a) Use MFC AppWizard (exe) to
create a dialog-based application named autoclient2 Un-select
everything in step 2 of 4. Accept defaults for everything
else.
(b) Use the ClassWizard to add a new class from a
type library. Choose the type library for CMyMath. It
is located in the Debug folder of MathAutoServer project under
the name ‘AutoServer.tlb’ file. Select OK on the
“confirm class” dialog box that props up. The class
wizard creates a class IMyMath derived from the MFC
class COleDispatchDriver.
(c) Add a member variable
m_MyMath of type IMyMath to the dialog class
CAutoClient2Dlg. Add the following lines to
CAutoClient2Dlg::OnInitDialog( ) member in the
application specific initialization section.
CoInitialize(NULL) ;
if ( !math.CreateDispatch ( "AutoServer.MyMath" ) )
AfxMessageBox ( "AutoServer.MyMath Not found" ) ;
(d)
Using the resource editor, design the dialog box
IDD_AUTOCLIENT2_DIALOG. Our dialog box looks as shown in Figure
shown below. Using class wizard, associate the edit boxes
against x and y with dialog members
m_x and m_y respectively. Associate the edit
box near Result with the dialog member m_z. Link the
buttons 'Add' and 'Subtract' with dialog member functions
OnButton1( ) and OnButton2( ). Implement these
two functions as shown in the listing.
void CAutoclient2Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData ( TRUE ) ;
m_z = math.MyAdd ( m_x, m_y ) ;
UpdateData ( FALSE ) ;
}
void CAutoclient2Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
UpdateData ( TRUE ) ;
m_z = math.MySubtract ( m_x, m_y ) ;
UpdateData ( FALSE ) ;
}
(e) Compile the application and run it. Specify some
values for x and y. Click 'Add' or 'Subtract' button. The result
appears in the Result box.