Loading a linux dynamic library (.so) in C# on Windows - c#

I am using swig to generate a C# wrapper to my code. But then I also need to link the dynamic library, which is a Linux .so file. Is it possible to do this from C#?
Thanks
Edit: using a .so file on Windows.

There is no direct way to do this. You need to create a native Windows version of all your native code (the so itself, plus all dependencies).
Other options include writing all your code on Linux, if possible, including the C# portion (using Mono). Or creating some sort of client/server setup, where your native code remains on Linux (as a server application) and your C# code goes on the Windows-client. If that is a viable solution for you is hard to tell without further details.

This Post seems to have some answers for Linux, are you trying to do this in Windows?

It's more than likely that your shared library will have been compiled to use linux command calls.
The only possible way to use linux .so files in a "windows" app would be to compile it (your app) using cygwin, but that way would make your windows app a posix app which means it would have to fire cygwin up everytime it runs.

Related

How to build AIR Native Extension for Windows with C#?

I recently developed native extensions for mobile projects on Adobe AIR (Android and iOS).
I want to port these ANEs for desktop projects on Windows and OSX. The OSX part is not a problem because it uses the same mechanisms as for iOS. The problem is essentially on the Windows side.
Adobe AIR offers bridges to write the native part in C / C ++, I prefer to use C# to simplify the task and access more simply .NET libraries.
Has anyone ever heard of experiments or viable projects to code a native Adobe AIR extension with C#?
I have not found a complete solution to achieve this:
Create a DLL in C # including access to FlashRuntimeExtensions.h (C Header file)
Be able to use .NET libraries from this unique DLL
Produce only one DLL file
Do not use the flash.desktop.NativeProcess library
Thank you for your help or a different point of view on these issues.
According to Extending AIR, you just need access to any function on your DLL regardless what native method you used to create it.
This means that you still need to use a C++ project to link AIR to your native library but the main code can be done on C#. So you export your main code/logic into a DLL from C# and then use the C++ bridge project to Link both DLL and flash.
This link could also be useful : Windows ANE - tutorial introduction
A developer sent me this link to TUARUA's FreSharp GitHub page.
It corresponds exactly to the subject of my question. I share it so that everyone can see how to create an ANE (AIR Native Extension) from C# under Windows.
So I will be able to resume my development and port my libraries for desktop computers.
Thanks to everyone.

A very complicated workaround for using a C# dll with an Android app? (xamarin)

I have an proprietary Android app (created with Android Studio) and I need to add some functionality which requires to use a C# DLL.
The app is connecting wirelessly to a development board that does some sensor measurements. Normally on windows apps the dll is used to do some complex calculations of calibration coefficients for the measuring.
My problem is:
I don't have the the source code of the DLL and most likely I won't be approved to access it.
I've read that you can use xamarin to make android apps and use C# dlls but I can't rewrite the whole app for xamarin just to use the dll (also I've never used xamarin)
So far I had the idea to build a simple http server with C# that gets parameters with a GET request, runs them through the dll and returns the results. However it's not an ideal solution because it requires a separate pc to run the server and the adndroid device to have a network connection to it.
My second idea is to build a separate app with xamarin that uses the dll and make my main app start it just to calculate the coefficients and get the results. Perhaps make the second app not visible in the launcher and somehow distribute the two apps together.
Since I am not experienced with android development and especially xamarin and I don't have a lot of time to waste on this project I want to know if my idea is feasible or if there is a better alternative.
Can you suggest some useful tutorials for xamarin and using dlls with it?
===============================================
Edit
I have managed to get the source code of the dll project which is in C++ (and full of windows only stuff) and now I'm trying to use NDK to compile it and JNI with Android studio to use the native functions. Unfortunately almost every step has it's own quirks and problems and it's far from smooth :/ I wouldn't recommend it to beginners like me!
I know this answer is late, but I'm in a similar situation right now.
This project here helped me setup a native Android Studio build with an embedded C# lib:
https://github.com/royd/KotlinAppWithXamarinDependency
The only issue I'm facing right now, that I can't get the R8 obfuscation/optimization to work without breaking the app.

Consuming .so files of android in c#

I have a .so files which I was using it in java code which compiled to be used on an Android machines, now I forced to use some of the functionalists that are defined in the .so files, in a .NET c# application.
How can I reference or consume .so files in the c# program?
I don't think this is possible.
.so is a shared object, which is machinecode (Android Native Interface) for the phone / emulator.
I don't know C# very good, but I don't think there is any interface with which you could access the members of the .so.
Beside of this is .so the shared objects format for linux, and .NET runs mostly on windows.
Also, the .so is most likely compiled for ARM processors on phones, so it would need an emulator to run t on windows.
You see, there is a huge gap between these technologies.
If you want to use this on a PC, no, you can't use the file directly. You need to get the source and recompile to a DLL which you can use from .NET via Platform Invoke (IIRC the name). What you have is most probably ARM code, and you will need x86 for a PC.
Monodroid might work an Android device, since it most likely just compiles to Android code. I've never used it though, so don't quote me on this.

Porting C# code to run on OSX

We have a console app that runs in .NET 3.5. It connects to a USB device and spits out data received from it.
I'd like to port this over to OSX and have some questions about the strategy to do this. The USB Driver is already installed on OSX.
Mono looks promising but I don't get it. Does the end user have to install Mono or run something?
After looking at some other posts it seems that you can write a bash script and do some hacking to get the program installed...but I can't find a really good explanation of this. it also seems that mono wouldn't be installed in this process. These posts were also from '09 so I'm wondering if something has changed to make this easier.
My question is, what's a good way to approach running/porting a C# console app to OSX.
.NET is compiled down to IDL (bytecode) on all platforms including the windows platform. It is then run on the CLR (common language runtime iirc) which is a similar concept to the Java Virtual Machine. It just so happens that on most Windows machines that this CLR is installed by default. So if you wish your application to run on another platform you first need a CLR for that platform. Mono does include a CLR which can run on OSX. So either you (as part of your package/ bundle ) or the user would need to install this before your .NET will run.
The other issue you have is that .NET also contains certain API's which are not part of the ECMA standard which your application may or may not use. Some of these API's are present in mono, some of them are not. Those that are not usually have an equivalent or similar API which you can use to achieve the same thing however you may need to alter your application to deal with that scenario.
It really depends on what .NET api's your application is using as to how difficult it will be to port. I am guessing you are probably using winforms as part of your application so here is the guide from the mono site for that portion of the API
http://www.mono-project.com/Guide:_Porting_Winforms_Applications
You can bundle the Mono installer with your application (or your own build of Mono). You might even have the option of statically linking the mono runtime into your application on the Mac, I can't remember if it's supported (yet) or not. I'm pretty sure you can.
Also, wrt gordatron's warnings, if you use Path.Combine() to create paths instead of hard-coding \ as your path separator (which you should be doing anyway), you won't have to worry about having file system path problems.
For a nice easy way to check if your app uses any features which aren't available in Mono, you can use http://www.mono-project.com/MoMA to check for unsupported methods/classes and any other potentially non-cross-platform blockers like P/Invokes.
For a console application, I don't see you running into many issues (although you'll likely need to interface with the USB hardware differently than you do in Windows).

Cross platform development, GUI and Database - c#

Forgive me for asking a common question, but I couldn't quite get what I needed from what I found so far.
First question - SQLite. I am using this DB as in my C#.NET 3.5 windows service and it works great, I was looking for a portable solution, because I would like port my service to a linux daemon etc, using Mono, which seems to support it. However, I am not sure how to implement this. I had thought the dll was compatable, but it seems there is a seperate dll for Mono/.NET which I should have expected. Does this mean I need to seperately code/compile for each platform or is there something that would allow me use of SQLite with the same code on various platforms? I have encountered this a few times in my searches, csharp sqlite, a "reimplementation". To be honest, this is quite new to me, is it of use? The Mono SQlite page says that they Mono.Data.Sqlite code is based on System.Data.SQlite and goes on to say...
"We have chosen this way as means to
provide a migration path for
developers using SQLite in their .NET
applications"
Are they referring to creating a new, seperate binary? Or could I run my code as is with some adaptation?
Second question - GUI. As far as I can tell the two main options for cross platform dev in .NET would be GTK# and Winforms. Again however, its the specifics of implementation that are a bit hazey. Can I create a win forms GUI in visual studio as I normally would then easily migrate this using mono? Or should I develop this in something like X-Develop or MonoDevelop?
Many thanks for your advice/patience :D
To help out a little bit here I used the full mysql dll file that was provided and I was able to compile my program on windows using visual studio, and then deploy it to Linux without changing anything(except the case of the mysql dll file, which is kind of annoying you'll see what I mean at some point).
Also from what I've heard winforms isn't totally supported by mono yet, but I may be wrong. I haven't done a gui cross platform yet, but I would test winforms first, just so you could develop it in visual studio, and if that doesn't work I think GTK should be fine.
You just need to make sure that the dll's are compiled for .net 3.5 or below and you should be fine(90% of the time).
Check this out here, http://www.mono-project.com/MoMA and also the link that chris provided.

Categories