Consuming .so files of android in c# - 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.

Related

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.

Compile C# on a (not for) Windows 8 ARM Tablet

Would it be possible to code and compile C#, on a Windows 8 Tablet (WinRT) (the ARM processor edition)?
Basically it comes down to this:
Is there a C# compiler that runs on ARM?
Is there and IDE that can run in WinRT?
If the above is true, I don't see any issue, but I currently can't find if the C# compiler runs on ARM (only a lot of posts about compiling for ARM). I've also looked at SharpDevelop, and found that their source code compiles for "Any CPU", which according to this post: Windows RT and c#, means that it'll run on ARM.
Sharpdevelop however requires .NET 4.0 "Full" runtime, which I couldn't find if WinRT has or not. I'm betting it doesn't, as WinRT is supposed to be a really lightweight edition of Windows.
As a sidenote, I know that Windows tablets will come in two editions, one for ARM and one for classic processors. The classic processors will run a normal Windows 8 edition, which means it can run all the native applications. Compiling C# wouldn't be an issue here - so the question is rather, can I do the same on ARM?
This would be awesome for travelling and trying out new ideas quickly.
Currently: no, and no.
It would certainly be possible to build a Windows Store app that contains an IDE and a C# compiler. However, you would not be able to run any programs built using such an app. Windows Store apps run with reduced privileges in a sandbox. In this sandbox, the CLR will only load system assemblies and assemblies contained in the app package. The app package is immutable and cannot be modified at runtime.
Actually, the .Net framework on the surface includes csc.exe, the Csharp compiler.
I've gotten code to compile, but WinRt doesn't seem to like it being run without proper signing.
The IDE won't happen for a while, not yet at least. I'm sure that with proper signing, it is possible to run a compiled executable.
Or the other guy might be right and it isn't just an issue of signing.
I've ported SharpDevelop to run on unlocked Windows RT devices, it works at least for C# Windows Forms apps. http://chentiangemalc.wordpress.com/2013/03/18/sharpdevelop-rt-edition-beta-code-windows-forms-directly-on-windows-rt/

WinRT and DLLs: What exactly am I allowed to use?

It's not all together clear to me, so I am hoping someone here knows the answer. I am using the C# Json.Net and the C# HtmlAgilityPack both built from sources for "Any CPU". I've noticed that both of these include using directives that reference assemblies that don't seem to be available in Metro yet they both build fine.
If I reference them, does this mean my app will thunk to unmanaged code?
What exactly causes thunking to unmanged code?
Why are DLLs allowed to reference assemblies that are not available to WinRT and my app is not?
Will this fail the windows store approval process?
What are the general rules for DLLs I include with my app that I need to worry about?
Thanks in advance!
When developing apps that target WinRT, the app should only use the API's available in Windows 8 WinRT. The API's are defined in WinMD files. If the app used the desktop API's, then it will be rejected during the Windows store approval process.
Imagine this scenaio. You developed a WinRT app that uses .NET DLL's from desktop mode (client profile). The app is deployed in Windows store and user downloads it on ARM tablet. The application will crash as it does not have the desktop (client) .NET DLL's on the ARM devices.
so this brings us to the question on what .NET classes we can use while developing apps for WinRT. The article http://kishore1021.wordpress.com/2012/08/06/what-is-portable-class-library-project-in-visual-studio-2012-net-4-5/ lists the .NET classes available for WinRT application development.
Change all open source projects dependencies to target WinRT. See if some methods / classes in WinRT are missing so the code does not compile. Try to find the alternative methods/classes.
Such as List.FindAll(), change to Linq Where()

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

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.

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).

Categories