12

Is there a way for a Shared Object file written in C and built on Unix to be called from C# P/Invoke?

Or do I need to use Java or something like that?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Natasha Thapa
  • 979
  • 4
  • 20
  • 41
  • 2
    please provide more information... is the C# running on Mono/Linux or on Windows ? – Yahia Aug 16 '11 at 02:26
  • are you talking Mono on GNU/linux here? or trying to do so in Windows? – shelleybutterfly Aug 16 '11 at 02:26
  • Is the .so file yours or third party? If yours, consider porting to a Windows DLL; if third party, try to find a Windows build. – Seva Alekseyev Aug 16 '11 at 03:31
  • the .so file is my own, and i was planning to have it run on windows. – Natasha Thapa Aug 16 '11 at 04:27
  • 1
    Running a Windows .dll on Windows (via C# Interop/DllImport) is OK. Running a Linux .so on Linux is also OK: http://www.mono-project.com/Interop_with_Native_Libraries. But running a .dll on Linux or a Linux shared object on Windows will almost certainly fail. – paulsm4 Aug 16 '11 at 07:40
  • would Java be the best soluiton then for me? i am trying to wrap legacy code and expose them using web services. Host the Java services on the Unix box itself, under tomcat/ weblogic – Natasha Thapa Aug 16 '11 at 15:08

2 Answers2

14

Mono has the ability to integrate with native libraries from within C# built on top of dlopen(3). You just have to use the DllImport statement with the name of the library (i.e. 'libform.so.5'), then wrap the native code and data types with a friendly C# class that takes care of all the low-level stuff. This page has a good overview with lots of information on how to deal with marshaling pointers and other unsafe types.

Once you've got your wrapper class written, you can just use that without worrying about the fact that it's using a native shared library underneath.

James O'Doherty
  • 2,186
  • 13
  • 14
1

I would say at the least there's likely to be no easy way, especially if you mean C# on Windows. In that case you would need something that would be able to decode the shared object and get to the code in it, sort of a re-implementation of the ABI for GNU/linux. Also, any other libraries would have to be present and usable as well, such as the C runtime library and the like. This would likely be a very significant effort.

As for doing it directly under linux/Mono, see this answer: Calling UNIX and Linux shared object file .so from c# .

You could also try to see if what open office does, http://packages.debian.org/lenny/cli-uno-bridge could be helpful; but this is more of an interface rather than directly linking the two together.

Community
  • 1
  • 1
shelleybutterfly
  • 3,216
  • 15
  • 32
  • would Java be the best soluiton then for me? i am trying to wrap legacy code and expose them using web services and Host the Java services on the Unix box itself, under tomcat/ weblogic – Natasha Thapa Aug 16 '11 at 15:07
  • 1
    Java would not be a bad choice, but if you're trying to host it on Unix / Linux, C# would be a bad choice. It has nothing to do with the ability of the language, and a lot to do with the history of the tool set and the concept of "supported". Java was meant from day one to run on Unix / Linux, with working distributed, supported platforms. C# was meant to run on Unix / Linux, without working distributed, supported platforms. While a few people have managed to put together platforms for C#, they are explicitly advertised as "use at your own risk." – Edwin Buck Aug 16 '11 at 15:46
  • so you know: it looks like my answer was incorrect; James O'Doherty's answer http://stackoverflow.com/questions/7072961/calling-unix-and-linuxshared-object-file-so-from-c/7073354#7073354 has how to do it in linux with clopen(). I thought I remembered there being a way but then couldn't find it when i was searching and thought i must have imagined it. as for how to do what you want; I'm afraid I've not done anything like that. But I'm going to add to my answer saying it can be done (apparently easily) in linux/Mono. – shelleybutterfly Aug 16 '11 at 20:36