[Mono-list] Using P/Invoke

Aaron Bockover aaron.lists@aaronbock.net
Fri, 08 Apr 2005 10:45:36 -0400


When trying your alternative base.Raw code, I get an error from mcs
stating that I can't change the access modifiers on base.Raw. So either
way get/set will work. I guess I should just stick with explicitly
setting base.Raw?

Using Mono 1.1.6. Maybe this was allowed in 1.0.x?

--Aaron

On Fri, 2005-04-08 at 10:27 -0400, Nigel Benns wrote:
> 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
> >
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
>