動態鏈接庫的應用

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

DLL代碼:

//dll2.cpp
int 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)
#endif

DLL1_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)
#endif

DLL1_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.lib
void 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.cpp
int 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).cpp
using 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.def
LIBRARY dll2

EXPORTS
add2
sub



顯示鏈接

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.lib


void 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");
}


發佈了61 篇原創文章 · 獲贊 20 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章