Home

Using a C++/CLI Library

 

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 another. As an illustration, we saw earlier that you could 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. Furthermore, you may be working with a team of C++ programmers who 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
    {
    public:
	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
    {
        [DllImport("Business.dll")]
        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,
                                                          discountDate);
            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.

Using the Win32 Library

The Microsoft Windows operating system was originally written in C, the parent language of C++ and C# (also of Java and JavaScript). To allow programmers to create applications, Microsoft released a library called Win32. This is a series of functions and classes, etc, that you previously had to use. As time has changed, you don't need to exclusively use Win32 anymore to create a Windows application. Nonetheless, Win32 is still everywhere and it is not completely avoidable because many or some of the actions you would want to perform in a Windows application are still available only in Win32. Fortunately, in most cases, it is not always difficult to use some of these functions in a C# applications, as long as you observe some rules. Here is an example:

using System;
using System.Runtime.InteropServices;

namespace Win32Applied
{
    class Program
    {
        [DllImport("Kernel32.dll")]
        public static extern bool SetConsoleTitle(string strMessage);

        static int Main()
        {
            SetConsoleTitle("C# Programming");

	    return 0;
        }
    }
}
 

Home Copyright © 2006-2016, FunctionX, Inc.