当前位置: 首页 > news >正文

VS2010 c++生成和调用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文件,加入代码如下:

 

[cpp]  view plain  copy
 
  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头文件,加入代码如下:

    

 

[cpp]  view plain  copy
 
  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中添加以下代码:
    

[cpp]  view plain  copy
 
  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文件,加入代码如下:

 

[cpp]  view plain  copy
 
  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头文件,加入代码如下:

 

 

[cpp]  view plain  copy
 
  1. LIBRARY DllDemo  
  2.   
  3. EXPORTS  
  4. add  
  5. subtract  
  6. multiple  


    

 

    编辑,生成,可以看到Debug目录里生成了以下这些文件:
    
    
    调用dll:
    同上,新建一个控制台应用程序,取名InvokeDll
    在InvokeDll.cpp中添加以下代码:
    
    

[cpp]  view plain  copy
 
  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. }  



      
    编译运行得结果:

 

 

点此下载示例源代码

参考 http://blog.csdn.net/big_wang5/article/details/8216557 
c++新手 
一、DLL简介 
包含能被可执行程序或其他DLL调用的函数。动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数。函数的可执行代码位于一个 DLL 中,该 DLL 包含一个或多个已被编译、链接并与使用它们的进程分开存储的函数。 
若要访问dll中的函数,该函数必须是导出的(_declspec(dllexport)关键字),在命令提示符下使用dumpbin函数查看导出情况。

二、DLL的2中加载方式:隐式连接和显示加载。

1.隐式连接 
生成dll 
使用_declspec(dllexport)关键字导出函数,使用静态调用dll 
VS2010中新建控制台程序,在“应用程序设置”页面的“应用程序类型”下,选择“DLL”和空项目。 
创建头文件,命名为DllDemo.h

    /**********************************************/  
    /*FileName:DllDemo.h                          */  
    /**********************************************/  

    #ifdef DllDemoAPI  
    #else  
    #define DllDemoAPI _declspec(dllimport)  
    #endif  

    DllDemoAPI int add(int a, int b);  
    DllDemoAPI int subtract(int a, int b);  
    DllDemoAPI int multiple(int a, int b);  

    class DllDemoAPI Point  
    {  
    public:  
        void Print(int x, int y);  
    };  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

创建源文件,命名为DllDemo.cpp

    /**********************************************/  
    /*FileName:DllDemo.cpp                        */  
    /**********************************************/  

    #define DllDemoAPI _declspec(dllexport)  
    #include "DllDemo.h"  
    #include <stdio.h>  

    DllDemoAPI int add(int a, int b)  
    {  
        return a+b;  
    }  

    DllDemoAPI int subtract(int a, int b)  
    {  
        return a-b;  
    }  

    DllDemoAPI int multiple(int a, int b)  
    {  
        return a*b;  
    }  

    DllDemoAPI void Point::Print(int x, int y)  
    {  
        printf("x=%d,y=%d",x,y);  
    }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

生成,Debug目录里生成了以下这些文件 
这里写图片描述

调用dll 
新建控制台应用程序InvokeDll,默认即可。 
InvokeDll.cpp中添加如下代码:

    // InvokeDll.cpp : 定义控制台应用程序的入口点。  
    //  

    #include "stdafx.h"  
    #include <Windows.h>  
    #include "F:\PCLBOOK\DllDemo\DllDemo\DllDemo.h"  

    int _tmain(int argc, _TCHAR* argv[])  
    {  
        /*加载dll函数调用方式为默认调用方式*/  

        printf("5+3=%d\n",add(5,3));   

        Point p;  
        p.Print(5,3);  

        system("pause");  
        return 0;  
    }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

右键InvokeDll项目,选择“属性”选项,在“链接器”选择的字选项“输入”|“外部依赖项”中添加DemoDll.lib 
编译运行得结果 
这里写图片描述

错误提示: 
1、这里写图片描述 
项目–属性–清单工具–输入输出–嵌入式菜单–将是改成否即可。 
2、error LNK1104: 无法打开文件“DllDemo.lib” 
在“属性”-》链接器-》常规-》附加库目录 和“属性”-》链接器-》输入-》附加依赖项 中都添加上了Dll1.lib库文件的完整路径如“F:\PCLBOOK\DllDemo\Debug\DllDemo.lib”后就成功了。

相关文章:

  • 使用mongoose和bcrypt实现用户密码加密
  • Android WebView基本用法及常见问题
  • 9.Azure文件(文件共享)-NAS(下)
  • 用CORS 解决vue.js django跨域调用
  • 2U 4节点Xeon SP服务器 (上):PowerEdge C6420更受重视
  • Laravel5.5 生成测试数据
  • Docker CentOS7 安装SSH
  • BZOJ3273 : liars
  • Python学习记录——Ubuntu(一)基本配置、快捷键和系统启停命令行
  • 使用Shiro进行权限控制的实现流程
  • mysql开发之---每日一得01
  • 网络基础CCNP|OSPF(5)
  • Npoi导出excel整理(附源码)
  • 理解数据库连接池底层原理之手写实现
  • python结巴(jieba)分词
  • [笔记] php常见简单功能及函数
  • CAP理论的例子讲解
  • es6要点
  • ES6之路之模块详解
  • IDEA 插件开发入门教程
  • Java程序员幽默爆笑锦集
  • leetcode46 Permutation 排列组合
  • MobX
  • Mysql5.6主从复制
  • Object.assign方法不能实现深复制
  • python3 使用 asyncio 代替线程
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • springboot_database项目介绍
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • 从setTimeout-setInterval看JS线程
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 机器学习 vs. 深度学习
  • 记录:CentOS7.2配置LNMP环境记录
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 如何设计一个比特币钱包服务
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 微信支付JSAPI,实测!终极方案
  • 小李飞刀:SQL题目刷起来!
  • 验证码识别技术——15分钟带你突破各种复杂不定长验证码
  • 最近的计划
  • #define,static,const,三种常量的区别
  • #Spring-boot高级
  • $forceUpdate()函数
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (30)数组元素和与数字和的绝对差
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (转) ns2/nam与nam实现相关的文件
  • (转载)Linux 多线程条件变量同步
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .htaccess配置常用技巧