473,416 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to call API in Unix from C#?

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
Dec 15 '05 #1
15 5830

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
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


You can only call API's (using PInvoke) on the system where your application
is running, you can never 'call API's' across systems. All you can do is use
some form of inter-process/inter-systems communication using sockets.
Willy.
Dec 15 '05 #2
So I can call the unix API through C# socket implementation?

"Willy Denoyette [MVP]" wrote:

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
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


You can only call API's (using PInvoke) on the system where your application
is running, you can never 'call API's' across systems. All you can do is use
some form of inter-process/inter-systems communication using sockets.
Willy.

Dec 16 '05 #3
Is the "unix api" running on a different machine than your C#
application? In this case, you would use whatever protocol the remote
API requires (sockets/tcp/http).

Or do you mean the calling the Windows POSIX support DLLs or Windows
Services For Unix add-on functionality from a C# application? You should
be able to do this via PInvoke if you know the DLL and function names.

I am assuming your C# application is running on a Windows computer, and
not a Unix computer compiled with MONO.

Alpha wrote:
So I can call the unix API through C# socket implementation?

"Willy Denoyette [MVP]" wrote:

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AA**********************************@micros oft.com...
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


You can only call API's (using PInvoke) on the system where your application
is running, you can never 'call API's' across systems. All you can do is use
some form of inter-process/inter-systems communication using sockets.
Willy.

Dec 16 '05 #4
It depends on what you call an API, an API is a pure local interface, that
is, a program or program module (DLL's, shared libraries ..) calls functions
contained in another module through an API, but a program module cannot
directly call functions in a module that is not local to the calling module
(f.i. located on a remote system). When the calling and called modules are
both local, one can call another through an API, provided they conform to
the same API requirements. Managed code like C# can call a non managed API
through PInvoke (the runtime interop layer) provided the API is a C style
API.
However, if the called module is "remote", your only option to call it's
"functions" (API's), is by using a network based "remote procedure call"
protocol. You could develop your own protocol over tcp sockets, you could
use one of the (commercially) available RPC products like CORBA or you could
expose your API's as Webservices running on the remote system. But, again
all depends on what you are exactly looking for.

Willy.
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com...
So I can call the unix API through C# socket implementation?

"Willy Denoyette [MVP]" wrote:

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
>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


You can only call API's (using PInvoke) on the system where your
application
is running, you can never 'call API's' across systems. All you can do is
use
some form of inter-process/inter-systems communication using sockets.
Willy.

Dec 16 '05 #5
Hi Joshua, You're absolutely correct about the my C# application running on a
Window machine and the other Unix API is running on a Unix server. I need to
wirte some code for importing some information retrieved from the Unix by
placing some calls to the Unix server. What are those "Unix add-on
functionality from a C# application" that you're referring to? Can you
recommend any good reading on doing something like this? Are there certain
..net library calls that I can utilize for communicating with Unix?

Thanks,
Alpha

"Joshua Flanagan" wrote:
Is the "unix api" running on a different machine than your C#
application? In this case, you would use whatever protocol the remote
API requires (sockets/tcp/http).

Or do you mean the calling the Windows POSIX support DLLs or Windows
Services For Unix add-on functionality from a C# application? You should
be able to do this via PInvoke if you know the DLL and function names.

I am assuming your C# application is running on a Windows computer, and
not a Unix computer compiled with MONO.

Alpha wrote:
So I can call the unix API through C# socket implementation?

"Willy Denoyette [MVP]" wrote:

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:AA**********************************@micros oft.com...

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

You can only call API's (using PInvoke) on the system where your application
is running, you can never 'call API's' across systems. All you can do is use
some form of inter-process/inter-systems communication using sockets.
Willy.

Dec 16 '05 #6
>What are those "Unix add-on functionality from a C# application" that
you're referring to?

I was referring to the Windows Services For Unix add-on. It has nothing
to do with C#, but I was suggesting you might be able to call them from
C#. But you aren't trying to call Unix functionality on a Windows
machine, so that is probably not relevant.
Are there certain .net library calls that I can utilize for

communicating with Unix?

If you are just trying to make remote networking calls to a unix service
(or any remote network service), you will need to use the
System.Net.Sockets classes (TcpClient if possible, or Socket if you need
tighter control).
Dec 17 '05 #7
Thank you Joshua. I think what I need is System.Net.Sockets classes, as you
suggested. I will look into that.

Many thanks and happy holiday,
Alpha

"Joshua Flanagan" wrote:
>What are those "Unix add-on functionality from a C# application" that

you're referring to?

I was referring to the Windows Services For Unix add-on. It has nothing
to do with C#, but I was suggesting you might be able to call them from
C#. But you aren't trying to call Unix functionality on a Windows
machine, so that is probably not relevant.
>Are there certain .net library calls that I can utilize for

communicating with Unix?

If you are just trying to make remote networking calls to a unix service
(or any remote network service), you will need to use the
System.Net.Sockets classes (TcpClient if possible, or Socket if you need
tighter control).

Dec 19 '05 #8
In this application, the windows side would be the TCPServer, created under
the System.net.sockets. Will the Unix side be able to send me request? I
mean, is there a way for Unix to do the socket programming like what I'll be
doing on the Windows side?

Basicly, I need to exchange data with the Unix side of program. Sometimes,
requesting but most of the time answering and giving data from my side. I
just want to make user that this can be done.

Thanks,
Alpha

"Joshua Flanagan" wrote:
>What are those "Unix add-on functionality from a C# application" that

you're referring to?

I was referring to the Windows Services For Unix add-on. It has nothing
to do with C#, but I was suggesting you might be able to call them from
C#. But you aren't trying to call Unix functionality on a Windows
machine, so that is probably not relevant.
>Are there certain .net library calls that I can utilize for

communicating with Unix?

If you are just trying to make remote networking calls to a unix service
(or any remote network service), you will need to use the
System.Net.Sockets classes (TcpClient if possible, or Socket if you need
tighter control).

Dec 19 '05 #9
i have not had success using Services for UNIX to access a UNIX share
through C#.

Jan 11 '06 #10
Wanted to know if you were able to solve the problem ?, if so how cause I am
also looking for a similar solution

"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
Oct 14 '08 #11
On Tue, 14 Oct 2008 03:51:01 -0700
Andnas <An****@discussions.microsoft.comwrote:
"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_...tive_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.

Oct 14 '08 #12
Thanks Mike, I agree to this solution if the calling and called API's are in
similar environments/server. What if the "libc.so" library is on a UNIX
server...How do you call an api and get a return value if its on different
machine.
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_...tive_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.

Oct 15 '08 #13
Lets say, I have set of routines built and running on UNIX..say some C++
libraries. If we need to build a .NET GUI which needs to invoke these
libraries and execute some function in these libraries, which are sitting on
UNIX. One of the options is to expose the routines as a webservice...but
without that...is there some combination of Socket programming in .NET with
some interop programming....etc..

a simple example would be say...there is a unix function which is adding 2
integers and returning the sum...i need to build a UI in C# where these 2
integers can be passed, invoke the UNIX function and return the value
back...something like that.

I'd assumed by your question on Unix-like systems that you were _on_ a
Unix-like system.

If you're trying to call library routines on a Unix-like system from
Windows, then you'll have to do some more work. You need some form of
software on the server that will call make the API call on your behalf
and return the result to you.

What precisely are you trying to do? Perhaps I (or someone else here,
anyway) can give you a much better answer if you can provide some
functional details in an example that demonstrates what you're after.

--- Mike

--
My sigfile ran away and is on hiatus.

Oct 15 '08 #14
What you are looking for Unix data, you may want to look at Mono.Unix
namespace for your mono port...
On 10/14/2008 3:51 AM, Andnas wrote:
Wanted to know if you were able to solve the problem ?, if so how cause I am
also looking for a similar solution

"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

--
Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

.... Share your knowledge. It's a way to achieve immortality.
Oct 16 '08 #15
A webservice on the Unix system is probably the easiest route.

Alternatively, you could have a background process on the unix box running via
mono, with .Net Remoting (this limits the types you can safely send back and
forth).

Lastly, a plain/custom socket service could be used as well..

Without more specifics, its' hard to say... there is no "built in" PInvoke for
remote libraries (ala DOM/COM+)... you may want to look into various CORBA
libraries, as this may also provide the functionality you are looking for.

Me, I would probably go with the .Net remoting route, with Mono on the unix
server, and .Net on the windows side, with the client/server defined, and in a
common dll that gets deployed to both.

On 10/15/2008 3:32 AM, Andnas wrote:
Lets say, I have set of routines built and running on UNIX..say some C++
libraries. If we need to build a .NET GUI which needs to invoke these
libraries and execute some function in these libraries, which are sitting on
UNIX. One of the options is to expose the routines as a webservice...but
without that...is there some combination of Socket programming in .NET with
some interop programming....etc..

a simple example would be say...there is a unix function which is adding 2
integers and returning the sum...i need to build a UI in C# where these 2
integers can be passed, invoke the UNIX function and return the value
back...something like that.

>I'd assumed by your question on Unix-like systems that you were _on_ a
Unix-like system.

If you're trying to call library routines on a Unix-like system from
Windows, then you'll have to do some more work. You need some form of
software on the server that will call make the API call on your behalf
and return the result to you.

What precisely are you trying to do? Perhaps I (or someone else here,
anyway) can give you a much better answer if you can provide some
functional details in an example that demonstrates what you're after.

--- Mike

--
My sigfile ran away and is on hiatus.


--
Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

.... B5: You are not ready for immortaility.
Oct 16 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
2
by: Ryan Gaffuri | last post by:
Ill also need to return values. How do I do this? Im using GNU on solaris 2.8 with korn shell scripts?
1
by: Alan Wang | last post by:
Hi all: I am just wondering if it's possible to call Unix script from asp.net application. Thanks in advanced Alan
1
by: Alan Wang | last post by:
Hi all: I am just wondering if it's possible to call Unix script from VB.Net application. Thanks in advanced Alan
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
0
by: Aashif | last post by:
I want to call Unix Shell script which is available in other Server (Unix server) from windows application using C#. Currently the shell script runs the C program but the GUI is not good, So I want...
4
by: jane007 | last post by:
Hello everybody: I am having a problem. On Unix platform, there is a script that need user to input data from console, then when I call Unix perl script from Windows, these is an issue occurs,...
0
by: suganthibytes | last post by:
hi, please tell me how to call a unix script from C#
2
by: sixtyfootersdude | last post by:
Hey all! Just doing some tinkering with python. I am running mac os x and I am wondering how I can call unix commands from python. In C I think I would use a fork, although it has been...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.