Explanation of Dynamic Link Libraries (DLLs) by creating a DLL library in C++ and using it with C#

Explanation of Dynamic Link Libraries (DLLs) by creating a DLL library in C++ and using it with C#

First of all, we need to understand what Dynamic Link Library (DLL) is. A DLL is something like a library that you make for your own purposes. Such as, in the C language, if you want to use the printf(“”) function, you must #include<stdio.h>. Which is nothing but a library and using this library, you will execute the corresponding functions. As a programmer, sometimes you need to make your own library for various purposes. You can use these libraries in every platform, every language and even every framework. DLLs are like EXEs but they are not executable. A DLL can contain functions, variables, classes, any resources(icons, images, files) and so on. It also can contains COM components and .NET libraries. A program loads a DLL at start-up, via the Win32 API load library.

So, here we have something known as a DLL library. You can make a DLL library and can it from every environment or framework as your wish. Imagine, you need to write code in C#. As far I know, C/C++ is pretty faster than C#. But, you can find a framework (.NET) in C# that can be very helpful to write the code. What if you can use both C# and C/C++ in your code? A DLL library shows its real beauty when using this for cross-platform code. You can use the framework and at the same time, you can use C/C++ for calculating a large amount of data for reducing time complexity. In that case, you must make a DLL library in C and make a relation with it in C# to get access to that DLL library.

I will make a DLL library with C++ that can execute a function called gpacounter and this function takes a number as a parameter and returns its specific GPA. So, here we go.

First of all, you need to write a DLL library making code with C++. The code is pretty straight forward. Open your Visual Studio then click New Project then select other language > Visual C++ > Win32 console application and then press OK. A new window will open. Click Next.

Here, you need to click on the DLL radio button. Otherwise a Dynamic Link Library will not be generated. Then click Finish. Then point the cursor over the project that is in the Solution Explorer. Right-click and add a new item. I mean create a new source file and name it. Remember about the extension. It must be .cpp. Then start writing code.

  1. #include < stdio.h >  
  2. extern "C"  
  3. {  
  4.     __declspec(dllexport)  
  5.     double gpacounter(double total_number)  
  6.     {  
  7.         if (total_number >= 80)  
  8.         {  
  9.             return 4;  
  10.         }  
  11.         if (total_number >= 75 && total_number <= 79.99)  
  12.         {  
  13.             return 3.75;  
  14.         }  
  15.         if (total_number >= 70 && total_number <= 74.99)  
  16.         {  
  17.             return 3.50;  
  18.         }  
  19.         if (total_number >= 65 && total_number <= 69.99)  
  20.         {  
  21.             return 3.25;  
  22.         }  
  23.         if (total_number >= 60 && total_number <= 64.99)  
  24.         {  
  25.             return 3.00;  
  26.         }  
  27.         if (total_number >= 55 && total_number <= 59.99)  
  28.         {  
  29.             return 2.75;  
  30.         }  
  31.         if (total_number >= 50 && total_number <= 54.99)  
  32.         {  
  33.             return 2.50;  
  34.         }  
  35.         if (total_number >= 45 && total_number <= 49.99)  
  36.         {  
  37.             return 2.25;  
  38.         }  
  39.         if (total_number >= 40 && total_number <= 44.99)  
  40.         {  
  41.             return 2.00;  
  42.         } else  
  43.         {  
  44.             return 0;  
  45.         }  
  46.     }  
  47. }  
Here we are exporting the DLL. So, in the third line we have written:
  1. __declspec(dllexport)  
This was for exporting the DLL reference. When you import this reference from C# code you will write the import string. We will learn about that now.

So, the Dynamic Link Library has been created. It has a function and I do believe you can of course understand the functionality of this function. Most of all, remember about the 2nd and 3rd lines code. These codes are necessary for making a DLL.

Now it's time for some C# code. Let's start. So, let's create a Windows Forms application and make a GUI like this:

It's pretty straight forward code here in the backend. Go to the TextBox event property. Double-click or enter in the key_down property. The code will be like this.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Runtime.InteropServices;  
  11. namespace Reference_with_DLL  
  12. {  
  13.     public partial class Form1: Form  
  14.     {  
  15.         [DllImport("GPACounter.dll")]  
  16.         public static extern double gpacounter(double total_number);  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.         private void textBox1_KeyDown(object sender, KeyEventArgs e)  
  22.         {  
  23.             if (e.KeyCode == Keys.Enter)  
  24.             {  
  25.                 double gpa = double.Parse(textBox1.Text);  
  26.                 double result = gpacounter(gpa);  
  27.                 label2.Text = result.ToString();  
  28.                 textBox1.Text = string.Empty;  
  29.                 textBox1.Focus();  
  30.             }  
  31.         }  
  32.     }  
  33. }
Don't forget to write using System.Runtime.InteropServices.

This is for importing the DLL reference. And the import string is [DllImport("GPACounter.dll")].

And after this line, we just called the function to import. Here is this code that was in the DLL file.

Now imagine, we have made a file DLL code. But where is the DLL? Where is the DLL file that was created? Go to your DLL file making project's folder. Then go to the debug folder. You will find a file with the extension .dll. This is the file we have made. Just copy the file and paste it into the debug folder of the C# project. You are done. Test it, see how it works.

So, here we have used two languages in just one application using a Dynamic Link Library.

Thank you for reading.

Have a nice day.

Similar Articles