Simple demo of using C# code to call C++ code and methods
- Visual Studio
- .Net Framework
- Compatible compiler
Create a new C++ project in Visual Studio. Choose "Dynamic-Link Library (DLL)" as the project type.
#pragma once
class Sample {
int x;
public:
Sample(int x);
int add(int x);
int multiply(int x);
};
// Helper methods for constructor and other method
extern "C" _declspec(dllexport) void* Instantiate(int x) {
return (void*) new Sample(x);
}
extern "C" _declspec(dllexport) int Add(Sample* t, int y) {
return t->add(y);
}
extern "C" _declspec(dllexport) int Multiply(Sample* t, int y) {
return t->multiply(y);
}
#include "pch.h"
#include "sample.h"
// Example Constructor
Sample::Sample(int x) {
this->x = x;
}
// Example methods below
int Sample::add(int y) {
return this->x + y;
}
int Sample::multiply(int y) {
return this->x + y;
}
- To enable C# code to interact with the c++ code:
Right Click on the C# project > Properties > Configuration Properties > Advanced > Common Language Runtime Support > Select Common Language Runtime Support (/clr)
This will generate a DLL (e.g., YourDllName.dll) in the output directory
// Write this code on top of your c# source file
using System.Runtime.InteropServices;
1. Create a wrapper class to encapsulate all the dll methods
2. Declare the C++ function you want to call using the DllImport attribute.
3. Specify the name of the C++ DLL and the calling convention (CallingConvention.Cdecl for C++).
4. Define some C# methods that wraps the import c++ functions
public static class WrapperClass
{
#region dllImports
// Target the cpp dll here -- add project reference to this dll in C# project also
private const string _dllImportPath = @"filePathofYourDll";
// Declare the C++ function you want to call using the DllImport attribute.
// Specify the name of the C++ DLL and the calling convention (CallingConvention.Cdecl for C++).
[DllImport(_dllImportPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Instantiate(int x);
[DllImport(_dllImportPath, CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(IntPtr t, int y);
[DllImport(_dllImportPath, CallingConvention = CallingConvention.Cdecl)]
public static extern int Multiply(IntPtr t, int y);
#endregion
}
public class Program
{
public static void Main(string[] args)
{
#region callDllMethods
IntPtr i = WrapperClass.Instantiate(2);
int total = WrapperClass.Add(i, 3);
int total2 = WrapperClass.Multiply(i, 10);
Console.WriteLine("Total Add method: " + total + ", Total Multiply method: " + total2);
#endregion
}
}
- This serves as a minimalistic and simple demonstration of C# and C++ code interaction
- Extra caution should be taken by the user to ensure type safety