一 静态库的使用 在静态库文件中 、、、、、、、、、、、、在头文件中 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); 、、、、、、、、、、、、、 ///////////////////////////// 动态链接库 空工程 一 头文件中 add.h #ifdefine DLL_API #else #define DLL_API _declspec(dllimport) #endif DLL_API int add(int a,int b); 动态库工程的cpp文件 。。。。。。。。。。。。。。。。。。。 #define DLL_API _declspec(dllexport) #include "add.h" DLL_API int add(int a,int b) { return a+b; } 动态链接库的测试工程 #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("函数没有找到"); } } ------------------------------------------- 建立一个典型的动态库工程 新添加一个模块定义文件 def 格式的 LIBRARY "dll" EXPORTS add |
|