I have a c library that has cdecl callbacks. How can I use these from c#.
Everything seems to say that they must be stdcall callbacks
to be clear:
delegate int del();
[dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int funcwithcallback(del foo);
where del must be called cdecl-wise
Take a look at this. The functionality has been around since 1.1 so it should cover whatever .NET version you are using. You just have to specify the CallingConvention.
CallingConvention Documenation at MSDN
You can also look at this article on Code Project:
Using the _CDECL calling convention in C#
EDIT: Also, Here is a example from FreeImage.NET.
Then on the C# side, simply:
It works due to some safeguard code added by the 2005 compiler.
EDIT: Don't try this if you can possibly make a shim in native code.