Wanted to know if you were able to solve the problem ?, if so how
cause I am also looking for a similar solution
APIs can be called via the DllImport attribute within a class. For
example (tested on Mono, on Ubuntu):
using System;
using System.Runtime.InteropServices;
public sealed class UnixFunctionCalls {
[DllImport("libc.so.6")] // UNIX uses .so.* not .dll
private static extern void exit(int status);
[DllImport("libc.so.6")]
private static extern int abs(int num);
public static int Main(string[] args) {
int x = -20;
int y = UnixFunctionCalls.abs(x);
Console.WriteLine("Original: {0}, New: {1}", x, y);
// Use native exit call instead of returning from main.
UnixFunctionCalls.exit(0);
// Also include a return statement since the compiler will
// otherwise complain (because we're supposed to return int).
return(0);
}
}
You can do this for any C library that exists in the system, but you
have to know the name of the C library and you have to be able to put
together a prototype for the function. I am not entirely clear, for
example, how one would use libc's printf() function from C# (not that I
know of any reason why anyone would *want* do to that). Running this
on my system yields:
Tuesday, 2008-Oct-14 at 12:20:20 - mbt@zest - Linux v2.6.27
Ubuntu Intrepid:[1-30/5223-0]:test> ./UnixFunctionCalls.exe
Original: -20, New: 20
Tuesday, 2008-Oct-14 at 12:20:22 - mbt@zest - Linux v2.6.27
Ubuntu Intrepid:[1-31/5224-0]:test>
This demonstrates that the call to libc's abs() function worked as
expected.
You'll want to read the Interop with Native Libraries page on the Mono
wiki, too, for quirks, details, and more, which is relevant for
working on UNIX-like systems with a CLR:
http://www.mono-project.com/Interop_with_Native_Libraries
My understanding is that you can do this on Windows for any DLL whose
name and entrypoint you know, as well. For example, you can call
UNIX-like APIs using the Cygwin DLL on a Windows system.
HTH,
Mike