您的位置:首页 > 其它

DLL&LIB 【留给自己用】

2013-11-29 13:59 183 查看
<1>_declspec(dllexport) 在要export的函数、类、数据前加上,在C调用约定、C编译情况下可以去掉函数名的下划线前缀,这样一C编译的方式编译函数;
<2>当需要使用C++编译器编译成为C形式的函数时,要在前面加上extern"C"
<3>.def导出的dll把函数地址保留了

1、win32形式的dll,下面这种的使用比较其他的方便一些,像使用函数一样
<myDll.h>

#ifdef __cplusplus
#define EXPORT extern"C" __declspec(dllexport)
#else
#define EXOPRT extern __declspec(dllexport)
#endif

EXPORT int Add(int add1, int add2);

<myDll.cpp>
#include<windows.h>
#include"myDll.h"

int WINAPI DllMain(HINSTANCE hInstance, DWORD fdsReason, PVOID pvReserved){
return TRUE;
}

EXPORT int Add(int add1, int add2){
return add1 + add2;
}

使用dll的项目在编译连接的时候注意将.h和.dll以及.lib文件拷贝到相关的目录下面,并且需要设置依赖dll属性

使用dll也很方便,直接包含dll的头文件,就可以像使用函数那样直接使用
#include "myDll.h"
#include "stdafx.h"
int APIENTRY WinMain()(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow){
int sum = Add(1, 2);
return 0;
}

2、动态C函数库lib和dll,即函数lib和dll的使用,不需要没有.h头文件。但是需要知道函数的定义形式
<fundll.c>
_declspec(dllexport) int Add(int add1, int add2){
return add1 + add2;
}

//使用.lib的方式:
编译后将生成的lib拷贝到需要使用的地方
<fundlllibTest.c>
#pragma comment(lib, "../fundll.lib")
int main(){
int sum = Add(1, 2);
return 0;
}
//dll的使用方式
HMODULE hModule = LoadLibrary("");

3、静态C函数库(.lib)的使用,区别和C动态库的生成方式
<staticlib.c>
int Add(int add1, int add2){
return add1 + add2;
}
//编译即可,将生成的.lib文件放在需要的目录下

<staticlibTest.c>
#pragma comment(lib, "../staticlib.lib")//如果lib放在系统文件夹下,可以不需要这句

int main(){
int sum = Add(1, 2);
return 0;
}

4、C++中Win32的函数动态库dll的使用:
<fundll.cpp>
_declspec(dllexport) int Add(int add1, int add2){
return add1 + add2;
}
Win32工程使用.dll方式:
编译后即可,将生成的dll拷贝到需要调用的地方
<fundllTest.cpp>
HINSTANCE hInstance = LoadLibrary("fundll.dll");
typedef int (*pAdd)(int add1, int add2);
pAdd Add = (pAdd)GetProcAddress(hInstance, "Add");//另外还有其他方法
int sum = Add(1,2);
FreeLibrary(nDll);//卸载动态库

5、静态C++库(.lib)的使用:
<wincpplib.cpp>
int CPP_Add(int add1, int add2){
return add1 + add2;
}

//usewincpplib使用静态C++库
#pragma comment(lib, "../cppstatic.lib")
_declspec(dllimport)int Add(int add1, int add2);//_declspec(dllimport)可以不加

//在c++中使用c库文件
#pragma comment(lib, "../cstatic.lib")
extern"C" _declspec(dllimport)int C_Add(int add1, int add2);//_declspec(dllimport)可以不加

int main(){
int sumcpp = CPP_Add(1, 2);
int sumc = C_Add(1, 2);
return 0;
}
6、调用C++动态库dll:
<wincppdll.cpp>
//显示连接动态库,不能加_declspec(dllexport),由.def文件说明导出
int cppAdd(int add1, int add2){
return add1 + add2;
}

int cppSub(int sub1, int sub2){
return sub1 - sub2;
}

//这个保留为隐式连接动态库,以作对比
_declspec(dllexport)int cppMul(int mul1, int mul2){
return mul1 * mul2;
}

<wincppdll.def>
LIBRARY WinCPPDLL
EXPORTS
cppAdd @1
cppSub @2

//使用方式
<wincppdllTest.cpp>
#include "windows.h"
typedef int (*pcppAdd)(int add1, int add2);
typedef int (*pcppSub)(int sub1, int sub2);
typedef int (*pcppMul)(int mul1, int mul2);

int main(){
//.def导出的dll把函数地址保留了
HMODULE hModule = LoadLibrary("wincppdll.dll");
pcppAdd cppAdd = (pcppAdd)GetProcAddress(hModule, "cppAdd");
int sum = cppAdd(1, 2);
...//其他相同操作
FreeLibrary(hModule);
}

7、类的动态链接库产生(lib/dll)和使用:
<dllclass.h>
#ifndef __DLLCLASS__
#define __DLLCLASS__

#ifdef MY_DLLCLASS_EXPORTS
#define EXT_CLASS _declspec(dllexport)
#else
#define EXT_CLASS _declspec(dllimport)
#endif

class EXT_CLASS CMath{
public:
int Add(int add1, int add2);
};

#endif

<dllclass.cpp>
#define MY_DLLCLASS_EXPORTS
#include "dllclass.h"
#include "windows.h"
#include "stdio.h"

BOOL WINAPI DLLMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved){
printf("Load...\n");
switch(fdwReason){
case DLL_PROCESS_ATTACH:
printf("Load...\n");
break;
case DLL_PROCESS_DETACH:
printf("UNLoad...\n");
break;
}
return TRUE;
}

int CMath::Add(int add1, int add2){
return add1 + add2;
}

//lib使用
#include "../dllclass.h"
#pragma comment(lib, "../dllclass.lib")
int main(){
CMath math;
int sum = math.Add(1, 2);
return 0;
}
//dll的使用
LoadLibrary();

本文出自 “BaggerSky” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: