一 靜態(tài)庫的使用
在靜態(tài)庫文件中
、、、、、、、、、、、、在頭文件中
int add(int a,int b);
、、、、、、、、、、、 cpp文件中
int add(int a,int b)
{
return a+b ;
}
/////////////////////////////////////
在測試 工程中
包括頭文件
#include "add.h"
、、、、、、、、、、、、、、、、、、
CString str;
str.Format("%s",add(1,2));
MessageBox(str);
、、、、、、、、、、、、、
/////////////////////////////
動(dòng)態(tài)鏈接庫 空工程
一 頭文件中
add.h
#ifdefine DLL_API
#else
#define DLL_API _declspec(dllimport)
#endif
DLL_API int add(int a,int b);
動(dòng)態(tài)庫工程的cpp文件
。。。。。。。。。。。。。。。。。。。
#define DLL_API _declspec(dllexport)
#include "add.h"
DLL_API int add(int a,int b)
{
return a+b;
}
動(dòng)態(tài)鏈接庫的測試工程
#include "add.h" // 如果不包括頭文件 ,就用 extern int add(int a,int b );
#pragma comment(lib,"add.lib")// 將lib 和 Dll 文件拷貝到相應的目錄下
函數中:
{
CString str;
str.Format("%d",add(1,3));
MessageBox(str);
}
二
dll.h
---------------------------------------------------------
#ifdef _DLL_API
#else
#define _DLL_API extern "C" _declspec(dllimport))
#endif
_DLL_API int add(int a,int b);
--------------------------------------------------
dll.cpp
---------------------------------------------------
#define _DLL_API _declspec(dllexport)
#include "dll.h"
//#include "stdio.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
int add(int a,int b)
{
return a+b;
}
---------------------------------------
測試工程中
---------------------------------------
#include "dll.h"
void CtextDllDlg::OnBnClickedOk()
{
HMODULE hmoudle=::LoadLibrary("dll.dll");
if(!hmoudle)
{
MessageBox("加載不成功");
}
typedef int (* add)(int a,int b);
add fun=NULL;
fun=(add)::GetProcAddress(hmoudle,"add");
if(fun)
{
CString str;
str.Format("%d",fun(1,3));
MessageBox(str);
}
else
{
MessageBox("函數沒(méi)有找到");
}
}
-------------------------------------------
建立一個(gè)典型的動(dòng)態(tài)庫工程
新添加一個(gè)模塊定義文件 def 格式的