Using a Visual C++/CLI Library



One of the most important sought goals in .NET is to allow different languages to collaborate, such as sharing code. One way this can happen is to be able to use the functionality of one language into a program create with another language. For example, you can use the rich library of Visual Basic functions in a C# application. As no library is ever complete, you may still need functionality that is not easily found in the language you are using. Furthermore, you may be working with a team of programmers who are using different languages and/or have already created a set of functions or complex operations. You should be able to use that existing code.


Creating a Library

In previous years, it used to be a challenge to create a library, especially in C++. Fortunately, Microsoft Visual C++ now makes it particularly easy to create one, because a wizard highly assists you. To create a library, first display the New Project dialog box. After specifying Visual C++, in the Templates list, click Class Library and give it a name. In the body of the file, you can create the classes and/or functions as you see fit. Here is an example:

// Business.h

#pragma once

using namespace System;

namespace Business {

    public ref class Finance
	double CalculateDiscount(double MarkedPrice,
		                     double DiscountRate)
		return MarkedPrice * DiscountRate / 100;

Once the project is ready, you must build it (on the main menu, Build -> Build Business). As a result, the compiler would create a file with the .dll extension:

Normally, as far as creating a library, that's it.

Using the Library

Creating a library in C++ is easy. To use it, there are a few rules you must follow. To start, you must make sure that your project can "physically" find the library. Probably the easiest way to take care of this is to copy the dll file and paste it in the folder that contains your project's executable. You can also do this directly in Visual Studio by importing the library file as we saw earlier.

In your project, you should include the System.Runtime.InteropServices namespace. Before the section where the library will be accessed, enter the DllImport attribute that takes as argument the name of the library passed as a string. Here is an example:

using System;
using System.Runtime.InteropServices;
using Business;

namespace DepartmentStore
    class Exercise
        public static extern double CalculateDiscount(double price,
                                                      double discount)

        static int Main()
            Finance fin = new Finance();

	    double markedPrice  = 275.50;
	    double discountRate =  25.00; // %
            double discountAmount = fin.CalculateDiscount(markedPrice,
            double netPrice = markedPrice - discountAmount);

            Console.WriteLine("Marked Price:    {0:C}", markedPrice);
            Console.WriteLine("Discount Rate:   {0:P}", discountRate / 100);
            Console.WriteLine("Discount Amount: {0:C}", discountAmount);
            Console.WriteLine("Net Price:       {0:C}\n", netPrice);

	    return 0;

This makes your library code ready to be used, which you can do as you would any other code. This means that you can compile your program the way we did in the previous section.


Home Copyright 2008 Yevol