Create and use dynamic link library (C ++) C # Call

Source: Internet
Author: User
  1. The first type of library we will create is the dynamic link library (DLL ). Using DLL is an excellent way to reuse code. You don't have to re-implement the same routine in every program you create, but you just need to write these routines once and then reference them from the application that requires this function.

    This drill covers the following:

    • Create a new dynamic link library (DLL) project.

    • Add a class to the dynamic link library.

    • Create an application that references the dynamic link library.

    • Use the class library function in the console application.

    • Run the application.

    Required

    This topic assumes that you have basic knowledge of the C ++ language. If you are just learning C ++, it is recommended that you refer to the C ++ beginner's Guide (C ++ beginner's guide) written by Herb schildt, the book can be from http://go.microsoft.com/fwlink? Linkid = 115303 online.

    Create a new dynamic link library (DLL) project
    1. Slave"File"In the menu, select"New"And then select"Project ...".

    2. In"Project type"In the pane, select"Visual c ++"Under"Win32".

    3. In"Template"In the pane, select"Win32 console application".

    4. Select a name for the project, such as mathfuncsdll, and enter"Name"Field. Select a name for the solution, such as dynamiclibrary, and enter it"Solution name"Field.

    5. Click"OK"Start the Win32 Application Wizard. In"Win32 Application Wizard"Dialog Box"Overview"Page, click"Next".

    6. In"Win32 Application Wizard"In"Application settings"Page, select"Application type"Under"DLL"(If available), or select"Console application"(If"DLL"Unavailable ). A certain version of Visual Studio does not support creating DLL projects by using the wizard. You can make changes later to compile the project as a DLL.

    7. In"Win32 Application Wizard"Of"Application settings"Page, select"Additional Options"Under"Empty Project".

    8. Click"Done"Create a project.

    Add a class to the dynamic link library
    1. To create a header file for the new class"Project"Select"Add new project ...". Will display"Add new project"Dialog box. In"Category"In the pane, select"Visual c ++"Under"Code". In"Template"Select"Header file (. h )". Select a name for the header file, such as mathfuncsdll. H, and click"Add". A blank file is displayed.

    2. Add a simple class named "mymathfuncs" to perform common arithmetic operations, such as addition, subtraction, multiplication, and division. The Code should be similar to the following:

      # Ifdef mathfuncsdll_exports
      # Define mathfuncsdll_api _ declspec (dllexport)
      # Else
      # Define mathfuncsdll_api _ declspec (dllimport)
      # Endif // windows header file:
      # Include <windows. h>
    3. // Returns a + B
      Extern "C" mathfuncsdll_api double add (double A, double B );
    4. // Returns a-B
      Extern "C" mathfuncsdll_api double subtract (double A, double B );
    5. // Returns a * B
      Extern "C" mathfuncsdll_api double multiply (double A, double B );
    6. // Returns a/B
      // Throws dividebyzeroexception if B is 0
      Extern "C" mathfuncsdll_api double divide (double A, double B );
    7. Note the _ declspec (dllexport) modifier in this code method declaration. These modifiers enable the DLL to export this method for use by other applications. For more information, see dllexport and dllimport.

    8. To create a source file for the new class"Project"Select"Add new project ...". Will display"Add new project"Dialog box. In"Category"In the pane, select"Visual c ++"Under"Code". In"Template"In the pane, select"C ++ file (. cpp )". Select a name for the source file, such as mathfuncsdll. cpp, and click"Add". A blank file is displayed.

    9. "Mymathfuncs" is implemented in the source file. The Code should be similar to the following:

      # Include "mathfuncsdll. H"
    10. # Include <stdexcept>
    11. Using namespace STD;

    12. Bool apientry dllmain (hmodule,
      DWORD ul_reason_for_call,
      Lpvoid lpreserved
      )
      {
      Return true;
      }
    13. Extern "C" Double add (double A, double B)
      {
      Return A + B;
      }
    14. Extern "C" Double subtract (double A, double B)
      {
      Return A-B;
      }
    15. Extern "C" Double multiply (double A, double B)
      {
      Return a * B;
      }
    16. Extern "C" Double divide (double A, double B)
      {
      If (B = 0)
      {
      Throw new invalid_argument ("B cannot be zero! ");
      }
      Return A/B;
      }
    17. Create a C # console application
    18. Create an apihelper class to manage dllimport and add using system. runtime. interopservices;
    19. Public class apihelper
      {
    20. [Dllimport ("mathfuncsdll. dll", entrypoint = "divide", callingconvention = callingconvention. stdcall)]
      Public static extern double divide (double A, double B );
    21. [Dllimport ("mathfuncsdll. dll")]
      Public static extern double add (Double X, Double Y );
      }

    1. Called in program,Copy all files in the debug folder to the bin/debug folder.
    2. Console. writeline (apihelper. Divide (2, 3 ));
      Console. writeline (apihelper. Add (2, 3 ));
      Console. Readline ();
      : Http://download.csdn.net/source/2980458
Related Article

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.