Udostępnij za pośrednictwem


Walkthrough: Creating and Using a Managed Assembly (C++)

A managed assembly is a kind of library that you can create to reuse code efficiently. Instead of re-implementing the same routines in many programs, you can write them once and then reference them from applications that require the functionality.

This walkthrough covers the following tasks:

  • Creating a class library project.

  • Adding a class to the class library.

  • Creating an application that references the class library.

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

  • Running the application.

Prerequisites

To complete this walkthrough, you must understand the fundamentals of the C++ language.

To create a class library project

  1. On the File menu, point to New and then click Project.

  2. In the Project types pane, under Visual C++, select CLR.

    Every project type in this group creates a project that targets the common language runtime (CLR).

  3. In the Templates pane, select Class Library.

  4. In the Name box, type a name for the project, for example, MathFuncsAssembly. In the Solution Name field, type a name for the solution, for example, ManagedAssemblies.

  5. Click OK to create the project.

  6. By default, when a project is created, it is set up to use a precompiled header. To disable the precompiled header for the MathFuncsAssembly project, in Solution Explorer, select the project and then, on the Project menu, click Properties. Expand the Configuration Properties node, expand the C/C++ node, and then select Precompiled Headers. On the list next to Create/Use Precompiled Header, select Not Using Precompiled Header. Click OK to save these changes. For more information, see Creating Precompiled Header Files.

To add a class to the class library

  1. After you create a CLR Class Library, the wizard generates a basic class for you. The generated header file and source file both have the same name as the one you gave the project when you created it. In this example, they are named MathFuncsAssembly.h and MathFuncsAssembly.cpp.

  2. Replace the existing code in MathFuncsAssembly.h by using a basic class that is named MyMathFuncsAssembly. This class performs common mathematical operations such as addition, subtraction, multiplication, and division. The code should resemble the following example.

    // MathFuncsAssembly.h
    
    using namespace System;
    
    namespace MathFuncs
    {
        public ref class MyMathFuncs
        {
        public:
            // Returns a + b
            static double Add(double a, double b);
    
            // Returns a - b
            static double Subtract(double a, double b);
    
            // Returns a * b
            static double Multiply(double a, double b);
    
            // Returns a / b
            // Throws DivideByZeroException if b is 0
            static double Divide(double a, double b);
        };
    }
    
  3. Implement the functionality for MyMathFuncs in the source file. The code should resemble the following example.

    // MathFuncsAssembly.cpp
    // compile with: /clr /LD
    
    #include "MathFuncsAssembly.h"
    
    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 gcnew DivideByZeroException("b cannot be zero!");
            }
    
            return a / b;
        }
    }
    
  4. Compile the class library by clicking Build Solution on the Build menu. This creates a dynamic link library (DLL) that can be used by other programs. For more information about DLLs, see DLLs.

To create a console application that references the class library

  1. On the File menu, point to New and then click Project.

  2. In the Project types pane, under Visual C++, select CLR.

  3. In the Templates pane, select CLR Console Application.

  4. In the Name box, type a name for the project, for example, MyExecRefsAssembly. On the list next to Solution, select Add to Solution to add the new project to the solution that contains the class library.

  5. Click OK to create the project.

  6. Disable the precompiled header for the MyExecRefsAssembly project by selecting it in Solution Explorer and then, on the Project menu, clicking Properties. Expand the Configuration Properties node, expand the C/C++ node, and then select Precompiled Headers. On the list next to Create/Use Precompiled Header, select Not Using Precompiled Header. Click OK to save these changes.

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

  1. After you create a CLR Console Application, the wizard generates a program that just writes "Hello World" to the console. The generated source file has the same name as the one you gave the project when you created it. In this example, it is named MyExecRefsAssembly.cpp.

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

  3. The Add Reference dialog box lists all the libraries that you can reference. The .NET tab lists the libraries that are included with the .NET Framework. The COM tab lists all the COM components on your computer. The Project tab lists all the projects in the current solution and any libraries they contain. On the Projects tab, select MathFuncsAssembly and then click OK.

    Note

    You can reference an assembly directly from the source file by including the #using directive, for example, #using <MathFuncsAssembly.dll>. For more information, see #using Directive (C/C++).

  4. You can now use the MyMathFuncs class in this application. In MyExecRefsAssembly.cpp, replace the contents of the file function by using the following code.

    // MyExecRefsAssembly.cpp
    // compile with: /clr /FUMathFuncsAssembly.dll
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        double a = 7.4;
        int b = 99;
    
        Console::WriteLine("a + b = {0}",
            MathFuncs::MyMathFuncs::Add(a, b));
        Console::WriteLine("a - b = {0}",
            MathFuncs::MyMathFuncs::Subtract(a, b));
        Console::WriteLine("a * b = {0}",
            MathFuncs::MyMathFuncs::Multiply(a, b));
        Console::WriteLine("a / b = {0}",
            MathFuncs::MyMathFuncs::Divide(a, b));
    
        return 0;
    }
    
  5. Build the executable by clicking Build Solution on the Build menu.

To run the application

  1. Make sure that MyExecRefsAssembly is selected as the default project by selecting MyExecRefsAssembly in Solution Explorer, and then clicking Set As StartUp Project on the Project menu.

  2. To run the project, on the Debug menu, click Start Without Debugging. The output should resemble the following example.

    a + b = 106.4
    a - b = -91.6
    a * b = 732.6
    a / b = 0.0747474747474748
    

Next Steps

Previous: Walkthrough: Creating and Using a Static Library (C++). Next: Where to Go Next (C++).

See Also

Tasks

Visual C++ Guided Tour