3. Writing, Compiling, and Debugging Simple Programs

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 MFC-based Dynamic Link Library
An MFC-based DLL can be created and compiled in a manner similar to the MFC Windows applications of Chapters 24 through 26 in this book, but with some subtle changes. To build the example FRAMER.DLL dynamic link library, use the AppWizard to create all necessary header, resource, and source code files. Follow these steps to complete the Framer project.
  1. Use the Visual C++ File | New menu option to bring up the New dialog box, as shown in Figure C-1.
Figure C-1: The New dialog box allows you to create a MFC AppWizard(DLL) project
  2. Name the new MFC AppWizard (DLL) project Framer.
  3. Click OK to start the MFC AppWizard (DLL).
  4. For step 1 of the DLL AppWizard, make sure the Regular DLL using shared MFC DLL option is selected, as shown in Figure C-2.
Figure C-2: Use the AppWizard’s first step to select the shared MFC DLL option
  5. For remaining steps, use the defaults suggested by the AppWizard.
  6. Click Finish, review the options as shown in Figure C-3, then generate the base code for the project by selecting OK.
Figure C-3: View the project information in the New Project information dialog box
When the AppWizard creates the base code for the Framer project, your subdirectory should contain the files shown in Figure C-4.
Figure C-4: The left pane shows the files created by the AppWizard for the framer DLL project
Like other AppWizard templates, the code that was generated is functional—it just doesn’t do anything for us until we add our own unique code.
The two files that are of greatest interest to us are the FRAMER.H and FRAMER.CPP files.
The FRAMER.H Header File
The FRAMER.H header file is used to hold any function prototypes that we wish to export. In this example, DateAndTime( ) is the only function we’ve included in this code. Here is a partial listing of this file.
// Framer.h : main header file for the FRAMER DLL
//

  .
  .
  .
#ifndef __AFXWIN_H__
 #error include ‘stdafx.h’ before including this file for PCH
#endif

#include “resource.h”               // main symbols


__declspec( dllexport ) void WINAPI DateAndTime();
////////////////////////////////////////////////////////////////
// CFramerApp
// See Framer.cpp for the implementation of this class
//

class CFramerApp : public CWinApp
  .
  .
  .
Microsoft uses the extended attribute syntax—for example, __declspec—for simplifying and standardizing Microsoft-specific extensions to the C++ language. Here, the __declspec keyword indicates that an instance of the type will be stored with a Microsoft-specific storage-class attribute.
The explicit use of the dllexport keyword has eliminated the need for EXPORT statements in the module definition file (FRAMER.DEF). Developers of C-based DLLs are familiar with the practice of identifying all exported functions in the module definition file. That practice is now outdated.
The FRAMER.CPP Source Code File
The specific DLL code is added to the FRAMER.CPP file. In the following complete listing, that code is shown in a bold font:
// Framer.cpp : The initialization routines for the DLL.
//

#include “stdafx.h”
#include “Framer.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
//  Note!
//
//    If this DLL is dynamically linked against the MFC
//    DLLs, any functions exported from this DLL which
//    call into MFC must have the AFX_MANAGE_STATE macro
//    added at the very beginning of the function.
//
//    For example:
//
//    extern “C” BOOL PASCAL EXPORT ExportedFunction()
//    {
//      AFX_MANAGE_STATE(AfxGetStaticModuleState());
//      // normal function body here
//    }
//
//    It is very important that this macro appear in each
//    function, prior to any calls into MFC.  This means that
//    it must appear as the first statement within the
//    function, even before any object variable declarations
//    as their constructors may generate calls into the MFC
//    DLL.
//
//    Please see MFC Technical Notes 33 and 58 for additional
//    details.
//
/////////////////////////////////////////////////////////////
// CFramerApp

BEGIN_MESSAGE_MAP(CFramerApp, CWinApp)
 //{{AFX_MSG_MAP(CFramerApp)
   // NOTE - the ClassWizard will add and remove mapping
   // macros here.
   //    DO NOT EDIT what you see in these blocks of
   // generated code!
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////
// CFramerApp construction

CFramerApp::CFramerApp()
{
 // TODO: add construction code here,
 // Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////
// The one and only CFramerApp object

CFramerApp theApp;

__declspec( dllexport ) void WINAPI DateAndTime()
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState());

 // get current date and time information

 struct tm *date_time;
 time_t timer;

 time(&timer);

 date_time=localtime(&timer);
 
 const CString& strtime = asctime(date_time);

 // Draw a message box to the window

 AfxMessageBox(strtime, MB_OK, 0);
}
As you learned while examining this listing, if this DLL is dynamically linked against the MFC DLLs, certain considerations must be made. Specifically, all exported functions that call into the MFC must have the AFX_MANAGE_STATE macro added at the start of the function.
The next six lines of code are used to retrieve the date and time information from the system. This information is then placed in a string, strtime.
When a call is made to this DLL, the DLL will in turn draw a message box to the window. The message box reports the date and time that the DLL was called. The message box can be canceled by clicking on the OK button.
Building the FRAMER.DLL
Build the DLL by selecting the appropriate build option from the compiler’s Build menu. When the build cycle is complete, the DEBUG subdirectory will contain several important files.
The FRAMER.DLL is the dynamic link library, and FRAMER.LIB is the associated library. Both files must be placed in specific locations:
  Copy FRAMER.DLL to your Windows subdirectory containing system DLLs. This is usually C:\WINDOWS\SYSTEM.
  Copy FRAMER.LIB to the DEBUG subdirectory of the application that will use the DLL. The subdirectory for this example will be named C:\DLLDEMO\DEBUG.
In order to test the DLL, we will have to build a standard MFC application and call the DLL.

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