打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【动态链接库】VC++2010中创建和使用动态链接库dll

一、动态链接库简介



动态链接库英文为DLL,是Dynamic Link Library 的缩写形式,DLL是一个包含可由多个程序同时使用的代码和数据的库
,DLL不是可执行文件。动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数。函数的可执行代码位于
一个 DLL 中,该 DLL 包含一个或多个已被编译、链接并与使用它们的进程分开存储的函数。DLL 还有助于共享数据和
资源。多个应用程序可同时访问内存中单个DLL 副本的内容。DLL 是一个包含可由多个程序同时使用的代码和数据的库。

dll优点和作用:


    1、扩展了应用程序的特性;
  2、可以用许多种编程语言来编写;
  3、简化了软件项目的管理;
  4、有助于节省内存;
  5、有助于资源共享;
  6、有助于应用程序的本地化; 


二、创建并使用动态链接库




1.使用_declspec(dllexport)关键字导出函数,使用静态调用dll

    在VisualStudio工程向导中建立一个Windows Console Application,在“应用程序类型”选项中选择DLL,在“附加选项”中选择空项目。如下:


    在项目中添加cpp文件,加入代码如下:

  1. /**********************************************/  
  2. /*FileName:DllDemo.cpp                        */  
  3. /**********************************************/  
  4.   
  5. #define DllDemoAPI _declspec(dllexport)  
  6. #include "DllDemo.h"  
  7. #include <stdio.h>  
  8.   
  9. DllDemoAPI int add(int a, int b)  
  10. {  
  11.     return a+b;  
  12. }  
  13.   
  14. DllDemoAPI int subtract(int a, int b)  
  15. {  
  16.     return a-b;  
  17. }  
  18.   
  19. DllDemoAPI int multiple(int a, int b)  
  20. {  
  21.     return a*b;  
  22. }  
  23.   
  24. DllDemoAPI void Point::Print(int x, int y)  
  25. {  
  26.     printf("x=%d,y=%d",x,y);  
  27. }  


    在项目中添加.h头文件,加入代码如下:

    

  1. /**********************************************/  
  2. /*FileName:DllDemo.h                          */  
  3. /**********************************************/  
  4.   
  5. #ifdef DllDemoAPI  
  6. #else  
  7. #define DllDemoAPI _declspec(dllimport)  
  8. #endif  
  9.   
  10. DllDemoAPI int add(int a, int b);  
  11. DllDemoAPI int subtract(int a, int b);  
  12. DllDemoAPI int multiple(int a, int b);  
  13.   
  14. class DllDemoAPI Point  
  15. {  
  16. public:  
  17.     void Print(int x, int y);  
  18. };  


    编辑,生成,可以看到Debug目录里生成了以下这些文件:
    


    
    调用dll:
    新建一个控制台应用程序,取名InvokeDll,如下图:
    在InvokeDll.cpp中添加以下代码:
    
  1. // InvokeDll.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <Windows.h>  
  6. #include "..\DllDemo\DllDemo.h"  
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     /*加载dll函数调用方式为默认调用方式*/  
  11.        
  12.     printf("5+3=%d\n",add(5,3));   
  13.   
  14.     Point p;  
  15.     p.Print(5,3);  
  16.       
  17.       
  18.     return 0;  
  19. }  


    
    选择InvokeDll项目,点击右键,选择“属性”选项,在“链接”选择的字选项“输入”|“外部依赖项”中添加DemoDll.lib
    
    编译运行得结果:
    

2.通过应用程序定义文件,并动态调用


    在VisualStudio工程向导中建立一个Windows Console Application,在“应用程序类型”选项中选择DLL,在“附加选项”中选择空项目。
    在项目中添加cpp文件,加入代码如下:

  1. /**********************************************/  
  2. /*FileName:DllDemo.cpp                        */  
  3. /**********************************************/  
  4.   
  5. #define DllDemoAPI _declspec(dllexport)  
  6. #include "DllDemo.h"  
  7. #include <stdio.h>  
  8.   
  9. DllDemoAPI int add(int a, int b)  
  10. {  
  11.     return a+b;  
  12. }  
  13.   
  14. DllDemoAPI int subtract(int a, int b)  
  15. {  
  16.     return a-b;  
  17. }  
  18.   
  19. DllDemoAPI int multiple(int a, int b)  
  20. {  
  21.     return a*b;  
  22. }  


    在项目中添加.def头文件,加入代码如下:

  1. LIBRARY DllDemo  
  2.   
  3. EXPORTS  
  4. add  
  5. subtract  
  6. multiple  

    

    编辑,生成,可以看到Debug目录里生成了以下这些文件:
    


    
    调用dll:
    同上,新建一个控制台应用程序,取名InvokeDll
    在InvokeDll.cpp中添加以下代码:
    
    

  1. #include "stdafx.h"  
  2. #include <Windows.h>   
  3.   
  4. int _tmain(int argc, _TCHAR* argv[])  
  5. {  
  6.     /*加载dll函数调用方式为默认调用方式*/  
  7.     HINSTANCE hInst = LoadLibrary(L"DllDemo.dll");  
  8.     if(!hInst)  
  9.     {  
  10.         printf("加载MathFuns.dll失败!\n");  
  11.     }  
  12.     typedef int (*DllDemoAPIProc)(int a, int b);  
  13.     DllDemoAPIProc Add = (DllDemoAPIProc)::GetProcAddress(hInst,"add");  
  14.     printf("5+3=%d\n",Add(5,3));  
  15.     ::FreeLibrary(hInst);  
  16.       
  17.       
  18.     return 0;  
  19. }  


      
    编译运行得结果:



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在Visual Studio中使用C++创建和使用DLL | 果冻想
C/C++中动态链接库的创建和调用
VC 的DLL应用(含Demo演示) - 一点一滴的Beer - 博客园
C语言第一个DLL程序
利用VC调用动态链接库中的函数
在C#中调用VC编写的dll库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服