CALCDRIV Sample: Demonstrates an Automation Client Application
The CALCDRIV sample is a simple Automation (formerly OLE Automation) client application. CALCDRIV drives the MFCCALC sample application, which is an Automation server that provides basic calculator functions. MFCCALC has a simple calculator interface that looks like the Calculator application that comes with Microsoft Windows.
Building and Running the Sample
To build and run the CALCDRIV sample
- Follow the instructions in MFCCALC to build and run that project.
- Open the solution calcdriv.sln
- On the Build menu, click Build.
Note If you do not build and register MFCCALC before building CALCDRIV, you get an "Unable to create 'MFCCALC.Application' object" message.
When you run CALCDRIV, the Microsoft Foundation Class Library (MFC) Calc Driver dialog box appears and the MFCCALC application is launched. You can use MFCCALC's calculator user interface directly at this point, or you can drive MFCCALC from CALCDRIV as follows:
- In MFC Calc Driver's Expression box, provide two or more numbers separated by the operator +, -, *, or /.
Note MFCCALC does not interpret parentheses and does not implement operator precedence.
- Click Go to have MFCCALC evaluate the expression in one step.
- Click Single Step to step through the expression one number or operator at a time.
- Click Refresh to request state information for MFCCALC (Last Accum and Last Operand).
Using a Dispatch Class
CALCDRIV uses the CRemoteCalcDlg class as follows:
- The CRemoteCalcDlg class of CALCDRIV represents the dispatch interface of MFCCALC. Class CRemoteCalcDlg is derived from CCmdTarget, which has a few automation-specific member functions, such as CreateDispatch.
- CDriverDlg embeds a CRemoteCalcDlg object,
m_calc. The CRemoteCalcDlg object, like the
CDriverDlgobject in which it is embedded, is alive for most of the duration of CALCDRIV. The CRemoteCalcDlg is constructed when the dialog object is constructed.
- CDriverDlg::OnInitDialog calls CCmdTarget::CreateDispatch for the CRemoteCalcDlg object. CreateDispatch requires the dispatch name as the first parameter. Typically, the developer of an automation server application provides documentation describing the names of the dispatch interfaces and the properties and methods of the interfaces. Another way to find the names of the dispatch interfaces of an automation server application is to look at the server's Windows registration, using REGEDIT /v (the verbose option).
- CDriverDlg implements CALCDRIV's expression evaluator by calling the
Buttonmethod of MFCCALC, which is an emulator for the various buttons in the calculator's dialog box.
- CDriverDlg implements CALCDRIV's
Refreshfunction by calling the
GetAccummethods of MFCCALC.
- The CDriverDlg destructor calls the
Quitmethod exposed by MFCCALC to shut down MFCCALC when CALCDRIV closes.
This sample demonstrates the following keywords:
AfxMessageBox; AfxOleInit; CDialog::DoModal; CDialog::EndDialog; CDialog::OnInitDialog; CEdit::GetSel; CEdit::SetSel; COleDispatchDriver::AttachDispatch; COleDispatchDriver::CreateDispatch; COleDispatchDriver::GetProperty; COleDispatchDriver::InvokeHelper; COleDispatchDriver::SetProperty; CString::GetLength; CWinApp::InitInstance; CWnd::DoDataExchange; CWnd::GetWindowText; CWnd::SetWindowText; GetWindowText; afxMemDF; min; wsprintf
Note Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.