博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Creating and Using a Dynamic Link Library
阅读量:6336 次
发布时间:2019-06-22

本文共 9108 字,大约阅读时间需要 30 分钟。

Walkthrough: Creating and Using a Dynamic Link Library 
原文链接:

In this walkthrough, you will create a dynamic link library (DLL) containing useful routines that can be used by other applications. Using DLLs is a great way to reuse code. Rather than re-implementing these routines in every program you create, you write them once and reference them from applications that need the functionality.

This walkthrough uses native C++. For a walkthrough using native C++ to create a static library, see . For a walkthrough using Visual C++ that targets the Common Language Runtime, see .

This walkthrough covers the following:

  • Creating a new dynamic link library (DLL) project

  • Adding a class to the dynamic link library

  • Creating an application that references the dynamic link library

  • Using the functionality from the class library in the console application

  • Running the application

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

To create a new dynamic link library (DLL) project

  1. From the File menu, select New and then Project….

  2. From the Project types pane, under Visual C++, select Win32.

  3. From the Templates pane, select Win32 Console Application.

  4. Choose a name for the project, such as MathFuncsDll, and enter it in the Name field. Choose a name for the solution, such as DynamicLibrary, and enter it in the Solution Name field.

  5. Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application Wizard dialog, pressNext.

  6. From the Application Settings page of the Win32 Application Wizard, under Application type, select DLL if it is available or Console application if DLL is not available. Some versions of Visual Studio do not support creating a DLL project using wizards. You can change this later to make your project compile into a DLL.

  7. From the Application Settings page of the Win32 Application Wizard, under Additional options, select Empty project.

  8. Press Finish to create the project.

To add a class to the dynamic link library

  1. To create a header file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select Header File (.h). Choose a name for the header file, such as MathFuncsDll.h, and press Add. A blank file will be displayed.

  2. Add a simple class named MyMathFuncs to do common mathematical operations, such as addition, subtraction, multiplication, and division. The code should resemble the following:

    // MathFuncsDll.hnamespace MathFuncs{    class MyMathFuncs    {    public:        // Returns a + b        static __declspec(dllexport) double Add(double a, double b);        // Returns a - b        static __declspec(dllexport) double Subtract(double a, double b);        // Returns a * b        static __declspec(dllexport) double Multiply(double a, double b);        // Returns a / b        // Throws DivideByZeroException if b is 0        static __declspec(dllexport) double Divide(double a, double b);    };}
  3. Note the __declspec(dllexport) modifier in the method declarations above. These modifiers enable the method to be exported by the DLL so they can be used by other applications. For more information, see .

  4. To create a source file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as MathFuncsDll.cpp, and press Add. A blank file will be displayed.

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

    // MathFuncsDll.cpp// compile with: /EHsc /LD#include "MathFuncsDll.h"#include 
    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 == 0) { throw new invalid_argument("b cannot be zero!"); } return a / b; }}
  6. To build the project into a DLL, from the Project menu, select MathFuncsDll Properties…. From the left pane, underConfiguration Properties, select General. From the right pane, change the Configuration Type to Dynamic Library (.dll). Press OK to save the changes.

    NoteNote

    If building from the command line, use the /LD compiler option to specify that the output file should be a DLL. For more information, see .

  7. Compile the dynamic link library by selecting Build Solution from the Build menu. This creates a DLL that can be used by other programs. For more information on DLLs, see .

To create an application that references the dynamic link library

  1. To create an application that will reference and use the dynamic link library that was just created, from the File menu, select New and then Project….

  2. From the Project types pane, under Visual C++, select Win32.

  3. From the Templates pane, select Win32 Console Application.

  4. Choose a name for the project, such as MyExecRefsDll, and enter it in the Name field. Next to Solution, select Add to Solution from the drop down list. This will add the new project to the same solution as the dynamic link library.

  5. Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.

  6. From the Application Settings page of the Win32 Application Wizard, under Application type, select Console application.

  7. From the Application Settings page of the Win32 Application Wizard, under Additional options, deselectPrecompiled header.

  8. Press Finish to create the project.

To use the functionality from the class library in the console application

  1. After you create a new Console Application, an empty program is created for you. The name for the source file will be the same as the name you chose for the project above. In this example, it is named MyExecRefsDll.cpp.

  2. To use the math routines that were created in the dynamic link library, you must reference it. To do this, selectReferences… from the Project menu. From the Property Pages dialog, expand the Common Properties node and selectReferences. Then select the Add New Reference… button. For more information on the References… dialog, see.

  3. The Add Reference dialog is displayed. This dialog lists all the libraries that you can reference. The Project tab lists all the projects in the current solution and any libraries they contain. From the Projects tab, selectMathFuncsDll. Then select OK. For more information on the Add Reference dialog, see .

  4. To reference the header files of the dynamic link library, you must modify the include directories path. To do this, from the Property Pages dialog, expand the Configuration Properties node, then the C/C++ node, and select General. Next to Additional Include Directories, type in the path to the location of the MathFuncsDll.h header file.

  5. Dynamic link libraries are not loaded by the executable until runtime. You must tell the system where to locateMathFuncsDll.dll. This is done using the PATH environment variable. To do this, from the Property Pages dialog, expand the Configuration Properties node and select Debugging. Next to Environment, type in the following: PATH=<path to MathFuncsDll.dll file>, where <path to MathFuncsDll.dll file> is replaced with the actual location ofMathFuncsDll.dll. Press OK to save all the changes made.

    NoteNote

    If you intend to run the executable from the command line rather than from within Visual Studio, then you must manually update the PATH environment variable from the command prompt as follows: set PATH=%PATH%;<path to MathFuncsDll.dll file>, where <path to MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll.

  6. 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 
    #include "MathFuncsDll.h"using namespace std;int main(){ double a = 7.4; int b = 99; 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; return 0;}
  7. Build the executable by selecting Build Solution from the Build menu.

To run the application

  1. Make sure MyExecRefsDll is selected as the default project. From the Solution Explorer, select MyExecRefsDll, and then select Set As StartUp Project from the Project menu.

  2. To run the project, select Start Without Debugging from the Debug menu. The output should look like this:

    a + b = 106.4a - b = -91.6a * b = 732.6a / b = 0.0747475
本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2010/03/04/1678494.html
,如需转载请自行联系原作者
你可能感兴趣的文章
man openstack >>1.txt
查看>>
linux几大服务器版本大比拼
查看>>
在BT5系统中安装postgresQL
查看>>
【Magedu】Week01
查看>>
写给MongoDB开发者的50条建议Tip25
查看>>
为什么要让带宽制约云计算发展
查看>>
2012-8-5
查看>>
VS中ProjectDir的值以及$(ProjectDir)../的含义
查看>>
我的友情链接
查看>>
PHP实现排序算法
查看>>
Business Contact Mnanager for Outlook2010
查看>>
9种用户体验设计的状态是必须知道的(五)
查看>>
解决WIN7下组播问题
查看>>
陈松松:视频营销成交率低,这三个因素没到位
查看>>
vmware nat模式原理探究,实现虚拟机跨网段管理
查看>>
JavaSE 学习参考:集合运算
查看>>
【Signals and Systems】 SYLLABUS
查看>>
RH135-2-command-line-interface
查看>>
浅谈OS
查看>>
mac下开启docker API远程调用
查看>>