Original Link: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx

Following content is only used for knowledge sharing. ^^

__________________________Have__A__Nice___Trip____________________________

his step-by-step walkthrough shows how to create a dynamic link library (DLL) for use with a C++ app. Using a library is a great way to reuse code. Rather than re-implementing the same routines in every program that you create, you write them one time and then reference them from apps that require the functionality. By putting code in the DLL, you save space in every app that references it, and you can update the DLL without recompiling all of the apps. For more information about DLLs, see DLLs in Visual C++.

This walkthrough covers these tasks:

  • Creating a DLL project.

  • Adding a class to the DLL.

  • Creating a console app that uses load-time dynamic linking to reference the DLL.

  • Using the functionality from the class in the app.

  • Running the app.

This walkthrough creates a DLL that can only be called from apps that use C++ calling conventions. For information about how to create DLLs for use with other languages, see Calling DLL Functions from Visual Basic Applications.

This topic assumes that you understand the fundamentals of the C++ language.

To create a dynamic link library (DLL) project

  1. On the menu bar, choose File, New, Project.

  2. In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Win32.

  3. In the center pane, select Win32 Console Application.

  4. Specify a name for the project—for example, MathFuncsDll—in the Name box. Specify a name for the solution—for example, DynamicLibrary—in the Solution name box. Choose the OK button.

  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.

  6. On the Application Settings page, under Application type, select DLL.

  7. Choose the Finish button to create the project.

To add a class to the dynamic link library

  1. To create a header file for a new class, on the menu bar, choose Project, Add New Item. In the Add New Item dialog box, in the left pane, under Visual C++, select Code. In the center pane, select Header File (.h). Specify a name for the header file—for example, MathFuncsDll.h—and then choose the Add button. A blank header file is displayed.

  2. Add the following code to the beginning of the header file:

    // MathFuncsDll.h
    
    #ifdef MATHFUNCSDLL_EXPORTS
    #define MATHFUNCSDLL_API __declspec(dllexport)
    #else
    #define MATHFUNCSDLL_API __declspec(dllimport)
    #endif
     
  3. Add a basic class named MyMathFuncs to perform common mathematical operations such as addition, subtraction, multiplication, and division. The code should resemble this:

    namespace MathFuncs
    {
    // This class is exported from the MathFuncsDll.dll
    class MyMathFuncs
    {
    public:
    // Returns a + b
    static MATHFUNCSDLL_API double Add(double a, double b); // Returns a - b
    static MATHFUNCSDLL_API double Subtract(double a, double b); // Returns a * b
    static MATHFUNCSDLL_API double Multiply(double a, double b); // Returns a / b
    // Throws const std::invalid_argument& if b is 0
    static MATHFUNCSDLL_API double Divide(double a, double b);
    };
    }

    When the MATHFUNCSDLL_EXPORTS symbol is defined, the MATHFUNCSDLL_API symbol will set the __declspec(dllexport) modifier in the member function declarations in this code. This modifier enables the function to be exported by the DLL so that it can be used by other applications. When MATHFUNCSDLL_EXPORTS is undefined—for example, when the header file is included by an application—MATHFUNCSDLL_API defines the __declspec(dllimport) modifier in the member function declarations. This modifier optimizes the import of the function in an application. By default, the New Project template for a DLL adds PROJECTNAME_EXPORTS to the defined symbols for the DLL project. In this example, MATHFUNCSDLL_EXPORTS is defined when your MathFuncsDll project is built. For more information, see dllexport, dllimport.

    Note

    If you are building the DLL project on the command line, use the /D compiler option to define the MATHFUNCSDLL_EXPORTS symbol.

  4. In the MathFuncsDll project in Solution Explorer, in the Source Files folder, open MathFuncsDll.cpp.

  5. Implement the functionality for MyMathFuncs in the source file. The code should resemble this:

    // MathFuncsDll.cpp : Defines the exported functions for the DLL application.
    // #include "stdafx.h"
    #include "MathFuncsDll.h"
    #include <stdexcept> using namespace std; namespace MathFuncs
    {
    double MyMathFuncs::Add(double a, double b)
    {
    return a + b;
    } double MyMathFuncs::Subtract(double a, double b)
    {
    return a - b;
    } double MyMathFuncs::Multiply(double a, double b)
    {
    return a * b;
    } double MyMathFuncs::Divide(double a, double b)
    {
    if (b == )
    {
    throw invalid_argument("b cannot be zero!");
    } return a / b;
    }
    }

Compile the dynamic link library by choosing Build, Build Solution on the menu bar.

 Note

If you are using an Express edition that does not display a Build menu, on the menu bar, choose Tools, Settings, Expert Settings to enable it, and then choose Build, Build Solution.

Note

If you are building a project on the command line, use the /LD compiler option to specify that the output file is to be a DLL. For more information, see /MD, /MT, /LD (Use Run-Time Library). Use the /EHsc compiler option to enable C++ exception handling. For more information, see /EH (Exception Handling Model).

To create an app that references the DLL

  1. To create a C++ app that will reference and use the DLL that you just created, on the menu bar, choose File, New, Project.

  2. In the left pane, under Visual C++, select Win32.

  3. In the center pane, select Win32 Console Application.

  4. Specify a name for the project—for example, MyExecRefsDll—in the Name box. Next to Solution, select Add to Solution from the drop-down list. This adds the new project to the same solution that contains the DLL. Choose the OK button.

  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.

  6. On the Application Settings page, under Application type, select Console application.

  7. On the Application Settings page, under Additional options, clear the Precompiled header check box.

  8. Choose the Finish button to create the project.

To use the functionality from the class library in the app

  1. After you create a console app, an empty program is created for you. The name for the source file is the same as the name that you chose earlier. In this example, it is named MyExecRefsDll.cpp.

  2. To use in the app the math routines that you created in the DLL, you must reference it. To do this, select the MyExecRefsDll project in Solution Explorer, and then on the menu bar, choose Project, References. In the Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Framework and References, Common Properties, <Projectname> Property Pages Dialog Box.

  3. The Add Reference dialog box lists the libraries that you can reference. The Project tab lists the projects in the current solution and any libraries that they contain. On the Projects tab, select the check box next to MathFuncsDll, and then choose the OK button.

  4. To reference the header files of the DLL, you must modify the included directories path. To do this, in the Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next toAdditional Include Directories, specify the path of the location of the MathFuncsDll.h header file. You can use a relative path—for example, ..\MathFuncsDll\—then choose the OK button.

  5. You can now use the MyMathFuncs class in this application. Replace the contents of MyExecRefsDll.cpp with the following code:

     
    // MyExecRefsDll.cpp
    // compile with: /EHsc /link MathFuncsDll.lib #include <iostream> #include "MathFuncsDll.h" using namespace std; int main()
    {
    double a = 7.4;
    int b = ; cout << "a + b = " <<
    MathFuncs::MyMathFuncs::Add(a, b) << endl;
    cout << "a - b = " <<
    MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
    cout << "a * b = " <<
    MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
    cout << "a / b = " <<
    MathFuncs::MyMathFuncs::Divide(a, b) << endl; try
    {
    cout << "a / 0 = " <<
    MathFuncs::MyMathFuncs::Divide(a, ) << endl;
    }
    catch (const invalid_argument &e)
    {
    cout << "Caught exception: " << e.what() << endl;
    } return ;
    }
  6. Build the executable by choosing Build, Build Solution on the menu bar.

To run the application

  1. Make sure that MyExecRefsDll is selected as the default project. In Solution Explorer, select MyExecRefsDll, and then on the menu bar, choose Project, Set As StartUp Project.

  2. To run the project, on the menu bar, choose Debug, Start Without Debugging. The output should resemble this:

    a + b = 106.4
    a - b = -91.6
    a * b = 732.6
    a / b = 0.0747475
    Caught exception: b cannot be zero!

Walkthrough: Creating and Using a Dynamic Link Library (C++)的更多相关文章

  1. Walkthrough: Create and use your own Dynamic Link Library (C++)

    参考网站:https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-librar ...

  2. How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)

    The Dynamic Link Library (DLL) is stored separately from the target application and shared among dif ...

  3. DYNAMIC LINK LIBRARY - DLL

    https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...

  4. Custom Action : dynamic link library

    工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...

  5. [DLL] Dynamic link library (dll) 的编写和使用教程

    前一阵子,项目里需要导出一个DLL,但是导出之后输出一直不怎么对,改了半天才算改对...读了一些DLL教程,感觉之后要把现在的代码导出,应该还要花不少功夫...下面教程参照我读的3个教程写成,所以内容 ...

  6. 动态链接库(Dynamic Link Library)

    DLL INTRODUCTION A DLL is a library that contains code and data that can be used by more than one pr ...

  7. 动态链接库(Dynamic Link Library)学习笔记(附PE文件分析)

    转载:http://www.cnblogs.com/yxin1322/archive/2008/03/08/donamiclinklibrary.html 作者:EricYou 转载请注明出处   注 ...

  8. How to Create DLL(Dynamic link library)

    该文章属于在YouTube视频上看到的,链接如下: https://www.youtube.com/watch?v=EmDJsl7C9-k&t=3s 1.创建一个工程并建立一个控制台程序 2. ...

  9. Linux Dynamic Shared Library && LD Linker

    目录 . 动态链接的意义 . 地址无关代码: PIC . 延迟版定(PLT Procedure Linkage Table) . 动态链接相关结构 . 动态链接的步骤和实现 . Linux动态链接器实 ...

随机推荐

  1. tomcat+JNDI+spring 的配置问题

    在做spring有关的项目时,往往需要配置数据源,当然配置的方式有很多种,可以单独写在一个properties文件中,这样修改数据源配置的话比较容易,也比较简单,下面介绍另外一种数据源的配置 利用jn ...

  2. Redis实战之征服 Redis + Jedis + Spring (一)

    Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: Redis实战 Re ...

  3. IPv6介绍

    一.为什么需要IPv6 为了扩大地址空间,拟通过IPv6重新定义地址空间.IPv4采用32位地址长度,只有大约43亿个地址,估计在2005-2010年间将被分配完毕,而IPv6采用128位地址长度,几 ...

  4. FloatingWindow

    https://github.com/dev0x10/android-bubble https://github.com/dev0x10/FloatingView https://github.com ...

  5. PKU Online Judge 1054:Cube (设置根节点)

    1054:Cube 总时间限制:   1000ms 内存限制: 131072kB   描述 Delayyy君很喜欢玩某个由Picks编写的方块游戏,游戏在一个由单位格组成的棋盘上进行. 游戏的主角是一 ...

  6. 给定一个字符串,仅由a,b,c 3种小写字母组成。

    package com.boco.study; /** * 题目详情 给定一个字符串,仅由a,b,c 3种小写字母组成. 当出现连续两个不同的字母时,你可以用另外一个字母替换它,如 有ab或ba连续出 ...

  7. C++和python使用struct传输二进制数据结构来实现

    网络编程问题往往涉及二进制数据的传输.在C++经常使用的传输是文本字符串和分组结构. 假设该数据可以预先送入连续的内存区域,然后让send函数来获得的第一个地址,这一块连续的内存区就能完成传输数据.文 ...

  8. Clover

    为您的 Windows Explorer 插上翅膀! Clover 是 Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能. 方便的 Tab ...

  9. js整理常用方法

    javascript对象合并或追加属性的方法 function objMerger(obj1, obj2){ for(var r in obj2){ //eval("obj1."+ ...

  10. How to Use Custom TTF Font on iOS

    Cocos2d-x uses FontLabel to draw customer ttf font before v2.0.3(including v2.0.3). Now it uses UIFo ...