会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
sqlite例子
博客园
首页
新随笔
联系
订阅
管理
动态链接库dll创建静动态调用方法一例
动态链接库dll创建静动态调用方法一例 动态链接库dll创建 MFC_DLL.dll stdafx.h 中添加函数声明 //定义求和函数-------------------------- extern "C" __declspec(dllexport) int __stdcall Add_new(int a,int b); extern "C" _declspec(dllexport) int WINAPI TestC(int i); extern "C" _declspec(dllexport) long __stdcall Loop(int n); extern "C" _declspec(dllexport) long __stdcall GetNum(int n); stdafx.cpp 中添加函数实现 //实现函数---------------------------- extern "C" __declspec(dllexport) int __stdcall Add_new(int a,int b) { return a+b; } extern "C" __declspec(dllexport) int WINAPI TestC(int i) { return i; } extern "C" __declspec(dllexport) long __stdcall Loop(int n) { long sum=0; if(n==1) sum=1; else { sum=Loop(n-1); sum*=2; } return sum; } //1,1,2,3,5,8,13,21,....X(n) extern "C" __declspec(dllexport) long __stdcall GetNum(int n) { int r = 0; if (n == 1 || n==2) { r = 1; } if (n > 2) { r = GetNum(n - 1) + GetNum(n - 2); } return r; } MFC_DLLTestConsole.exe 调用测试控制平台 静态调用 --------------------------------- //在头文件目录中添加已有项MFC_DLL.lib文件 //添加test.h头文件 并添加如下函数声明 #pragma comment(lib,"MFC_DLL.lib") //告诉编译器DLL相对应的lib文件所在路径和文件名 extern "C" _declspec(dllimport) int _stdcall Add_new(int a,int b);//声明导入函数 extern "C" _declspec(dllexport) int _stdcall TestC(int i); extern "C" _declspec(dllexport) long _stdcall Loop(int n); extern "C" _declspec(dllexport) long _stdcall GetNum(int n); // MFC_DLLTestConsole.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> //int _tmain(int argc, _TCHAR* argv[]) //{ // return 0; //} //静态调用 //#include "test.h" //int main() //{ // //静态引用 动态链接库dll的方法 // std::cout<<"Add_new(10,3)=10+3="<<Add_new(10,3)<<"\n"; // std::cout<<"TestC(i)=i="<<TestC(100)<<"\n"; // // //暂停操作 // char str; // std::cin>>str; // return 0; //} //动态调用 #include <stdio.h> #include <windows.h> typedef int (* lpAddFun)(int ,int);//定义一个与Add_new函数接受参数类型和返回值均相同的函数指针类型 typedef int (* lpReturn)(int); typedef long(* lpLoop)(int); typedef long(* lpGetNum)(int); int main() { HINSTANCE hDll;//句柄 lpAddFun addFun;//函数指针 lpReturn ReturnAddr; lpLoop loopFun; lpGetNum getNumFun; hDll=LoadLibraryA((LPCSTR)"MFC_DLL.dll");//动态加载DLL模块句柄 if(hDll) { addFun=(lpAddFun) GetProcAddress(hDll,"Add_new");//得到所加载DLL模块中函数的地址 if(addFun) { int result=addFun(2,3); //printf("%d",result); std::cout<<"addFun(2,3)=2+3="<<result<<"\n"; } ReturnAddr=(lpReturn) GetProcAddress(hDll,"TestC"); //得到所加载DLL模块中函数的地址 if(ReturnAddr) { int result=ReturnAddr(200); std::cout<<"TestC(200)=200="<<result<<"\n"; } loopFun=(lpLoop) GetProcAddress(hDll,"Loop"); if(loopFun) { long result=loopFun(1); std::cout<<"Loop(1)=1="<<result<<"\n"; result=loopFun(2); std::cout<<"Loop(2)=1="<<result<<"\n"; result=loopFun(3); std::cout<<"Loop(3)=2="<<result<<"\n"; } getNumFun=(lpGetNum)GetProcAddress(hDll,"GetNum"); if(getNumFun) { long result=getNumFun(1); std::cout<<"GetNum(1)=1="<<result<<"\n"; result=getNumFun(2); std::cout<<"GetNum(2)=1="<<result<<"\n"; result=getNumFun(3); std::cout<<"GetNum(3)=2="<<result<<"\n"; result=getNumFun(4); std::cout<<"GetNum(4)=3="<<result<<"\n"; result=getNumFun(5); std::cout<<"GetNum(5)=5="<<result<<"\n"; } FreeLibrary(hDll);//释放已经加载的DLL模块 } else { std::cout<<"加载dll失败!"; } //暂停操作 char str; std::cin>>str; return 0; } //--the end
posted @
2011-07-21 16:13
sqlite例子
阅读(
191
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
公告