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 Search this book:
Previous | Table of Contents | Next
#### Exporting a Function from a DLL We want to export a new function, MultiplyByTwo(), from our DLL. There are two steps in creating this function. First, we declare the function in the DLL’s header file, DoublerDLL.h. Then, we write the code for the function in DoublerDLL.cpp. We start by declaring the function in the DLL’s header, DoublerDLL.h. To do that, we define a new symbol, DLLexport, that will stand for the standard export preface, __declspec(dllexport).
    // 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 )                        ⇐
        .
        .
        .
Now we declare our function, indicating that it takes an integer, returns an integer, is an exported function, and uses the WINAPI calling mechanism. (This mechanism is based on the Pascal calling protocol, not the C protocol, because the Pascal protocol is considered more robust.)
    // 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);                   ⇐
We’ve declared our function. The next step is to create the code for that function. We do that at the end of the DoublerDLL.cpp file, placing our new function just after the CDoublerDLLApp object that forms the basis of the DLL.
    /////////////////////////////////////////////////////////////////////////////
    // The one and only CDoublerDLLApp object

    CDoublerDLLApp theApp;

    DLLexport int WINAPI MultiplyByTwo(int DoubleMe)                    ⇐
    {                                                                   ⇐

    }                                                                   ⇐
If we were planning to use any MFC functions in our new function, we would start with the AFX_MANAGE_STATE() macro, which is necessary to set up the MFC connection. If we did use MFC functions, this would be the first line of our new function (see the comment in DoublerDLL.cpp on the following page that has been placed there by AppWizard on AFX_MANAGE_STATE()):
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
However, we do not use the MFC in this simple function, so we start by doubling the value passed to us and storing the new value as an integer named DoubledValue.
    /////////////////////////////////////////////////////////////////////////////
    // The one and only CDoublerDLLApp object

    CDoublerDLLApp theApp;

    DLLexport int WINAPI MultiplyByTwo(int DoubleMe)
    {
        int DoubledValue = 2 * DoubleMe;                                ⇐
            .
            .
            .

    }
To let the user know we’re doing our job, we can place a system message box on the screen indicating the value passed to us and the value we’re about to return. Notice that we use AfxMessageBox() here, not the MFC MessageBox() function:
    /////////////////////////////////////////////////////////////////////////////
    // 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);                                           ⇐
            .
            .
            .

    }
If a program passes MultiplyByTwo() a value of 2, we pop a message box on the screen saying, “MultiplyByTwo() is about to double 2 and return 4.” Finally, we return the new value, DoubledValue.
    /////////////////////////////////////////////////////////////////////////////
    // 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;                                           ⇐

    }
We have just exported a function from a DLL. Select the Build DoublerDLL.dll item in the Visual C++ Build menu now. This creates DoublerDLL.dll and DoublerDLL.lib. We put these files to work now in a new MDI program, Doubler. #### The Doubler Program The Doubler program sends two successive values—1 and 2—to our MultiplyByTwo() function and displays what that function returns. In this way, we can check whether our DLL is successful. Create a new MDI project now named Doubler and add a new item to the project’s File menu. Double the values and connect a handler function to it.
    void CDoublerView::OnFileDoublethevalues()
     {
        // TODO: Add your command handler code here

    }
First, set up the two values to be doubled: value1 and value2.
    void CDoublerView::OnFileDoublethevalues()
     {
        int value1 = 1;                                                 ⇐
        int value2 = 2;                                                 ⇐
            .
            .

            .
Then, double those values by passing them to our MultiplyByTwo() function, storing the results as result1 and result2.
    void CDoublerView::OnFileDoublethevalues()
     {
        int value1 = 1;
        int value2 = 2;

        int result1 = MultiplyByTwo(value1);                            ⇐
        int result2 = MultiplyByTwo(value2);                            ⇐
            .
            .
            .
As you might expect, we need to let this project know where to find Multiply-ByTwo(), and we do that now.
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.