[Mono-docs-list] Example for using C code in C#
Raphael J. Schmid
raphael.schmid@gmx.de
13 Apr 2003 10:38:47 +0200
--=-0ghVkCJ37do/qf3R4fYR
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hello there,
since I needed to use some C functions from a library in C#
I played around to figure out how it works. Many thanks to
Miguel and Mike (who gave the significant hint) :-).
Anyway, since there doesn't seem to be such an example in the
Monkeyguide, I adopted my test app to mono/samples/embed/* and
commented it.
Maybe this can be useful for somebody.
Cheers, Raphael
Attached: gtkteste.c, gtktest.cs
--=-0ghVkCJ37do/qf3R4fYR
Content-Disposition: attachment; filename=gtktest.cs
Content-Type: text/plain; name=gtktest.cs; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
//
// Simple mono embedding example.
// Unlike test.cs/teste.c this uses
// C code in C#, not vice versa.
//
// Compile with:
// gcc --shared -o gtkteste.so gtkteste.c `pkg-config --cflags --libs gtk+-2.0` -lm
// mcs --unsafe gtktest.cs -r gtk-sharp
//
// Run with:
// cp gtkteste.so $mono_prefix/lib; ldconfig; mono gtktest.exe
//
//
using System;
using System.Runtime.InteropServices; // For DllImport().
using Gtk;
using GtkSharp;
class EmbedSample {
// "Import" the create_button() function from
// a library "gtkteste.so" which can be in any
// of the system's library directories. Please
// note the "unsafe" keyword. It is used because
// with pointers it is possible to manipulate
// memory locations directly. Normally this is
// not possible with C#.
[DllImport("gtkteste", SetLastError=true)]
static extern unsafe IntPtr create_button();
public static void Main()
{
Application.Init();
Window window = new Window("Embed Sample");
window.DeleteEvent += new DeleteEventHandler(On_window_DeleteEvent);
// Using Gtk.Button's IntPtr ctor to create a
// managed button from the native "handle" returned
// by the -formerly PInvoked- create_button().
Button button = new Button(create_button());
// From now on the button can be used as
// if it had been created the normal way.
button.Clicked += new EventHandler(On_button_Clicked);
window.Add(button);
window.ShowAll();
Application.Run();
}
private static void On_window_DeleteEvent(object obj, DeleteEventArgs args)
{
Application.Quit();
}
private static void On_button_Clicked(object obj, EventArgs args)
{
Application.Quit();
}
}
--=-0ghVkCJ37do/qf3R4fYR
Content-Disposition: attachment; filename=gtkteste.c
Content-Type: text/x-c; name=gtkteste.c; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
/***
* Simple mono embedding example.
* Unlike test.cs/teste.c this uses
* C code in C#, not vice versa.
*
* Compile with:
* gcc --shared -o gtkteste.so gtkteste.c `pkg-config --cflags --libs gtk+-2.0` -lm
* mcs --unsafe gtktest.cs -r gtk-sharp
*
* Run with:
* cp gtkteste.so $mono_prefix/lib; /sbin/ldconfig; mono gtktest.exe
***/
#include <gtk/gtk.h>
GtkWidget *create_button () {
/* A Gtk Button is created, just as if the
* container it will go in to was right here.
*/
GtkWidget* button = NULL;
button = gtk_button_new_with_label("Click to Quit");
/* Please note: what is returned here is a _pointer_.
* In the C# file we will be able to use it as IntPtr.
*/
return button;
}
--=-0ghVkCJ37do/qf3R4fYR--