How does MonoRuntime lives side-by-side with ART? - c#

I've read that applications which are being developed using Mono are deployed with a MonoRuntime that works similar as ART, interprets the C# code, executes them etc. I assume Unity and Xamarin both does this.
What is strange for me is that theoretically I could write a runtime and ship it with my app? But that is supposed to be next to the runtime, below the application framework? How can the sandboxing let this happen?
I assume this might be the solution to my confusion, but I struggle understanding it.
What am I missing here?
Thanks in advance

First think about how NDK/JNI apps work on Android. They are launched from standard Android Java code run via ART/Dalvik and use JNI calls into C/C++ code. The process is still within the standard Android 'sandbox' but has additional lower level access to libraries provided by Android NDK headers. As that code is written in C/C++ it can be performant/cross system compatible.
So to answer:
But that is supposed to be next to the runtime, below the application framework?
I would argue no, at least you aren't really below the Android application framework. The MonoRuntime is started from Android and can use any of the 'Standard Libraries' which are exposed either via a JNI wrapper to the Android Java side or by a NDK header.
If you look at the below diagram you'll see 'Standard Libraries' which can be OpenGL ES which has a Android Java API and OpenSL ES which exposes more detail in NDK than the Android Java media api.
I could write a runtime and ship it with my app?
Yes you could, although you may want to define clearly what you mean by 'runtime'. If you are pre-compiled you can strip out unnecessary classes/modules which I believe Mono does.
How can the sandboxing let this happen?
You are still in the sandbox. Nothing prevents you from writing your own interpreter / Virtual Machine written in C/C++, interfacing the Android framework through standard JNI/NDK calls, and adding it to your own APK. Regardless you are still limited to what the Android framework/runtime allows for apps along with same process restrictions and limitations.

Related

WPF, UWP and Hardware

I just got back from Microsoft Build 2018 where they announced .NET Core 3 support for WPF applications. This is great because I can finally start using some of those cool fluent design things that are available for UWP. The only problem is, it's not getting released until next year.
I started looking into converting my app to a UWP app (because I'm impatient) but I'm running into some challenges. Mainly hardware. Talking with a few guys at Build, they made it seem like it was possible to write UWP apps that connect to low-level Win32 drivers for communicating with hardware (HID printers, card readers, bar code scanners, etc). I'm not having any luck finding information about this. Just to be clear, this is not an app I want to release to the Windows Store. This is a kiosk app that runs on our own hardware.
How does one access Win32 APIs for hardware integration in UWP?
Your best bet is likely to use P/Invoke, which is technology designed to allow .NET code to call unmanaged code. Assuming that your hardware is accessed through Win32 APIs in a DLL, this may be your best option.
The pinvoke.net Web site provides specific examples of the .NET code needed to call a specific Win32 API. Perhaps those examples can help you get started.
UWP app written in C++/CX has access to the Win32 and COM APIs that are part of the Universal Windows Platform (UWP).
The APIs in Win32 and COM APIs for UWP apps is only for C++, you can not use the C# to access it directly. Please see Visual C++ Language Reference (C++/CX) to get more details about C++/CX.
But, you can create a Windows Runtime component in C++/CX, then call it from C#, you can see the Walkthrough here:
https://learn.microsoft.com/en-us/windows/uwp/winrt-components/walkthrough-creating-a-basic-windows-runtime-component-in-cpp-and-calling-it-from-javascript-or-csharp

Xamarin : add .dll files at runtime to add functionnalities to application

I'm wondering if it's possible for a Xamarin application (all the platforms : Android, iOS, Windows Phone) to detect and load assemblies at runtime.
This is the kind of thing that I already did in WPF, and I'd like to use those modules (maybe with some modifications) in a Xamarin app.
I've spent yesterday looking for info online, and today I tried to implement a solution, but without success.
Prism seemed to be a way to do it, but it works only in WPF.
The restriction which makes this difficult is that I don't know at compile time any information about the assemblies. No names, no versions, no classes.
I can put the name and the version in the filename, and maybe find the classes by reflection. Using an interface as entry point (Prism use this), I should be able to do it.
Do you have any thoughts about this challenge ?
Thank you very much !
iOS: No
Apple does not allow code that was not bundled into the app at signing time to be executed, thus no on-the-fly Jit'ing of CIL allowed. The only exception to this is Javascript code that is run via their Nitro JavaScript Engine.
Android: Yes
Google allows just about anything you can think of, for better or worse ;-)
Windows Phone: No
All code must be signed as part of the Microsoft Store' App Ingestion process and thus you can not dynamically load assemblies later
Note: If this is a hard requirement for your app, you should look at hybrid mobile apps, like Cordova/Ionic, where you can perform a hot push to roll out new features, bugs fixes, etc... Besides self-hosting those hot pushes, various vendors support hosting of those, even Microsoft Azure has a full versioning publication system for this. Of course you would most likely be coding in JavaScript (or some trans-compiled variant).

Xamarin and c++ code

Currently I`m exploring how to write mobile apps for Android using Xamarin and C#. Also I have some c++ libraries(and source code for them) that should be used in application. These libraries are responsible for communication via internet(just to say).
So it is possible to use this c++ code in xamarin.android project? Or it should be rewritten in c# to be used?
Im not sure with networking but most C++ code can be used 100% in Xamarin.
If it works, you can't be sure if it works correctly in every situation on IOS.
We had the same problem with using UDP Sockets for VOIP(C#).
Most of the time it will work, but in case of VOIP you need to use iOS sockets so you can set them to a special mode so they won't be closed when the app gets paused.
So it depends on what you want to do and how the C++ code is written.
Im not sure if you can use normal unix sockets when using C++ on iOS but that should be the only problem when using it.
Just checked: Does Mac/IOS use the same sys/socket.h as the Linux kernel?
I think, if your library uses BSD sockets everything should be fine.
Usually it is possible to call some code in any other library unless
It exports the functionality
There is a wrapper in your language to make use of that exported functionality.
I do not know much about this Xamarin thing but I guess you should see if the library is binary compatible not code (i.e) that compiles with the source of your C++ project and you can make calls to it from your C# program.
Also while checking the Zamarin library documents, it seems that not every piece of C# code can be used on Xamarin platform. Please check the Xamarin community and the forums for more in details.

How does Xamarin for Android (Mono for Android) compare to native Android development?

I've recently chosen to learn C# and develop mobile apps using C# on Xamarin. Particularly for Windows Phone and Android development. I know that Windows Phone app development is mostly C#, and that Xamarin can compile native code for Android, but how does it compare to native development in Java? Will I be able to use EVERY or at least most (like 90%+) of the Android SDK and extensions using Xamarin?
The main thing which I think there is to consider is your preferred programming language - which in your case is C# - and using that. The only thing that is of concern to me when building an app in C# as oppose to Java is their is not as many 3rd party references to use as there is with Java. You will find yourself downloading example files and reverse engineering them in order to learn how to do new features, you will have to search intellisence far more to find what something is using if you can only find a Java based example of something. One thing which you will find though is that it usually can just be the same method as what is in Java but the first letter being capitalized. LINQ is a major benefit I find in data manipulation which is not available in Java.
Good luck with whatever you choose, James
I did not do any stats or study of Xamarin C# coverage of Android Java API, but in my work, everything that I needed when porting code from Java to C# was there. Also, Xamarin says that for new Android SDK releases they usually have C# counterparts released within 24 hours. And, as #dotToString remarked, you can add Java jar libraries to Xamarin C# project and make calls to them if necessary.
Performance is another matter - I don't believe it runs natively on Android. Rather, Mono runtime is somehow interpreting the byte code. I did some comparison of Java vs. Xamarin C# code performance on real life code, see more at:
Does anyone have benchmarks (code & results) comparing performance of Android apps written in Xamarin C# and Java?
Greg
you can use all of the android functionality and java library interop. also, you get the advantage of non windows specific .net including linq.

Can C++ and C# be used for applications for iPhone

Can I write applications for iPhone in C++ or C#?
Where can I find simulators for iPhone for testing my apps.
How to write them?
You need at least a small Objective C stub to hook into the system and deal with provided services (including getting input), but your program can be primarily in C++ if you would like. Apple seems to disallow C#; tools such as MonoTouch appear to be banned by the current developer agreement.
With a Macintosh, you go to Apple's developer website and download the tools and SDKs for free. They only run on the Mac.
You will want to start here for iPhone development:
http://gemma.apple.com/iphone/index.action
This is where you will get the iPhone SDK, simulator, and other essential tools.
You can use C, Obj-C, and C# (via MonoTouch).
C++ is a partial via Objective-C++. If you're serious about iPhone development though, I would suggest learning Objective-C.
Apple seems to reserve the right to kill the ability to run any application that wasn't writting in either C or Objective-C.
As far as I know, you can use C# for the iPhone using MonoTouch.
Another tool is unity, which is geared towards games and is based on the same codebase.
Both of these are proprietary and cost $$$.

Categories