《Windows核心编程5》第19章-DLL基础

1、windows应用程序编程接口(applicationprogramming interface)提供的所有函数包含在dll中,其中三个最重要的dll分别是:

(1)Kernel32.dll。包含的函数用于管理内存、进程和线程。

(2)User32.dll。包含的函数用于执行与用户界面相关的任务,包括创建窗口和发送消息。

(3)GDI32.dll。包含的函数用来绘制图像和显示文字。

2、如果运行同一个可执行文件的多个实例,这些实例将不会共享可执行文件中的全局变量和静态变量,这是通过windows的写时复制机制保证的,同样的方法也适用于dll,当一个进程将一个dll映像文件映射到自己的地址空间中时,系统也会为全局变量和静态变量创建信的实例。

3、实际开发过程中,尽量避免从dll中导出变量,另外,除非可执行程序和dll采用的是同一个厂商提供的工具包,否则不要导出C++类

 

4、给出一个简单dll的生成步骤:

(1)头文件MyLib.h

//
// module: MyLib.h
//

#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif

MYLIBAPI  int g_result;
MYLIBAPI  int add(int a, int b);

(2)实现文件MyLib.cpp

#include <Windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)
#include "mylib.h"

int g_result;
int add(int a, int b){
	return a+b;
}

(3)def文件,mylib.def

LIBRARY  "MyLib"

EXPORTS

  add

5、使用dumpbin.exe查看dll的导出信息,如下:

Dump of fileG:\BookDemo\WindowsViaC++\debug\mylib.dll

File Type: DLL

 Section contains the following exports for MyLib.dll

          0 characteristics

   5403E7AE time date stamp Mon Sep 01 11:27:42 2014

       0.00 version

          1 ordinal base

          2 number of functions

          2 number of names

    ordinal hint RVA      name

          1   0 000110C8 add

          2   1 00017150 g_result

 Summary

       1000 .data

       1000 .idata

       2000 .rdata

       1000 .reloc

       1000 .rsrc

       4000 .text

      10000 .textbss

其中的RVA是导出变量或函数在dll内部的偏移量。

6、查看调用mylib.dll的可执行文件test.exe中的模块的导入段

Dump of fileG:\BookDemo\WindowsViaC++\debug\test.exe

File Type: EXECUTABLE IMAGE

 Section contains the following imports:

   MyLib.dll

                418354 Import Address Table

                4181B8 Import Name Table

                     0 time date stamp

                     0 Index of first forwarderreference

                   0  add

                   1  g_result


   MSVCR80D.dll

                418294 Import Address Table

                4180F8 Import Name Table

                     0 time date stamp

                     0 Index of first forwarderreference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值