Unleashing the power of C++ within C# through P/Invoke

Pulkit Garg
3 min readDec 27, 2023

--

Hola!!
In this article we will cover how to use C++ code inside C# project.
Let’s First begin with some theoretical part of this concept to understand some technical terms.

Image showing C# and C++ logos

1. What is Managed and Unmanaged code.
— — — — — — — — — — — — — — — — — — — — — — — —
Managed code
— C# code which is executed by CLR runtime
— Code that can not access low level system resources (OS)
— This is also called “Safe code”.
— Automatic Garbage Collection, Memory Management, Type referencing are managed by CLR

Unmanaged code
— C# code which is directly executed by OS (resources external to CLR).
— Code can access low level system resources (OS memory, etc.)
— This is also called as “non safe Code”.
— No Automatic Garbage Collection, Type checking is bypassed.

C++ is unmanaged code and C# is managed code by CLR
C++ can use pointers to directly access memory whereas C# cannot by itself.
One of the issues faced in making C++ code interoperable with C# is how to go about moving data across the boundary between managed and unmanaged code. This process is known as “Marshaling”.

Now, lets jump directly to what you all are waiting for .. Hands-on Practice.
Lets get started with creating our first project in C++ (.dll library project).
now, if you are not a C++ developer, or not much code in cpp as primary language then i got you covered.
-> First download Visual studio 2022.
-> Install “Desktop development with c++” within visual studio installer.
-> Create a new project by using template c++ (.dll library project). Name the Project “Example” or name of your wish just dont forget to change the reference file names whereever required.
-> Under source files folder create a new file with “example.cpp”
-> Add this sample code under this file.
-> Compile/build the project.

#include "pch.h"
#include <stdio.h>

extern "C"
{
__declspec(dllexport) int Add(int a, int b)
{
return a + b;
}
__declspec(dllexport) int Subtract(int a, int b)
{
return a - b;
}

__declspec(dllexport) int Multiply(int a, int b)
{
return a * b;
}
}

Now, its time to create C# Project (Console Application)
-> Create a new console C# application
-> In Program.cs file add this code snippet.

using System.Runtime.InteropServices;

class Program
{
[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b);

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Subtract(int a, int b);

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Multiply(int a, int b);

public static void Main()
{

int first = 10, second = 5;
Console.WriteLine("Input are !");
Console.WriteLine("First : " + first);
Console.WriteLine("second : " + second);

Console.WriteLine("Add : " + Add(first, second));
Console.WriteLine("Subtract : " + Subtract(first, second));
Console.WriteLine("Multiply : " + Multiply(first, second));
}
}

Note: to add multiple functions, you need to add different [DllImport()] attribute for each function (as shown above).
-> Now Compile this C# program.
-> It will give you error, example.dll is not found (DllNotFoundException)
-> now you need to copy the output files (.dll and supporting files) from C++ output folder to bin/* of C# project compiled output.
-> Now, Again run the C# application.
-> It works now!!

Thanks for reading this;
Follow me for more such amazing content.

--

--