[Mono-list] Using P/Invoke
Aaron Bockover
aaron.lists@aaronbock.net
Fri, 08 Apr 2005 10:38:30 -0400
Regarding the DllImport, just using "libgimpwidget-2.0" did not work,
but it's not an issue. I hardcoded the path to just make sure it would
import properly. When I merge this into the rest of the project, I'll be
checking for the Gimp libraries in configure, and generate a runtime
config file for the assembly that has the library paths for the Gimp
library in it.
As for the base.Raw, I'm about to try your alternative.
Thanks for the insight!
--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
>
>