Unmanaged C++ Dll Call From Managed C# Application

Here, you will learn the steps for using a simple C++ DLL in C# in .NET Framework 4.

Open Visual C++ Win 32 Project.

Unmanaged C++ Dll call From Managed C# Application
 
In Application Settings, chose "DLL" and select "Empty Project".

Unmanaged C++ Dll call From Managed C# Application
 
The solution will look like the following.

Unmanaged C++ Dll call From Managed C# Application
 
Add New Item (*.cpp) in the Source folder.

Unmanaged C++ Dll call From Managed C# Application
 
Select the C++ (.cpp) File type.

Unmanaged C++ Dll call From Managed C# Application
 
Write down the following simple code in this .cpp file. 
  1. #include < stdio.h >  
  2.   
  3.   extern "C" {  
  4.     __declspec(dllexportint add(int a, int b) {  
  5.       return a + b;  
  6.     }  
  7.     __declspec(dllexportint subtract(int a, int b) {  
  8.       return a - b;  
  9.     }  
  10.   }  

Now, export *.res file in the "Resource" folder for version information.

Unmanaged C++ Dll call From Managed C# Application
 
Now, the Solution Explorer will look like the following.

Unmanaged C++ Dll call From Managed C# Application
 
After compiling, you will see the Debug folder as below.

Unmanaged C++ Dll call From Managed C# Application
Now, our Unmanaged DLL is ready. Let us build a Managed C# App.

Unmanaged C++ Dll call From Managed C# Application
 
The design is like this.

Unmanaged C++ Dll call From Managed C# Application
 
The code is as follows. 
  1. [DllImport("OurDll.dll", CallingConvention = CallingConvention.Cdecl)]  
  2. publicstaticexternint subtract(int a, int b);  
  3.   
  4. privatevoid button2_Click(object sender, EventArgs e) {  
  5.   int x = Convert.ToInt32(textBox1.Text);  
  6.   int y = Convert.ToInt32(textBox2.Text);  
  7.   int z = subtract(x, y);  
  8.   MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  9. }  
The output will be like this.
 
Unmanaged C++ Dll call From Managed C# Application
 
In case of the previous version of .NET (3.5), the code will be like below. 

  1. [DllImport("OurDll.dll")]  
  2. publicstaticexternint subtract(int a, int b);  
  3.   
  4. privatevoid button2_Click(object sender, EventArgs e) {  
  5.   int x = Convert.ToInt32(textBox1.Text);  
  6.   int y = Convert.ToInt32(textBox2.Text);  
  7.   int z = subtract(x, y);  
  8.   MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);  

The output will be the same for both.


Explanation

extern "C" helps to show all code within brackets from outside.

__declspec(dllexport) int add(int a,int b) is a prefix which makes DLL functions available from your external application.

In .NET Framework 3.5 or the previous version, the code is like -

  1. [DllImport("OurDll.dll")]  
  2. publicstaticexternint add(int a, int b);  
For ..NET 4 and above, the code will be liek this.
  1. [DllImport("OurDll.dll", CallingConvention = CallingConvention.Cdecl)]  
  2. publicstaticexternint add(int a,int b);  
If you only write framework 3.5 [DllImport("OurDll.dll")], you get an exception -

"… the managed PInvoke signature does not match the unmanaged signature …"


Now, you can call a C++ dll from your C# application (.NET Framework 4)

Thank You!


Similar Articles