[Mono-list] Mono/.NET delegate incompatibility

J. Perkins jason@379.com
19 Dec 2002 14:42:12 -0500


Hang on a minute, I answered this too soon. I'm not talking about
calling a DLL function. [DllImport] works just fine on both Windows and
Linux. I am talking about a callback function, from unmanaged -> managed
code. That is, a C# delegate passed to and called from unmanaged code
via a function pointer. Here's a usenet thread on the subject:

 http://makeashorterlink.com/?C417214D2

The problem described in this thread is reversed for Mono: the callback
must use the cdecl convention. Because of this, there is no way to write
a C function that can call a delegate under both .NET and Mono. If I do
this:

  int SomeCFunction(int (__stdcall *callback)(int, int, int))
  {  return callback(1,2,3); }

...it will corrupt the stack when run under Mono on Windows. If I use
cdecl instead:

  int SomeCFunction(int (*callback)(int, int, int))
  { return callback(1,2,3); }

...it will corrupt the stack when run under .NET. Maybe I'm missing
something (quite possible), but it appears that Mono must use __stdcall
for delegates on Windows.

I'll give you guys a chance to sanity check my rambling. If it holds up
I will file a bug. 

Jason
379


> > I guess the default calling convention in mono is cdecl.  According to
> > MSDN, the default calling convention for dllimport should be stdcall
> > (which IMHO really only makes sense on windows).
> > 
> > You could try setting the CallingConvention property on the DllImport to
> > cdecl.