vs2008动态链接库编程

一,生成动态链接库
//DynamicLib.h
#ifndef_DYNAMIC_LIB_INCLUDE_H_
#define_DYNAMIC_LIB_INCLUDE_H_
#ifdef__cplusplus
extern"C"{
#endif
_declspec(dllexport)intadd(inta,intb);
_declspec(dllexport)intsub(inta,intb);
#ifdef__cplusplus
};
#endif
#endif
------------------------------------------------------

//DynamicLib.cpp
#include"DynamicLib.h"
intadd(inta,intb){
returna+b;
}
intsub(inta,intb){
returna-b;
}
---------------------------------------------------
生存动态链接库,得到两个文件如下:
DynamicLib.dll
DynamicLib.lib
二.使用动态链接库
2.1隐式使用
#include"DynamicLib.h"
#pragmacomment(lib,"DynamicLib.lib")
intmain(){
inta=10; intb=20; intc=0;
c=add(a,b);
printf("%d+%d=%d\n",a,b,c);

c=sub(a,b);
printf("%d-%d=%d\n",a,b,c);
return0;
}

2.2显示使用

#include
//#include"DynamicLib.h"
#include
//#pragmacomment(lib,"DynamicLib.lib")
typedefint(*pADD)(inta,intb);
typedefint(*pSUB)(inta,intb);
intmain(){//显示使用动态链接库
HINSTANCEhModule=::LoadLibrary("DynamicLib.dll");//句柄
pADDadd=(pADD)::GetProcAddress(hModule,"add");
pSUBsub=(pSUB)::GetProcAddress(hModule,"sub");
inta=10,b=20,c=0;
c=add(a,b);
printf("%d+%d=%d\n",a,b,c);
c=sub(a,b);
printf("%d-%d=%d\n",a,b,c);
::FreeLibrary(hModule);
return0;
}

你可能感兴趣的:(vs2008)