Overview
Sometimes you need/have to use C++ together with C#. Normally you would create a class in C++ and export it as a dll which you would use in C#. Other times you would want to execute some C# code when something happens in C++. This post will go step by step showing you how to pass in a C# callback to C++ to be executed. You can get more information about how to accomplish this task here.
Content
In C#, declare a delegate which takes in 2 ints and returns nothing. Here we’re declaring the callback signature.
1
2
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void YourCallback(int, int);
In your C++ code, also define the same signature for the callback.
1
2
// YourCallback is now a function that takes in 2 ints and returns void
typedef void (__stdcall * YourCallback)(int, int);
We’re going to make a function in C++ which takes in our C# callback and executes it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this says that it will be exported
#define DLL __declspec(dllexport)
DLL void TakesInCallbackAndDoesStuff(YourCallback yourCallback) {
// do stuff
yourCallback(param1, param1);
}
/*
This is another way, just declaring that it will be exported in the signature
extern "C" __declspec(dllexport) void __stdcall TakesInCallbackAndDoesStuff(YourCallback yourCallback) {
// stuff
yourCallback(param1, param1);
}
*/
Now that we have the function in C++ that will do stuff and execute our callback when it’s done, we have to make a callback in C#.
1
2
3
4
5
YourCallback callback =
(intParam1, intParam2) =>
{
Console.WriteLine("The result of the C++ function is {0} and {1}", intParam1, intParam2);
};
All that’s left is to send our callback to the C++ function from our C# code.
1
TakesInCallbackAndDoesStuff(callback);