Building a sample native library (C++)

In the previous section, we learned about cross-platform implementation, and how to Interop with existing native and Mono libraries. To demonstrate interoperability, we created small sample applications. Let's start with building our first native library in C++. Follow these steps:

  1. Open Visual Studio and select Windows Desktop under Visual C++ and select the project type Dynamic-Link Library (DLL). In this example, we name the project ExampleDLL and provide the location where we want to create the project:
  1. Right-click on the header files folder and create a new header file. In this example, we named it Calculate.h. The Calculate header file contains mathematical operations such as the summation of two integer numbers, multiplication, and division:
#ifndef Calculate
#define Calculate

extern "C"
{
__declspec(dllexport)int Sum(int a, int b)
{
return a + b;
}
__declspec(dllexport) int Multiply(int number1, int number2)
{
int result = number1 * number2;
return result;
}
__declspec(dllexport) double divide(int number1, int number2)
{
double result = 0.0;
if (number2 != 0)
result = number1 / number2;
return result;
}
}
#endif

In the preceding example, we defined a header called Calculate.

  1. extern "C" is used to instruct that the compiler will use the C function naming convention, not C++. Most code uses this directive because C function names are clearer to understand than C++. __declspec(dllexport) is utilized for export. In Microsoft's new versions of compiler, we can export data, functions, classes, or class member functions from a DLL utilizing this keyword, __declspec(dllexport), the export directive to the object file, so we don't have to utilize a .def record. This accommodation is most obvious when endeavoring to export decorated C++ function names. Since there is no standard specification for name decoration, the name of an exported function may change between compiler versions. To export functions, the __declspec(dllexport) keyword must appear to the left side of the calling-convention keyword, if a keyword is indicated, for instance: __declspec(dllexport)int Sum(int a, int b).

To export all public data members and functions in a class, the keyword must appear to the left side of the class name, as shown here:
Class __declspec(dllexport)int Calculations : public CPPObj { Class definition};.
When building your DLL, we normally make a header file that contains the function models, as well as classes we are trading, and include __declspec(dllexport) to the announcements in the header file. To make our code more intelligible, we can define a macro for __declspec(dllexport) and use the macro with each symbol we are sending out:
# define DLLExplort __declspec(dllexport):

  1. In the preceding example, we created a sample native library, and now we will see how we can access it. In the preceding code, we used __declspec(dllexport); to acccess this DLL, we will use DllImport. Click on Add | New Project; under C#, select .NET Core Console Application. In this example, we called it InteropWithCS. Open the solution and add the following code:
using System;
using System.Runtime.InteropServices;
namespace InteropWithCS
{
class Program
{
[DllImport(@"C:Users eshrivDocumentsVisual Studio
2017ProjectsExampleDLLDebugExampleDLL.dll")]

static extern int Multiply(int number1, int number2);

[DllImport(@"C:Users eshrivDocumentsVisual Studio
2017ProjectsExampleDLLDebugExampleDLL.dll")]

static extern double divide(int number1, int number2);

[DllImport(@"C:Users eshrivDocumentsVisual Studio
2017ProjectsExampleDLLDebugExampleDLL.dll")]

static extern int Sum(int a, int b);
static void Main(string[] args)
{

Console.WriteLine("Enter number1");
int number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number2");
int number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Select Operation : 1 for Sum , 2 for
multiply, 3 for divide");
int option = Convert.ToInt32(Console.ReadLine());
int result ;
switch (option)
{
case 1:
result = Sum(number1, number2);
Console.WriteLine("You have selected Sum
operation!
Sum is : " + result);
Console.ReadLine();
break;
case 2:
result = Multiply(number1, number2);
Console.WriteLine("You have selected Sum
operation!
multiplication is : " + result);
Console.ReadLine();
break;
case 3:
double result1 = divide(number1, number2);
Console.WriteLine("You have selected Sum
operation!
division is : " + result1);
Console.ReadLine();
break;
}

}

}

}
  1. In this example, we are using ExampleDLL and doing our calculations, taking input for number1 and number2 values. The user can select an operation—sum, multiply, or divide by pressing the corresponding number. On option number input, we check the user option, use switch case, and display the result:

This simple application gives us an understanding of how to create our own native library in C or C++, and how to set functions which can be exported and accessed from other languages, such as C#. The intention of making it simple and using small functions, such as sum, and multiplication, for example, is to make you aware of the functionality, so that the user won't get distracted with the heavy logic of functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset