I have been experimenting with C++/CLI delegates (as I am trying to make a .NET reference library), and I have been having the following problem.
I define a delegate in
void WriteMessage(void* Function, int number)
Passing function pointers as void* is a pretty bad idea. It prevents the compiler from checking you are doing anything wrong. There is something wrong, although the compiler cannot detect it in this specific case. The delegate is marshaled as a function pointer that uses the __stdcall calling convention, your actual function pointer uses the __cdecl calling convention, the default for native code. Which causes the stack to get imbalanced on the call.
You can fix it by applying the [UnmanagedFunctionPointer] attribute to the delegate declaration, specify CallingConvention::Cdecl.