Create and use a dynamic link library

Source: Internet
Author: User
Create a new dynamic link library (DLL) project
    1. Select "new" from the "file" menu, and then select "project ...".

    2. Select "Win32" under "Visual C ++" from the "project type" pane ".

    3. From the "template" pane, select "Win32 console application"Program".

    4. Select a name for the project, as shown in figure"Mathfuncsdll"And enter the "name" field. Select a name for the solution, as shown in figure"Dynamiclibrary"And enter the "solution name" field.

    5. Press "OK" to start the Win32 Application Wizard. On the "Overview" page of the "Win32 Application Wizard" dialog box, click "Next ".

    6. From the "application settings" page in the "Win32 Application Wizard", select "DLL" under "application type" (if available ), or select "console application" (if "DLL" is not available ). Some versions of Visual Studio do not support creating DLL projects using the wizard. You can make changes later to compile the project as a DLL.

    7. From the "application settings" page of the "Win32 Application Wizard", select "Empty Project" under "additional options ".

    8. Click "finish" to create a project.

Add a class to the dynamic link library
  1. To create a header file for the new class, select "Add new project..." from the "project" menu ...". The "Add new item" dialog box is displayed. In the category pane, selectCode". From the "template" pane, select "header file (. h )". Select a name for the header file, as shown in figure"Mathfuncsdll. h"And press "add ". A blank file is displayed.

  2. Add"Mymathfuncs"To perform common arithmetic operations, such as addition, subtraction, multiplication, and division. The Code should be similar to the following:

     

    // Mathfuncsdll. h

    Namespace 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_ Declspec (dllexport)Modifier. These modifiers enable the DLL to export this method for use by other applications. For more information, see dllexport and dllimport.

  4. To create a source file for the new class, select "Add new project..." from the "project" menu ...". The "Add new item" dialog box is displayed. In the category pane, select code under Visual C ++ ". From the "template" pane, select "C ++ file (. cpp )". Select a name for the source file, as shown in figure"Mathfuncsdll. cpp"And press "add ". A blank file is displayed.

  5. Implementation in the source file"Mymathfuncs". The Code should be similar to the following:

     

    // Mathfuncsdll. cpp
    // Compile with:/ehs/ LD

    # 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 = 0 )
    {
    Throw New Invalid_argument ( " B cannot be zero! " );
    }

    Return A/B;
    }
    }
     

  6. To generate a project as a DLL, select"MathfuncsdllAnd then select "properties ...". In the left pane, select general under configuration properties ". From the right pane, change "configuration type" to "dynamic library (. dll )". Click OK to save the changes.

    Note:

    Use/LDThe compiler option specifies that the output file should be DLL. For more information, see/MD,/mt,/LD (use the Runtime Library ).

  7. Compile the dynamic link library by selecting "generate solution" in the "generate" menu ". In this way, a DLL is created for other programs. For more information about DLL, see DLL.

Create an application that references the dynamic link library
    1. To create an application that references and uses the dynamically linked library you just created, select "new" from the "file" menu and select "project ...".

    2. From the project type pane, select Win32 under Visual C ++ ".

    3. From the "template" pane, select "Win32 console application ".

    4. Select a name for the project, as shown in figure"Myexecrefsdll"And enter the "name" field. Select "add solution" from the drop-down list next to "solution ". This will add the new project to the same solution to which the dynamic link library belongs.

    5. Press OK to start the Win32 Application Wizard ". On the "Overview" page of the "Win32 Application Wizard" dialog box, click "Next ".

    6. From the "application settings" page of the "Win32 Application Wizard", select "console application" under "application type ".

    7. From the "application settings" page of the "Win32 Application Wizard", deselect the "pre-compilation Header" under "additional options ".

    8. Click "finish" to create a project.

Use the class library function in the console application
  1. After creating a new console application, an empty program will be created for you. The source file name is the same as the name you selected for the project. In this example"Myexecrefsdll. cpp".

  2. To use the arithmetic routine created in the dynamic link library, you must reference the dynamic link library. To do this, select "reference…" in the "project" menu ...". In the "property page" dialog box, expand the "common attributes" node and select "Reference ". Then select "Add new reference ..." Button. Reference ..." For more information about the dialog box, see the <projectname> properties page Dialog Box> general attributes> reference ".

  3. The "add reference" dialog box is displayed. This dialog box lists all databases that can be referenced. The projects tab lists all the projects in the current solution and all the libraries they contain. From the projects tab, select"Mathfuncsdll". Then select "OK ". For more information about the "add reference" dialog box, see the "add reference" dialog box.

  4. To reference the header file of the dynamic link library, you must modify the directory path. In the "properties page" dialog box, expand the "Configure attributes" node, expand the "C/C ++" node, and select "General ". Next to "add include directory", Type"Mathfuncsdll. h"Path of the header file.

  5. The dynamic link library is only loaded by the executable file at runtime. Must tell the system where to find"Mathfuncsdll. dll". This is usedPathEnvironment variable implementation. To do this, expand the "Configure attributes" node in the "properties page" dialog box and select "debug ". Next to "environment", type the following:Path = <path to mathfuncsdll. dll File>, Where<Path to mathfuncsdll. dll File>Should be replaced"Mathfuncsdll. dll". Click OK to save all changes.

    Note:

    If you want to run the executable file from the command line instead of from Visual Studio, you must manually update the file from the command prompt.PathEnvironment variables are as follows:Set Path = % PATH %; <path to mathfuncsdll. dll File>, Where<Path to mathfuncsdll. dll File>Should be replaced"Mathfuncsdll. dll".

6. Now, you can use"Mymathfuncs"Class. Use the following code to replace"Myexecrefsdll. cpp"Content:

//  Myexecrefsdll. cpp
// Compile with:/ehs/ link mathfuncsdll. Lib

# Include <iostream>

# 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;
System ( " Pause " );
Return 0 ;
}

7. Select "generate solution" from the "generate" menu to generate an executable file.

Run the application
    1. Make sure you select"Myexecrefsdll"As the default project. From Solution Explorer, select"Myexecrefsdll"Select "set as startup project" in the "project" menu ".

    2. To run the project, select "start execution (not debugging)" in the "debug" menu )". The output should be as follows:

      A + B =106.4
      A-B =-91.6
      A * B =732.6
      A/B =0.0747475
      Converted from msdn: http://msdn.microsoft.com/zh-cn/library/ms235636 (vs.80). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.