现在的位置: 首页 > 综合 > 正文

Regular DLL Tutor For Beginners

2013年09月09日 ⁄ 综合 ⁄ 共 5294字 ⁄ 字号 评论关闭

Regular DLL Tutor For Beginners

By King Coffee

http://www.codeproject.com/dll/RegDLL.asp

Download source - 248 Kb

Introduction

This article shows you how to create a Win32 and MFC DLL to dynamically link a Library to your application. Microsoft Foundation Class (MFC) library can be used to create simplified DLLs. The MFC supports two types of DLLs, regular and extension:

  • Regular DLL using shared MFC DLL

  • Regular DLL with MFC statically linked (Client doesn't have to be MFC based)

  • MFC Extension DLL (Using shared MFC DLL)

Win32 DLL (non-MFC Library based) in general, only supports regular DLLs. You should use Win32 DLLs when your DLL is not using the MFC Library, Win32 is substantially more efficient.

Extension DLLs are for developing re-useable binary code and it is for advance usage (such as ATL and COM). DLLs are very useful, especially if you want to create programs that are modular. You can go to the Microsoft MSDN web page if you want to know more on DLLs.

This tutorial is in five parts. It gives a step by step procedure for developing a Win32 DLL and a regular MFC DLL object. There are three client applications, two Win32 console applications (one for Load Time linkage and the other illustrates Run Time linkage), the third DLL client application uses the shared MFC Library.

Regular DLLs execute in the same memory space as the DLL client application, you don't have to worry about marshaling data and pointers across process boundaries.

Part One, The Win32 DLL Object

First, we are going to make the Win32 DLL core files for the project, W32DLL.xxx.

  1. Start Visual C++ Studio.

  2. Close any open workspace and all files.

  3. Select New from the file menu.

  4. Select Project: Win32 Dynamic-Link Library.

  5. Give the the project a fitting name, say, W32DLL, make sure the location is acceptable and click OK.

  6. Select the "A simple DLL project" radio button.

  7. Click Finish and OK.

Next, we are going to make the DLL declaration/header file: DLLCode.h. Select New from the file menu, then select "C/C++ Header File" and name the file DLLCode. Click OK.

Copy and paste the following code excerpt:


Collapse

/*******************************************************

   File name: DLLCode.h

 

   This file contains all the DLL interfacing object

   declarations, in this example:

   a class object, two global function object, and

   a global integer variable.

   Notice: we use the same header file for compiling the

   .DLL and the .exe (application).

   This header file defines a macro which export the target

   DLL objects if we are building

   a DLL, otherwise it import the DLL objects into an

   application which uses the DLL. If

   we define DLLDIR_EX (a preprocessor identifier),

   then the preprocessor define macro

   DLLDIR (a mnemonic for DLL import/export Direction)

   becomes an export instruction,

   otherwise its an import instruction by default.

************************************************************/

#ifdef DLLDIR_EX

   #define DLLDIR  __declspec(dllexport)   // export DLL information

#else

   #define DLLDIR  __declspec(dllimport)   // import DLL information

#endif

 

// The extern "C" declaration allows mixed languages compactability,

// it prevents the C++ compiler from using decorated (modified)

// names for the functions

extern "C" {

       void DLLDIR DLLfun1(char*);

       int  DLLDIR DLLfun2(int);

};

extern int  DLLDIR DLLArg;

 

class DLLDIR DLLclass

{

   public:

      DLLclass();          // Class Constructor

      ~DLLclass();         // Class destructor

      int Add(int, int);   // Class function Add

      int Sub(int, int);   // Class function Subtract

      int Arg;             // Warning: you should not

               // import class variables

               // since the DLL object can be dynamically unloaded.

};

Save and close this header file. Now we are going to create the DLL implementation file, DLLCode.cpp.

Select New from the file menu, then select "C++ Source File" and name the file DLLCode. Then click OK.

Copy and paste the following code excerpt:

/*********************************************************

   File name: DLLCode.cpp

 

   The header file, DLLCode.h, prototypes

   all of the DLL interface objects

**********************************************************/

#include "Stdafx.h"

#include "DLLCode.h"

#include <iostream>

 

using namespace std;

 

void DLLfun1(char* a)

{

   cout << a << endl;

};

 

int DLLfun2(int a) { return a<<1; };

 

int DLLArg = 100;

 

DLLclass::DLLclass() {};

DLLclass::~DLLclass() {};

 

int DLLclass::Add(int a, int b)

{

   return a + b;

};

 

int DLLclass::Sub(int a, int b)

{

   return a - b;

};

We want to access the DLL, so we need to export it. When the DLL is built, it also builds something called an export library. The export library is similar to a regular library. It contains all of the information necessary to dynamically link to the DLL at runtime. When we export a function or a class, the function name and location is stored in the export library. The application uses the DLL links in the export library to access the DLL.

To create the DLL export library, select "setting..." from the Project menu. Select the C/C++ tab. Append, or insert, ",DLLDIR_EX" (without the quotation marks) to the Preprocessor Definition text box. Then click OK. This will prevent compiler assumptions and warnings.

Note, Visual C++ defines an export macro <projectname>_EXPORTS, in our case, W32DLL_EXPORTS. But we used DLLDIR_EX, a generic macro name.

Click the "!" button to compile, build, and run the W32DLL project.

Close the "Executable For Debug Session" dialog box, we ran the DLL prematurely.

Congratulation, you finished building the Win32 DLL and its export Library.

Part Two, DLL Client Application One

This is the simplest and most versatile DLL Client, without MFC. This flexibility is due to the DLL export library information.

Now, we are going to make a Win32 console application, project DLLClient1. In this application, the DLL is loaded at application startup, "Load Time" linkage.

  1. Close any open workspace and files, then select New from the File menu.

  2. Select Win32 Console Application.

  3. In the Project name box, give the project a fitting name, say, DLLClient1, then click Next.

  4. Select "An empty project", then click Finish and OK.

Next, we are going to make the C/C++ sour

抱歉!评论已关闭.