[Mono-list] DllImport and extern declarations for values

Thomas Sondergaard thomas@thomassondergaard.com
Tue, 30 Sep 2003 11:07:17 +0200


I have written a ruby extension module in managed C++ that allows ruby to
interop with .net. I want this to work with mono on linux and since the
managed C++ is not portable I am rewriting as much as possible to C# using
P/Invoke. Ideally I would only have to write a small stub of a dynamic link
library that exports the init function called by ruby, which uses the Mono
embedding API and then I can do the rest from C#.

I have made a C# class called RubyAPI, where I have manually entered all the
ruby functions I use from the ruby api. It looks something like this:


namespace RubyDotNet.Common {
    public class RubyAPI {

        [DllImport(@"c:/ruby180/bin/msvcrt-ruby18.dll")]
        public static extern void ruby_init();

        [DllImport(@"c:/ruby180/bin/msvcrt-ruby18.dll")]
        public static extern void ruby_finalize();

        [DllImport(@"c:/ruby180/bin/msvcrt-ruby18.dll")]
        public static extern uint
rb_eval_string([MarshalAs(UnmanagedType.LPStr)] string s);

        ...
    }
}

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;

but that doesn't work.

Is there a smarter way to do this, where I don't have to manually duplicate
all this? Like a utility program that will generate a managed wrapper class
with static methods given a .h file.


What is the performance overhead of using P/Invoke instead of IJW (It Just
Works) in managed C++?

What is the state of Mono P/Invoke, DllImport, MarshalAs etc. What about
System.Reflection and System.Reflection.Emit?

Thanks,

Tom