[Mono-list] PInvoke Conventions

Fergus Henderson fjh@cs.mu.OZ.AU
Tue, 24 Jul 2001 05:03:12 +1000


On 18-Jul-2001, Rhys Weatherley <rweather@zip.com.au> wrote:
> I've been investigating PInvoke support for Portable.NET,
> and I've come across some icky issues that will probably
> affect Mono's VM as well.
> 
> In particular, because [C] "int" and "long" have different types
> on different CPU's, it is difficult to write PInvoke declarations
> in C# that will work with all Unix libc's.  The result would
> be libraries and applications that need to be compiled
> separately for 32-bit and 64-bit systems.
> 
> To avoid duplication of effort, I propose we come up with a
> common way of solving these problems.

The Ada 95 standard has a solution to these problems.
They define a standard package Interfaces.C
that contains types Interfaces.C.int, Interfaces.C.long,
Interfaces.C.double, Interfaces.C.size_t, etc.
This package is implemented differently for different platforms.

When you want to interface to a C routine from Ada,
you give an Ada declaration for it using the Ada names
for the C types.  For example, to import the C function sin()
you could write

	with Interfaces.C;
	using Interfaces;
	package Example is
	  function sin(C.double) returns C.double;
	private
	  pragma Import(C, sin);
	end Example;

or something like that (my Ada is a little rusty, so I'm not sure
about the exact syntax, but anyway the syntax is not important).

There are various ways to convert from the Interfaces.C types to
similar native-Ada types and vice versa.  In other words, the
programmer has control over the marshalling, and can decide
how to handle overflow, etc.

Is there any reason why the same approach wouldn't work for .NET too?

What advantage is there to inventing special custom modifiers for
marshalling, rather than using the approach described above?

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.