16. Object-oriented Programming Foundations

Appendix C - Dynamic Link Libraries

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

An Application that Calls a DLL
In this section, you will build an application designed to take advantage of the FRAMER.DLL dynamic link library. This application, named DLLDemo, will make a single call to the DateAndTime( ) function in the DLL created in the previous section.
Use the following steps to create the DLLDemo base code with the AppWizard:
  1. Use the Visual C++ File | New menu option to bring up the New dialog box, as shown in Figure C-5.
Figure C-5: Use the New dialog box to create a new MFC AppWizard application named DLLDemo
  2. Name the project DLLDemo. Click OK to start the MFC AppWizard.
  3. Follow the normal project creation steps and create an application with a single-document interface using the Document/View architecture support as shown in Figure C-6.
Figure C-6: Select a single document interface for this project using the Document/view architecture
  4. Use wizard defaults for all other steps and build as a shared DLL.
  5. Review and accept the classes as shown in the review list. Click the Finish button to generate the project files.
It is now up to us to add the application-specific code to the above base code. As you will recall from Chapters 24 through 26, the AppWizard generated numerous files to support each application. In this case, there will be five source code files and their associated header files.
There are two files that are of interest to us. The first is DLLDEMOVIEW.H and the second is DLLDEMOVIEW.CPP.
The DLLDEMOVIEW.H Header File
The DLLDEMOVIEW.H header file is used to hold any function prototypes that we wish to import. In this example, DateAndTime( ) is the only function we wish to use. Here is a partial listing of this file.
// DLLDemoView.h : interface of the CDLLDemoView class
//
/////////////////////////////////////////////////////////////
  .
  .
  .
extern void WINAPI DateAndTime( );

class CDLLDemoView : public CView
{
  .
  .
  .
The extern keyword alerts the compiler that this function is external to the body of the current program. During the build process, the linker will look for this function. If the linker cannot find DateAndTime( ) in an appropriate library, you will receive a short but sweet error message.
The DLLDEMOVIEW.CPP Source Code File
In order to handle WM_PAINT messages, you’ll want to add the OnPaint( ) member function and associated message handlers. To do this, follow these steps:
  1. Use the View | ClassWizard… menu selection to open the MFC ClassWizard dialog box.
  2. Select WM_PAINT from the Messages list box and add the OnPaint( )
member function.
  3. Double-click the mouse on the OnPaint( ) member function to add and edit the member-specific code.
The following is a partial listing of the DLLDEMOVIEW.CPP source code file. The code specific to the OnPaint( ) member function is the only portion of code that is altered from the original listing. The file contains a single line of code in a bold font. This is the location where the DateAndTime( ) DLL function is called.
// DLLDemoView.cpp : implementation of CDLLDemoView
//

  .
  .
  .

/////////////////////////////////////////////////////////////
// CDLLDemoView message handlers
void CDLLDemoView::OnPaint( )
{
 CPaintDC dc(this); // device context for painting
 
 dc.TextOut(280,100,"Send a little text to the Window",32);

 // Call the DLL
 DateAndTime( );
 
 // Do not call CView::OnPaint( ) for painting messages
}
Before building this project, there is one more critical step that must be taken. The DLL FRAME.LIB must be identified so the linker can resolve the external functions. This is done using the compiler’s Project | Settings… menu selection to open the Project Settings dialog box. Figure C-7 shows this dialog box and the Link tab selected.
Figure C-7: Use the Link tab to identify the FRAME.LIB in the linker’s object/library module
You can now build the application by making the appropriate build selection from the compiler’s Build menu.
Run the program from within the IDE. You should see a screen similar to Figure C-8.
Figure C-8: DLLDemo shows a client screen with the DateAndTime message box
The DLLDemo application will draw the message box on the screen anytime a WM_MOUSE message is received. This action allowed us to keep the application as simple as possible and yet it demonstrates all of the steps necessary in incorporating a DLL.
More DLLs?
DLLs are considered an advanced programming topic by most developers. We included this appendix because of our discussion of ActiveX controls, which are really small DLLs.
If you are interested in expanding your knowledge of DLLs, we can only recommend one book. It is Steve Holzner’s Advanced Visual C++ Programming (by M&T Books, 1996). Steve devotes a whole chapter to this advanced but important topic.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net