vc++ 动态链接库 创建与基本使用

vc++ 动态链接库
静态库:
一个二进制文件,通常扩展名为LIB,连接器从库中复制这些函数和数据应用到可执行文件中,发布时不需要发布使用的静态库。
动态库:
一个引入库.lib和一个.dll。此LIB中包含DLL文件导出的函数和变量符号,而.dll文件包含该DLL实际的函数和数据。


lib和dll的区别
lib是编译时用到的,dll是运行时用到的。
如果要完成源代码的编译,只需要lib。
如果要使动态链接的程序运行起来,只需要dll。


构建动态库:
1:

.h
#ifdef dllapi
#else
#define dllapi _declspec(dllimport)
#endif

dllapi int add(int a,int b);

class  dllapi Point
{
public:
 void output(int x,int y);
};
.cpp
#define dllapi /*extern "C"*/ _declspec(dllexport)

#include "vs2005dll.h"
#include "windows.h"
#include "stdio.h"
int add(int a,int b)
{
 return a+b;
}

void Point::output(int x, int y)
{
 HWND hwnd=GetForegroundWindow();
 HDC hdc=GetDC(hwnd);
 char buf[20];
 memset(buf,0,20);
 sprintf_s(buf,"x=%d,y=%d",x,y);
 ReleaseDC(hwnd,hdc);
}

2:如何调用
隐式调用:
把DLL DEBUG中的DLL,.h和LIB文件复制到所需调用文件中,在所需调用的类中声明之后。
int i=add(5,7);

Point pt;
pt.output(3,4);

显示调用
dumpbin
cmd
dumpbin -exports xxx.dll

会显示出DLL中多包涵的函数。
函数名字会被重新定义。

HINSTANCE hinst; //定义一个应用程序实例的句柄
hinst=LoadLibrary("vs2005dll.dll");
typedef int (*addproc)(int a,int b); //
addproc add=(addproc)GetProcAddress(hinst,"add");
//addproc add=(addproc)GetProcAddress(hinst,MAKEINTRESOURCE(2));
//addproc ADD=(addproc)GetProcAddress(hinst,"?add@@YAHHH@Z");
int i=add(3,6);

dumpbin -exports xxx.dll 查看DLL中被导出的函数

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值