惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - 番茄鸡蛋面

VS .NET add web reference COM Vs .NET (Qt ActiveQt) 复习进程和线程 process vs thread Window Firewall cause "MS SQL Server New SQL Server Registeration" and "New ODBC DSN " failure 牛人与非牛人的对话 - 番茄鸡蛋面 - 博客园 Web Accessibility toolbar -- a must-have toolbar for web developer 好好学习天天向上 Oracle table problem Oracle - MSSQL Convert Tips (2) .NET 数据访问体系结构指南 NET Framework Data Providers Links or Postbacks ASP.NET features we use, ASP.NET experience I have Word Note 001 Skype 0.98.0.28 release! I can't agree more Skype Skype Skpe Getting Your Résumé Read 在行进中开火 Fire and Motion By Joel Spolsky
Dynamic Link Library DLL
番茄鸡蛋面 · 2005-03-31 · via 博客园 - 番茄鸡蛋面

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.