[Mono-list] Using P/Invoke

Nigel Benns nigel_benns@rogers.com
Fri, 8 Apr 2005 10:27:05 -0400 (EDT)


It looks pretty good to me :)

I wouldn't use "/usr/lib/libgimpwidgets-2.0" in the DLLImport field though.
Use either "libgimpwidgets-2.0" or "gimpwidgets" may work as well.  It
will make it more portable, so that it works on windows.

I don't think I would use base.Raw either, but you might want to ask
someone else on this.  When doing EFL-Sharp, I've been overriding Raw for
the class, and having it setup an IntPtr within the class.  That way I can
control that its only a "get" operation (I can't see a reason to set it),
and I can use a HandleRef instead to make sure nothing inadvertantly gets
GC'ed before the class is finalized.

public class GimpColorButton : Button
{
        [DllImport("libgimpwidgets-2.0")]
        static extern IntPtr gimp_color_button_new();

        HandleRef realWidget;

        public GimpColorButton() : base(IntPtr.Zero)
        {
                realWidget = new HandleRef(this, gimp_color_button_new());
        }

        public override IntPtr Raw
        {

           get {

              return this.realWidget.Handle;

           }

        }

        ~GimpColorButton()
        {
                Dispose();
        }
}


> Hello, one area I haven't had much practice in with Mono is P/Invoke. I
> am trying to import a function from libgimp, so I can use a custom GTK
> widget in the library.
>
> Here is what I've tried:
>
> using System;
> using System.Runtime.InteropServices;
> using Gtk;
> using GLib;
>
> public class GimpColorButton : Button
> {
>         [DllImport("/usr/lib/libgimpwidgets-2.0")]
>         static extern IntPtr gimp_color_button_new();
>
>         public GimpColorButton() : base(IntPtr.Zero)
>         {
>                 base.Raw = gimp_color_button_new();
>         }
>
>         ~GimpColorButton()
>         {
>                 Dispose();
>         }
> }
>
> ...
>
> Widget colorButton = new GimpColorButton();
>
> As for the actual Gimp Library, I think maybe multiple libraries have to
> be loaded (libgimpcolor, libgimpui, etc.) So maybe I should write a
> wrapper C library that links in all the Gimp libraries, and then import
> that library into the C#? Regardless of the C library in question, is
> the C# above on the right track?
>
> Thanks!
>
> --Aaron Bockover
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>