You can wrap a C library in a C# class and use it in any .Net application. This is arguable the best thing about C#. Here is an example of how to wrap a callback (function pointer).
Start by writing your C library#includeCompile that into a .so librarystatic const char*(*func)(void); void set_cb(const char*(*f)(void)) { func = f; } void call_cb() { printf("%s\n", (*func)()); }
gcc -shared -o t.so t.cWrite the C# wrapper
using System;
using System.Runtime.InteropServices;
public delegate string CallBack();
public class T {
[DllImport("t")]
public static extern void set_cb(CallBack x);
[DllImport("t")]
public static extern void call_cb();
public static void Main()
{
CallBack mC = new CallBack(T.Ashley);
set_cb(mC);
call_cb();
}
public static string Ashley()
{
System.Console.WriteLine("Ashley called.");
return "Ha, you wish";
}
}
Finally, compile and run that
mcs t.cs LD_LIBRARY_PATH=. mono t.exeYou should get the expected output
Ashley called. Ha, you wish