动态链接库的应用

来源:互联网 发布:redis c语言接口 编辑:程序博客网 时间:2024/06/07 16:40

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">隐式链接</span>

DLL代码:

//dll2.cppint add2(int a, int b){return a+b;}int sub(int a, int b){return a-b;}


//dll1.h#ifdef DLL1_API#else#define  DLL1_API _declspec(dllimport)#endifDLL1_API  int add(int a, int b);DLL1_API int sub(int a, int b);DLL1_API void printInfo();

//dll1(2).h#ifdef DLL1_API     //条件编译#else#define  DLL1_API _declspec(dllimport)#endifDLL1_API  int add(int a, int b);DLL1_API int sub(int a, int b);DLL1_API void printInfo();


加载程序代码:

//dlluse.cpp#include <iostream>#include "dll1.h"using namespace std;#pragma  comment(lib,"dllexport.lib")#pragma  comment(lib,"dll2.lib")//extern int add(int a,int b);//extern int sub(int a,int b);//extern void printInfo();//_declspec(dllimport) int add(int a,int b);//_declspec(dllimport)int sub(int a,int b);//_declspec(dllimport) void printInfo();extern int add2(int a, int b);   //for dll2.libvoid main(){int a = 12;int b = 2;cout<<add(a,b)<<endl;cout<<add2(3,7)<<endl;printInfo();system("pause");}


//dlluse(2).cpp//只需要拷贝.lib文件到代码目录下即可#include <iostream>using namespace std;#pragma  comment(lib,"dllexport.lib")//extern int add(int a,int b);//extern int sub(int a,int b);//extern void printInfo();_declspec(dllimport) int add(int a,int b);_declspec(dllimport)int sub(int a,int b);_declspec(dllimport) void printInfo();void main(){int a = 12;int b = 2;cout<<add(a,b)<<endl;printInfo();system("pause");}

#include <time.h>#include "dll1.h" #include <iostream>using namespace std;#define DLL1_API _declspec(dllexport) int add(int a, int b){return a+b;}//dllexport.cppint sub(int a, int b){return a-b;}void printInfo(){time_t nowtime;time(&nowtime);struct tm *printTime;printTime = localtime(&nowtime);//cout<<printTime<<endl;cout<<ctime(&nowtime)<<endl;}


//在新建的dll项目中编译#include <time.h>#include <iostream>dllexprot(2).cppusing namespace std;_declspec(dllexport)  int add(int a, int b){return a+b;}_declspec(dllexport)int sub(int a, int b){return a-b;}_declspec(dllexport)void printInfo(){time_t nowtime;time(&nowtime);struct tm *printTime;printTime = localtime(&nowtime);//cout<<printTime<<endl;cout<<ctime(&nowtime)<<endl;}

//dll2.defLIBRARY dll2EXPORTSadd2sub



显示链接

DLL代码:

int add2(int a, int b){return a+b;}int sub(int a, int b){return a-b;}


加载程序代码:

#include <iostream>#include <Windows.h>//#include "dll1.h"using namespace std;//#pragma  comment(lib,"dllexport.lib")//#pragma  comment(lib,"dll2.lib")//extern int add(int a,int b);//extern int sub(int a,int b);//extern void printInfo();//_declspec(dllimport) int add(int a,int b);//_declspec(dllimport)int sub(int a,int b);//_declspec(dllimport) void printInfo();//extern int add2(int a, int b);   //for dll2.libvoid main(){typedef  int (*funcdll2)(int a, int b);TCHAR dllname[32] = L"dll2.dll";  //dll2.dll中的函数必须使用.def定义或者extern "c"修饰,使函数名保存不变HMODULE hdll2 = LoadLibrary(dllname);if (hdll2 == NULL){cout<<GetLastError()<<endl;   //返回错误号,查询对应错误}//MAKEINTRESOURCE(1)通过函数在dll中的排序进行加载,对于不是采用.def定义,或者不少extern "c"形式的函数可以采用这种方式调用funcdll2 myadd = (funcdll2)GetProcAddress(hdll2,(LPCSTR)MAKEINTRESOURCE(1)); if (myadd == NULL){cout<<GetLastError()<<endl;}funcdll2 mysub = (funcdll2)GetProcAddress(hdll2,"sub");int a = 12;int b = 2;//cout<<add(a,b)<<endl;//cout<<add2(3,7)<<endl;cout<<myadd(2,3)<<endl;cout<<mysub(4,3)<<endl;//printInfo();system("pause");}


0 0
原创粉丝点击