zoukankan      html  css  js  c++  java
  • dll相关(二)

    1、如何制作dll?

    使用vs2012,新建控制台应用程序,在类型中选择DLL即可。

    在工程中添加.h文件和.cpp文件,如MathFuncsDLL.h和MathFuncsDll.cpp,这里展示一个类和一个函数,代码如下:

     1 // MathFuncsDll.h
     2 
     3 #ifdef MATHFUNCSDLL_EXPORTS
     4 #define MATHFUNCSDLL_API __declspec(dllexport) 
     5 #else
     6 #define MATHFUNCSDLL_API __declspec(dllimport) 
     7 #endif
     8 
     9 namespace MathFuncs
    10 {
    11     // This class is exported from the MathFuncsDll.dll
    12     class MyMathFuncs
    13     {
    14     public: 
    15         // Returns a + b
    16         static MATHFUNCSDLL_API double Add(double a, double b); 
    17 
    18         // Returns a - b
    19         static MATHFUNCSDLL_API double Subtract(double a, double b); 
    20 
    21         // Returns a * b
    22         static MATHFUNCSDLL_API double Multiply(double a, double b); 
    23 
    24         // Returns a / b
    25         // Throws const std::invalid_argument& if b is 0
    26         static MATHFUNCSDLL_API double Divide(double a, double b); 
    27     };
    28     MATHFUNCSDLL_API double Add(double a, double b);
    29 }
    30 extern "C" __declspec(dllexport) double add(double a, double b);
     1 // MathFuncsDll.cpp : Defines the exported functions for the DLL application.
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "MathFuncsDll.h"
     6 #include <stdexcept>
     7 
     8 using namespace std;
     9 
    10 namespace MathFuncs
    11 {
    12     double MyMathFuncs::Add(double a, double b)
    13     {
    14         return a + b;
    15     }
    16 
    17     double MyMathFuncs::Subtract(double a, double b)
    18     {
    19         return a - b;
    20     }
    21 
    22     double MyMathFuncs::Multiply(double a, double b)
    23     {
    24         return a * b;
    25     }
    26 
    27     double MyMathFuncs::Divide(double a, double b)
    28     {
    29         if (b == 0)
    30         {
    31             throw invalid_argument("b cannot be zero!");
    32         }
    33 
    34         return a / b;
    35     }
    36 }
    37 double add(double a, double b)
    38 {
    39     return a + b;
    40 }

    之后链接,生成即可。

    2、如何调用dll?

    dll调用分为显示调用和隐式调用。显示调用中,只需将.h和.dll文件放入工程中,在需要调用的代码中将.h文件include进来,然后使用类似如下的代码调用函数:

    1 #include "MathFuncsDll.h"
    2 
    3 typedef double(*All)(double,double);
    4 All pFunction;
    5 pFunction=(All)GetProcAddress(hinstance,"add");
    6 cout<<pFunction(10,10)<<endl;    

    隐式调用:需要将.h,.lib,.dll文件添加入工程中,include进来.h文件,直接访问函数即可。代码如下:

    1 #include "MathFuncsDll.h"
    2 
    3 cout<<add(10,10)<<endl;
  • 相关阅读:
    rename 批量重命名
    shell脚本实现轮询查看进程是否结束
    mysql 修改max_connections
    window10下的solr6.1.0入门笔记之---安装部署
    php下载大文件
    【转】Pyhton 单行、多行注释符号使用方法及规范
    window10系统下使用python版本实现mysql查询
    Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
    【Visual Studio】 使用EF、 Linq2Sql快速创建数据交互层(一)
    【OPCAutomation】 使用OPCAutomation实现对OPC数据的访问
  • 原文地址:https://www.cnblogs.com/mascure/p/3303259.html
Copyright © 2011-2022 走看看