第九章 windows下的动态链接
1. 创建和使用dll
/* math.c */
#ifdef __cplusplus
extern "C"{
#endif
__declspec(dllexport) double Add(double a, double b)
{
return a+b;
}
__declspec(dllexport) double Sub(double a, double b)
{
return a-b;
}
__declspec(dllexport) double Mul(double a, double b)
{
return a*b;
}
#ifdef __cplusplus
};
#endif
cl /LDd math.c 生成debug版的dll,如果是生成release的是/LD,将生成四个文件,自己尝试就知道了
/* test.c */
#ifdef __cplusplus
extern "C"{
#endif
#include <stdio.h>
__declspec(dllexport) double Sub(double a, double b);
int main(int argc, char *argv[])
{
double result=Sub(3.0, 2.0);
printf("result=%f\n", result);
return 0;
}
#ifdef __cplusplus
};
#endif
cl /c test.c
link test.obj math.lib
".lib"(在此称导入库)文件是一组目标文件的集合,但并不包含math.c的代码和数据,用来描述math.dll的导出符号
DLL显示运行时链接
#include <windows.h>
#include <stdio.h>
typedef double (*Func)(double, double);
int main(int argc, char *argv[])
{
Func function;
double result;
HINSTANCE hinstLib=LoadLibrary("math.dll");
if (hinstLib==NULL)
{
printf("ERROR:unable to load DLL.\n");
return 1;
}
function=(Func)GetProcAddress(hinstLib, "Add");
if (function==NULL)
{
printf("ERROR:unable to find DLL function.\n");
FreeLibrary(hinstLib);
return 1;
}
result=function(1.0, 2.0);
FreeLibrary(hinstLib);
printf("result=%f\n", result);
return 0;
}
exp文件是创建dll的临时文件,实际上是一个标准的PE/COFF文件,导出表放入.edata
2. c++与动态链接
参考《COM本质论》
指导意见:
(1)所有接口函数都应该是抽象的。所有的方法都应该是纯虚的。(或者inline的方法也可以)
(2)所有的全局函数都应该使用extern “C”来防止名字修饰的不兼容。并且导出函数的都应该是__stdcall调用规范(COM的DLL都使用这样的规范)。这样即使用户本身的程序默认以__cdecl方式编译的,对于DLL的调用也能正确。
(3)不要使用C++标准库STL
(4)不要使用异常
(5)不要使用虚析构函数。可以创建一个destroy()方法并且重载delete操作符并且调用destroy()
(6)不要在DLL里面申请内存。而且在DLL外释放(或者相反)。不同的DLL和可执行文件可能使用不同的堆,在一个堆里面申请内存而在另外一个堆里面释放会导致错误。比如,对于内存分配相关的函数不应该是inline的,以防止它在编译时被展开到不同的DLL和可执行文件
(7)不要在接口中使用重载方法。因为不同的编译器对于vtable的安排可能不同。
3. DLL HELL(dll噩梦)
预防方法:
静态链接
防止DLL覆盖(windows文件保护实现)
避免DLL冲突(让每个应用程序拥有自己依赖的DLL)
.Net下DLL Hell的解决方案(Manifest,XML描述文件,(side-by-side manager)SxS Manager实现对相应版本的DLL的加载)