[Mono-list] PInvoke Conventions
Tue, 24 Jul 2001 05:03:12 +1000
On 18-Jul-2001, Rhys Weatherley <email@example.com> 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
package Example is
function sin(C.double) returns C.double;
pragma Import(C, sin);
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 <firstname.lastname@example.org> | "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.