Creating the MFC Client to Access Your ATL Object


Now its time to use the ATL object you just created. For this step, you are going to create a simple dialog in an MFC project. With your ATLAttributes project still open, click New, New Project from the File menu. Select the Visual C++ project type and click the MFC Application template on the right side. Give your project the name MFCClient. Before you close this dialog, click the Add to Solution radio button underneath the Location field. Close the dialog by clicking OK. In the MFC Application Wizard dialog that is displayed, click the Application Type option on the left and select the Dialog radio button for the Application Type setting. Leave the other settings as they are and click Finish to create the project.

For this project, you will use the static text field already provided for you. First of all, click the static text field and set its ID property to IDC_OUTPUT using the prop. However, remove the two buttons and add a different button. Set the new button's Caption property to Hello and its ID property to IDC_HELLO using the Property Viewer. Arrange the controls in the dialog and resize the dialog so that it looks similar to Figure 15.5.

Figure 15.5. Designing the MFC Client.


To set up a message handler for the button, double-click it to create the OnBnClickedHello function. This function will first create the ATL object you created earlier. It will then ask for the interface, IAttributeTest, that contains the HelloWorld interface method. It will then finish by calling that method and displaying the results in the static text box. The OnBnClickedHello function appears in Listing 15.7.

Listing 15.7 Implementing the Button Handler for the MFC Dialog
 1: void CMFCClientDlg::OnBnClickedHello()  2: {  3:    CComPtr<IUnknown> spUnknown;  4:    HRESULT hr = spUnknown.CoCreateInstance(__uuidof(CAttributeTest));  5:        CComPtr<IAttributeTest> pI;  6:        spUnknown.QueryInterface(&pI);  7:  8:    TCHAR sTest[ 256 ];  9:    pI->HelloWorld( sTest ); 10: 11:    SetDlgItemText( IDC_OUTPUT, sTest ); 12: } 

Upon close examination of the OnBnClickedHello function, you may have noticed the use of the data types CAttributeTest and IAttributeTest without having specified where these declarations come from. At the top of the same file, you will need to import the type library that is created from your ATL project. Add the following code snippet to the top of this file after the initial preprocessing instructions:

 #import "..\ATLAttributes\_ATLAttributes.tlb" no_namespace using namespace std; 

The last step for your MFC client is to initialize the COM library so that your application doesn't fail when trying to create COM objects. In the OnInitDialog function in the MFCClientDlg.cpp file, add the following code snippet:


You can now build your solution and run MFCClient.exe. Clicking the Hello button should then call the HelloWorld function in the IAttributeTest interface and set the static text box accordingly, as shown in Figure 15.6.

Figure 15.6. Running the MFCClient application.



Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: