天天看点

Creating and Using a Dynamic Link Library

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 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

Creating and Using a Dynamic Link Library

  Prerequisites

<a></a>

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

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

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

From the Templates pane, select Win32 Console Application.

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.

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

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.

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

Press Finish to create the project.

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.

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

<a href="http://www.cnblogs.com/graphics/admin/javascript:CopyCode(" target="_blank">Copy Code</a>

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.

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

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.

Creating and Using a Dynamic Link Library

Note

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….

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.

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

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

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

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.

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.

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=&lt;path to MathFuncsDll.dll file&gt;, where &lt;path to MathFuncsDll.dll file&gt; is replaced with the actual location ofMathFuncsDll.dll. Press OK to save all the changes made.

Creating and Using a Dynamic Link Library

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%;&lt;path to MathFuncsDll.dll file&gt;, where &lt;path to MathFuncsDll.dll file&gt; is replaced with the actual location of MathFuncsDll.dll.

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

Build the executable by selecting Build Solution from the Build menu.

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.

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

本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2010/03/04/1678494.html,如需转载请自行联系原作者

继续阅读