I’m trying to do some native callbacks to my C# app using this code:
C# :
public delegate void MyCallback(int i );
[DllImport("core.dll")]
public extern static void set(MyCallback cb);
public static void callmemaybe(int i)
{
Console.Write(i);
}
[DllImport("lgcoree.dll")]
public extern static void Post();
static void Main()
{
set(callmemaybe);
Post();
}
C++:
void (*callback)(int) ;
extern "C" __declspec(dllexport) void __cdecl set( void (*cb)(int) )
{
callback = cb;
}
extern "C" __declspec(dllexport) void __cdecl Post()
{
callback(1);
}
When I execute this code, right after the callmemaybe method is executed I get this error :
‘The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convection with a function pointer declared with a different calling convection’
But when I remove the parameters from the callmemaybe function and adjust the code (remove the parameters from the delegate and the exported functions) it works perfectly.
Post() and set() have __cdecl as their calling convetion, so you must probably specify it:
You might need to do it for the delegate aswell, depending on what Post() expects:
Hope this helps.