[Mono-list] PInvoke Conventions

Miguel de Icaza miguel@ximian.com
19 Jul 2001 14:03:22 -0400


Hey Rhys,

> 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.

I read the article, and I believe that the problem is even more
complex than it looks like at first examination.

The particular case you show (lseek) as well as other POSIX APIs does
not use int or longs.  They use things like `off_t', `size_t', `struct
stat' which are part of the specification.

Notice that your proposal does not address the more serious issue with
structures and structure layout.  For example, wrapping `stat' is not
going to be trivial, as most UNIX systems will have different sizes
and *layouts* for the fields in the stat structure.  

Your proposal does not solve the problem of structures, and I am
afraid that adding even more attributes is not going to help out. 

There are a couple of solutions to the problem:

	* Get the ECMA specification to specifically list some core
	  types (off_t, size_t) and map those to NATIVE_POSIX_XXX.

	  This addresses the size issue, but does not address the
	  structure layout issue. 

	* Have the compiler produce out-of-band data to "annotate" the
          real types automatically, ie:

	  [DllImport("libc")] lseek (int fd, off_t off, int pos)

	  off_t would be a ValueType annotated with [System.POSIX]

	* Have a new PInvoke invocation that does not require extra C#
          types:

		[DllImport("libc"),PosixProto(int,off_t,int)]
		int lseek (int, long, int)

		[DllImport("libc"),PosixProto(string,struct.stat *)]
		int stat (char *, struct stat *)

Have the CLI runtime or the autoconf process figure the proper sizes
of the various POSIX type definitions at compile time.  It should also
figure out the layout of public structure fields for POSIX structures.

Miguel.