• 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;
  • 相关阅读:
    [置顶] 也论百度轻应用--一个开发者的吐槽
    HDU 1506 Largest Rectangle in a Histogram
    [置顶] 搭建apache+tomcat+memcached集群环境
    (step8.2.6)hdu 1848(Fibonacci again and again——组合博弈)
    SQL2005、2008、2000 清空删除日志
    网络获取北京时间和系统时间
    C++界面库
    Cocos2d-X游戏开发之Windows7+VS2010环境搭建(亲测)
    华为面试题——一道关于指针方面的编程题(C/C++)
    java之Math
  • 原文地址:https://www.cnblogs.com/mascure/p/3303259.html
Copyright © 2020-2023  润新知