[Mono-list] DllImport and extern declarations for values

Miguel de Icaza miguel@ximian.com
30 Sep 2003 13:03:13 -0400


hello,

> The thing is, in C++ I also use a few macros, which I won't be able to use
> either, but I can probably manage that by duplicating it in C# (yuck,
> duplication). I do however need to reference som global constants defined in
> ruby.h like this:
> 
> typedef unsigned long VALUE;
> 
> EXTERN VALUE rb_mKernel;
> EXTERN VALUE rb_mComparable;
> EXTERN VALUE rb_mEnumerable;
> EXTERN VALUE rb_mPrecision;
> EXTERN VALUE rb_mErrno;
> EXTERN VALUE rb_mFileTest;
> EXTERN VALUE rb_mGC;
> EXTERN VALUE rb_mMath;
> EXTERN VALUE rb_mProcess;
> 
> How can I DllImport these? I tried:
> 
> [DllImport(@"c:/ruby180/bin/msvcrt-ruby18.dll")]
> public static extern /* VALUE */ uint rb_cModule;

Jonathan already pointed out that you can not import variables, so you
need a helper library in the middle:

bash$ cat binding.c
#include <ruby.h>
int ruby_get_mKernel ()
{
	return rb_mKernel;
}

int ruby_get_mErrno ()
{
	return rb_mErrno;
}

Compile:

	gcc -shared binding.c -o binding-helper.so

Bind:

	[DllImport ("binding-helper.so")]
	public static extern int rb_mKernel

Miguel