[Mono-list] PInvoke Discussions

Miguel de Icaza miguel@ximian.com
01 Aug 2001 13:40:01 -0400


> That, or to just have multiple versions of the C# code and to
> abstract it out at that level, but the general idea being to end up with the
> code itself needing to be ported between environments at some point (at
> which point my assumption would be that the C case should be a lot more
> easily portable, as the compiler can munge the types, autoconf can be used
> to swap things in as necessary, etc.).

You are on the general right track.  It is even simpler than that.

Basically the "wrapper" library always exposes an interface that is
mapped always to the primitive CLR types.  Ie, no attempt to
"understand" things like time_t, size_t, layout of structures is
required.

There is no need to write any "manual" code, we will generate these
"stubs" and structures from a script, so there wont really be any
"porting" required.  It is a compile time make target.

Here is an example of how this will work:

	struct runtime_stat {
		long st_ino;
		long st_time;
		char st_something_else
	}


	int runtime_stat (const char *file, struct runtime_stat *buf)

	int runtime_lseek (int fd, long pos, int whence)

Then the implementation of runtime_stat does:

int runtime_stat (const char *file, struct runtime_stat *buf)
{
	struct stat s;
	int code;

	code = stat (file, &s);

	if (code == -1)
		return -1;

	buf->st_ino = s.st_ino;
	buf->st_time = s.st_time;
	...

	return code;
}

int runtime_lseek (int fd, long pos, int whence)
{
	return lseek (fd, pos, whence); 
}