Re: How to call API in Unix from C#?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Tue, 14 Oct 2008 03:51:01 -0700
Andnas <Andnas@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
"Alpha" wrote:

I was told that Unix API can only be called using C++, ATL and
MFC. However, I was also told that C# can do that through Pinvoke
to a DLL that interfaces with the Unix API. Can someone direct me
to some good article to read up on this?

Basically, I'm looking a possible project that has a Window client
but needs to get information from Unix side sometime by calling
some API in Unix.

Many thanks in advance,
Alpha

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

--
My sigfile ran away and is on hiatus.

.



Relevant Pages

  • Re: RUNDLL32.EXE
    ... 0x66800000 0x155000 Windows Shell Common Dll ... 0x75FA0000 0xA000 BSD Socket API for Windows ... 0x794D0000 0x15000 Microsoft WinSock Extension APIs ...
    (comp.security.firewalls)
  • Re: is this MFC? + Book recommendations?
    ... These are all API functions, and have nothing to do with MFC. ... import Bool MessageBeep *** API ... import int RegisterWindowMessage *** API ... import DWord SetForegroundWindow *** API ...
    (microsoft.public.vc.mfc)
  • Re: ZoneAlarm -- Alert for "Run a DLL"
    ... I am blocking it from accessing the internet also... ... DLL file called SENSAPI.DLL. ... 0x60000000 0x5000 SENS Connectivity API DLL ... 0x794D0000 0x15000 Microsoft WinSock Extension APIs ...
    (comp.security.firewalls)
  • Re: API PrntDlg function
    ... You're supposed to also follow the instructions to register the DLL using ... more or less the same job without requiring you to use any API functions. ... anything to the chosen printer you still need to use the VB Printer Object ... printing methods in the normal way. ...
    (microsoft.public.vb.general.discussion)
  • Re: C is fixed or not ?
    ... The only part of the Unix ... API that I can spot that went into the C ... Language is paltry for communication on the quick. ... libraries of programming languages notoriously suck. ...
    (comp.lang.c)