Dynamic Link Library DLL

The following article helps me a lot to understand DLL.

http://www.mindcracker.com/mindcracker/c_cafe/dll/dll_tut1l.asp

I have distributed this tutorial in three parts. First part will explain basics of MFC Dlls.
Second and third part will guide you towards developing your Regular and Extension DLLs.
What is a DLL?

DLL stands for Dynamic Link Library.
A dll is a binary file which contains some functions. It allows another process to call its functions.
More than one processes can call a dll simultaneously.

MFC support two types of dlls. Extension dlls and regular dlls. Each has its advantages and disadvantages.

Advantages of DLLs:

Dlls provide modularity to your application. You can break your big program into small modules, write some common functions in a separate module and compile it as a dll. Let all other modules call this dll.
For example, A database application has three modules Input, Output, Processing.
All three modules need to access a database. You can make a dll with database functions and let all three module call the dll to access the database.
If you need to add more functions, you can add those functions to the dll.
You don't have to change the entire application.
Since multiple processes can share a dll in memory so dll saves memory, disk space and execution time because of swapping. 

Extension DLLs

Extension DLLs implements reusable classes from MFC classes. They can export entire class. Client applications can create objects of that class and call its functions.

An MFC extension DLL has the following features and requirements:

The must implement DllMain and initialization can be done here.
Extension DLLs should be compiled with _AFXEXT defined. You need to add AFX_EXT_CLASS in dll's class.
MFC applications with _AFXDLL defined can only use Extension dlls. 
Extension DLLs should not instantiate a class derived from CWinApp, but should rely on the client application (or DLL) to provide this object.
See part III of this tutorial for how to develop and use extension dlls and more details.

Regular DLLs

If you need a DLL that can be used by Win32 or MFC applications then you need to create Regular dlls. Regular dlls can only export a C function. They can't export a class or its members as Extension dlls do.

You can link MFC library in two ways to the regular dlls. Either dynamically linked or statically liked. In static linking, your dll will include MFC library copy inside your code. Static linking make your dll size bigger. In dynamic linking, you dll doesn't copy MFC code but then you have to ship MFC with your dll.

See part II of this tutorial for how to develop and use regular dlls.

Dynamic Linking Vs. Static Linking

Dynamic linking allows an exe or dll to use required information at run time to call a DLL function. In static linking, the linker gets all the referenced functions from the static link library and places it with your code into your executable. Using DLLs instead of static link libraries makes the size of the executable file smaller. Dynamic linking is faster than static linking.

What kind of DLL you want?

This table show what kind of dll is your requirement.

DLL Requirements  Clients  DLL Selection 
DLL does not use MFC  --  non-MFC Win32 DLL 
DLL will use MFC Clients may or may not be MFC applications   MFC Regular DLL with dynamically link to MFC.
DLL will use MFC  All clients are MFC ( dynamically linked) and you want to export MFC derived classes. Extension DLL

Building a DLL that dynamically links to MFC is faster than building a DLL that statically links to MFC because it is not necessary to link MFC itself. But then you must distribute the shared DLLs MFCx0.DLL and MSVCRT.DLL with your dll and that's a big pain.

How Clients find a DLL?

You can use LoadLibrary to load a dll from a specific path. If you link implicitly, Windows searches in these paths:

Current directory of exe.
Processes current directory.
Windows System Dir
Windows Dir
Directories listed in the Path 
The best way is to copy your dll into systems directory but I don't like this approach because then your systems dir is a mess. Copying in exe 's dir is not a bad idea.

Steps to Use an Extension DLL

There are following steps required to perform by a client to use an extension dll.

1. Copy your extension class's header file to your project directory.

2. Link your project to the lib file of your dll.

3. Include header file of your class in your project's stdafx.h or cpp file.

4. Create Object of the class and call its member functions.

See how to create an extension dll in third part of this tutorial for more details.

Steps to Use a Regular DLL

1. Link to the Library. Copy your lib file to your test project directory. Or if you don't want to copy the lib file then enter your lib file with the path in Object/library modules text box.

2. Import functions. Import functions using __declspec. Write this code in the beginning of your cpp file.

extern "C" __declspec(dllimport) long AddTwoNumbers( long val1, long val2);
extern "C" __declspec(dllimport) long MultiplyTwoNumbers( long val1, long val2); 

3. Call Functions. Add these public members to your dialog's header class.

int lSum = AddTwoNumbers( 12, 65 ); 
int lMultiplyRes = MultiplyTwoNumbers(12, 65) ;  

See how to create a regular dll in second part of this tutorial for more details.

posted on 2005-03-31 17:54  番茄鸡蛋面  阅读(1337)  评论(0编辑  收藏  举报

导航