home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Linking to the New DLL

First, we include the prototype for MultiplyByTwo() by including the DLL project’s header file, DoublerDLL.h, in Doubler’s view.

// DoublerView.cpp : implementation of the CDoublerView class
//

#include “stdafx.h”
#include “Doubler.h”

#include “DoublerDoc.h”
#include “DoublerView.h”
#include “DoublerDLL.h”                                             ⇐
    .
    .
    .

Next, copy the actual DLL, DoublerDLL.dll, to the c:\windows\system directory so the Doubler program knows where to search for it. We also have to link the DoublerDLL.lib library file into Doubler so that our project knows how DoublerDLL.dll is structured. We link DoublerDLL.lib into the Doubler project by selecting the Settings item in the Project menu, which opens the Project Settings box, as shown in Figure 10.3.


Figure 10.3  Linking in a .lib file.

Select the Link tab in the Project Settings box and enter “.\DoublerDLL.lib” in the Object/library modules box. This indicates that we want to link this new library. Click OK to close the Project Settings box. Copy DoublerDLL.lib to the Doubler directory (e.g., c:\avc\Doubler). Now we’re free to use MultiplyByTwo() as well as other library routines.

Next, in OnFileDoublethevalues(), we place the newly doubled values in a formatted text string.

void CDoublerView::OnFileDoublethevalues()
 {
    int value1 = 1;
    int value2 = 2;

    int result1 = MultiplyByTwo(value1);
    int result2 = MultiplyByTwo(value2);

    char FormattedString[80];                                       ⇐
    wsprintf(FormattedString, “2 times %d is %d, 2 times %d is %d.”, value1,
result1, value2, result2);                                          ⇐
        .
        .
        .

}

Then, we place that formatted string into a CString object named text in the document object and invalidate the view.

void CDoublerView::OnFileDoublethevalues()
 {
    int value1 = 1;
    int value2 = 2;

    int result1 = MultiplyByTwo(value1);
    int result2 = MultiplyByTwo(value2);

    char FormattedString[80];
    wsprintf(FormattedString, “2 times %d is %d, 2 times %d is %d.”, value1,
result1, value2, result2);

    CDoublerDoc* pDoc = GetDocument();                              ⇐
    ASSERT_VALID(pDoc);                                             ⇐
    pDoc->text = FormattedString;                                   ⇐

    Invalidate();                                                   ⇐

}

To display the new text object stored in the document, we add this code to OnDraw():

void CDoublerView::OnDraw(CDC* pDC)
 {
    CDoublerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDC->TextOut(0, 0, pDoc->text);                                 ⇐

}

Run Doubler now and select the Double the values item in the File menu. When you do, the new, doubled values are displayed, as shown in Figure 10.4. Now we’re using DLLs; our first example is a success. You can generalize the process from here, passing parameters to and returning values from DLL functions. AppWizard has made the process relatively simple.

The code for this example, DoublerDLL.h and DoublerDLL.cpp, appears in Listing 10.1. DoublerView.h and DoublerView.cpp appear in Listing 10.2.


Figure 10.4  Using calls to a custom DLL.


Listing 10.1 DoublerDLL.h and DoublerDLL.cpp

// DoublerDLL.h : main header file for the DOUBLERDLL DLL
//

#if
!defined(AFX_DOUBLERDLL_H__A45E4A4C_A79A_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_DOUBLERDLL_H__A45E4A4C_A79A_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

)

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

#include “resource.h”        // main symbols

#define DLLexport    __declspec( dllexport )

DLLexport int WINAPI MultiplyByTwo(int DoubleMe);

/////////////////////////////////////////////////////////////////////////////
// CDoublerDLLApp
// See DoublerDLL.cpp for the implementation of this class
//

class CDoublerDLLApp : public CWinApp
{
public:
    CDoublerDLLApp();

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CDoublerDLLApp)
    //}}AFX_VIRTUAL

    //{{AFX_MSG(CDoublerDLLApp)
        // NOTE - the ClassWizard will add and remove member functions here.
        //    DO NOT EDIT what you see in these blocks of generated code !
  //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_DOUBLERDLL_H__A45E4A4C_A79A_11D1_887F_D42B07C10710__INCLUDED_)


// DoublerDLL.cpp : Defines the initialization routines for the DLL.
//

#include “stdafx.h”
#include “DoublerDLL.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.
//

/////////////////////////////////////////////////////////////////////////////
// CDoublerDLLApp

BEGIN_MESSAGE_MAP(CDoublerDLLApp, CWinApp)
    //{{AFX_MSG_MAP(CDoublerDLLApp)
        // 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()

/////////////////////////////////////////////////////////////////////////////
// CDoublerDLLApp construction

CDoublerDLLApp::CDoublerDLLApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDoublerDLLApp object

CDoublerDLLApp theApp;

DLLexport int WINAPI MultiplyByTwo(int DoubleMe)
{
    int DoubledValue = 2 * DoubleMe;
    char Text[80];
    sprintf(Text, “MultiplyByTwo() is about to double %d and return %d.”, DoubleMe, DoubledValue);
    AfxMessageBox(Text);
    return DoubledValue;
}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.