[Mono-devel-list] Callling convention for interop call-backs

Liyu Liu liyul at hotmail.com
Tue Jan 20 11:44:56 EST 2004


There are two issues with interop:
1. should winapi be cdecl or stdcall?
2. Can mcs be extended to support specifying call conventions in delegate as
an extension to C#?

There are only two call conventions supported in .net, --cdecl or stdcall.
For Microsoft implementation, winapi is stdcall on win32 and cdecl on CE.
The fastcall is almost never used. Currently mono on win32 take default
winapi as cdecl which created compatibility problems. Any assembly
containing P/Invoke working with mono, like Mono.Data.SqliteClient etc,
cannot work with Microsoft .net runtime without source code change and
recompilation. Also same vice versa.

Microsoft's csc doesn't have support for cdecl for callback, thus people has
to be resorted to IL hack to change such

      .method public hidebysig virtual instance int32
              Invoke(native int o,
                     int32 argc,
                     int8** argv,
                     int8** colnames) runtime managed
      {
      } // end of method SqliteCallbackFunction::Invoke

   to
      .method public hidebysig virtual instance int32
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
              Invoke(native int o,
                     int32 argc,
                     int8** argv,
                     int8** colnames) runtime managed
      {
      } // end of method SqliteCallbackFunction::Invoke

This should be relatively easy to support in mcs and mono.

It is a breaking change. But seemed to us best way is to extend mcs to
support cdecl callback and change mono to use stdcall as default for winapi
on win32. This will force all P/Invoke done with mono to change to use cdecl
explicitly. But the benefit is final assemeblies working without any hitch
or changes across linux/win32. There will also be no need of IL hacks.

I submitted this as a bug as
http://bugzilla.ximian.com/show_bug.cgi?id=52834 and Zoltan seemed to have
fixed it. But we really want to learn a bit more thoughts and make sure it
works.

Luke


> Message: 1
> From: "Karl Waclawek" <karl at waclawek.net>
> To: <mono-devel-list at ximian.com>
> Date: Sat, 17 Jan 2004 18:19:51 -0500
> Subject: [Mono-devel-list] Callling convention for interop call-backs
>
> In the MS version of C# it is possible to define a calling
> convention for PInvoking functions in an unmanaged Dll, but
> there is now way to specify a calling convention for the
> delegates used as call-backs. Well, there is an assembly-hack,
> but no easy way.
>
> Is that the same for C# in Mono?
>
> Karl



More information about the Mono-devel-list mailing list