Use .NET DLL on multiple platforms - c#

I am using C# to create a game using MonoGame which I wish to use on multiple platforms (which I know MonoGame can do).
Is there a way to create a .dll in C# and load it from other operating systems (preferably iOS, Android and MacOS) without recompiling the library? I am prepared to write a "loader" application for each platform, but would not like to rewrite the entire project.
I'm hoping there is a way to load functions from a .net dll (i.e. load the game) on these platforms without using paid products such as MonoTouch.

Basically the .NET/Mono Assemblies all get compiled to CIL Code which is an ECMA Standard.
However, you still have to program your game in a way that it do not depend on OS specifics.
Don't refer to certain .NET Assemblies (i.e. No WCF, cause it has only limited WCF support, no WPF at all, no WWF)
Don't refer to OS specific DLLs (i.e. no P/Invokes to kernel32.dll etc.
Don't use OS specific pathes or use preprocessor directives to make OS Specific code (yes, this will require recompile and you probably won't get around this easily)
Threading on Mono has some catches, so you will probably have to make platform specific code using preprocessor directives.
Honestly, I don't see a problem with having to recompile your code, if it's clearly written it's just a matter of adding a new project to your solution file and setting the preprocessor flags. Then all you have to do is compile the solution and have multiple DLLs in your bin folder. No one ever said you have to rewrite the complete project (unless the project is already finished and has any of the dependency mentioned up there), which in this case... it's your own fault for not having to think about it before starting development.
You'll just have to deploy your Apps (on iOS etc.) with the required Mono Runtime. And for this you will probably need something like Xamarin or wire up your own Mono runtime
Reference links:
http://www.mono-project.com/Compatibility
http://www.mono-project.com/MoMA

You can look at the Xamarin-Framework. But you always have to recompile your project.
Xamarin
on these platforms without using paid products such as MonoTouch.
This will be difficult.

Related

Xamarin System.Data.Linq inside PCL

I've got a PCL which includes System.Data.Linq(.Mappings) via the dll itself. This works with iOS, Windows and MacOS, but it does not with Android.
Temp: I will post the error message as soon as I am back at work, but I remember it was something like "Could not load file or assembly System.Data.Linq". It was not found
I know this namespace is not supported inside a PCL, but it is strange that it works (and yes, I made multiple projects with this) in every project type except Xamarin:Android.
Is there anything I could try like implementing the code myself, embed assemblies in native code, or something else?
Thanks for your help
Is there anything I could try like implementing the code myself, embed assemblies in native code, or something else?
I think no. As you already known, System.Data.Linq is unavailable in PCLs, and there is no lambda expressions in Android (or Java) yet. Also we need to respect the design of PCL, it is used to write and build managed assemblies that work on more than one .NET Framework platform. So if the method/dll or something else is not supported in the projects which uses this PCL, then this method/dll should not be placed in PCL.
Also, Xamarin.Forms is designed as a cross-platform UI toolkit that allows developers to easily create native user interface layouts that can be shared across Android, iOS, and Windows Phone. It is mainly target sharing UI. Of course you can also specify the platform when calling method/dll in its PCL, for example like this:
#if __MOBILE__
// Xamarin iOS or Android-specific code
#endif
For more info about specifying platform, you can refer to Dealing with Multiple Platforms.

Library compatibility between C# .NET vs C# Mono

I wanted to tryout C# for general purpose programming (not web development). I program in Windows environment, but I would like to avoid coding specifically for Windows (.NET), because I want to keep the option open for a future migration to Linux.
Are there any specific libraries in C# .NET that wouldn't work in C# Mono for general purpose programming work (not interested in Windows Forms, Silverlight and stuff like that) ?
Is there any internet link of things/features that provides a list that works on C# .NET wouldn't work on C# Mono or vice versa? I didn't readily find anything in google per se.
Note: I would be interested in specific answers, not opinions of which is better or worse (thanks!)
It is possible for a CLR assembly (even in the form of a DLL, as mentioned in the comments) to be read by Mono, as long as it does not have dependencies that do not exist in Mono, because...
...not every piece of code that compiles for .NET will compile for Mono, since there are lots of Windows-specific things in .NET (not strictly part of C#) that aren't implemented (WPF, ASP.NET async stack) or don't make sense at all in Linux (COM is one such example, I think).
Fortunately, there is a list of what .NET features are implemented in Mono. Even more fortunately, it seems they have an app that tells you a priori whether your code makes use of anything not implemented in Mono (but I have never tried it).
if you install xamarin (you need Pro or bigger so you have VS integration) you can create Portable Class Library that targets xamarin (which is based on mono) and visual studio will allow you to only use classes that are compatible with mono.
http://docs.xamarin.com/guides/cross-platform/application_fundamentals/pcl/introduction_to_portable_class_libraries/
apparently you can also use xamarin studio to create a PCL and there is a free version of that.

Managed dlls as embedded resources

Is it good practice to bundle all required managed dlls as embedded resources to a .NET library project in order to ship just one dll?
Background:
I have created an API as a .NET dll (in C#), and it's working all fine. But the library has quite some dependencies on other managed libraries (around 15 dlls) so I need to distribute those as well.
When the users of my API have created an application they again have to make sure to distribute all those dlls along with the application. To me it would seem better if they had just one dll to consider.
The main downside I can see to using embedded dlls is that they must be unpacked to a temporary folder before being loaded dynamically, which may or may not have performance and robustness issues.
There are a lot of questions around this. What happens if you're expecting to load a platform-specific dependency (i.e. x86 vs. x64), or that's true about the app consuming your API? Does that mean that you need to include specific x86 vs x64 assemblies in your package as well? It becomes hairy quickly.
You should consider using ClickOnce deployment for these types of scenarios. Then, all of the dependencies would be packaged together.
Realistically, it's a problem for the API consumer to solve, not for the API producer. Your API might be less popular if it has a lot of external dependencies, but you'll have to make decisions there about what's really crucial to your API's success.

Running .NET app and DLLs on an Android phone?

I've got a .NET app and some related third party DLLs, that I would like to run on an Android phone. The .NET app is written in C# and the DLLs are fully mananged, although they contain some unsafe code and work with DirectX using managed C++. Is it possible to retarget the application to Mono, and run it on the Android OS, considering that I only have a part of the application source code?
This is either impossible or very close to an Heracles's task.
Consider porting the existing code to Android and re-writing the missing bits.
The unsafe may be bit-twidlling in an OS specific way. I would use reflector to dump out the code and investigate what's it's doing and then see what mono makes of it.
Since it's using DirectX I'd suspect you're probably going to end up down a dead end.
I've looked at the Mono Libraries and Axiom 3D might be a useful abstraction to use if you need to re-engineer the Direct X calls.

Converting .NET App to x86 native code

There's a program written entirely in C# that targets .NET Framework 2.0.
Is there a way I could somehow compile (translate) managed EXE to a native one so it could be .NET-agnostic? I know there are probably commercial products for that purpose... but they are a bit expensive.
The problem is that we are to deploy the program on computers running Windows XP with no .NET Framework installed. There's also a requirement that the program's size must not exceed 500Kb (1Mb maximum) for it is downloaded from the web server (now the size is 255Kb). That is why there's no way we could attach a full-fledged .NET FX (or even a reduced one) to the downloaded program's file.
Obviously it is a terrible software engineering error that should have been detected and avoided earlier so we could use native technologies like C++ instead.
We have tried for now Novell's Mono - an open-source implementation of .NET Framework for Linux, MAC and Windows. Mono consists of C# Compiler, IDE, runtime (CLR) and Class Library assemblies (like System.dll and mscorlib.dll - much like .NET's class library assemblies installed to GAC).
What we tried to do is to locate CLR files and ship those along with our program's file and a few assemblies. This way the program can be invoked by running "mono program.exe" (command prompt) on a user's computer.
In addition to the inconvenience of such a use for the end user CLR files (mono.exe and mono.dll) turned out to be about 2.5 Mb in total that is much greater than the desired 500 Kb or even 1 Mb.
So, we have left with no other option but to translate our .NET App to a native one by a compiler, however the question remains - what compiler should we use and where could we find one...
For now I have stumbled upon a Singularity OS Project by Microsoft Research. It is an open-source research OS that is written in managed code (in part at least). The Singularity OS includes a Bartok compiler that the OS uses in order to translate a managed program to a native one (x86 32 bit). It should be noted that Bartok can't translate all the aspects of .NET 2.0 to a native code, but most of them. However I haven't yet learnt how to use the Singularity...
I would be really grateful to you if you could provide me with some useful tips and advice regarding the problem, your own experience with Singularity OS and Bartok Compiler or another approaches to the problem that I have overlooked and ways of solving it.
Thank you very much in advance!
Finally, using Mono's Full AOT feature (on Callum Rogers' advice) I've managed to produce a program.exe.dll that lacks a CLI header.
So it looks to me like a native dll. However I can't figure out how to convert that dll into exe or make it operational.
Also this dll doesn't seem to expose any functions of interest such as main function.
Check out AOT (Ahead Of Time) Compilation from the Mono project. This compiles your managed project into a native exe or an elf executable (depending on which system you target) that does not need the JIT. This is the technique used to get mono apps onto the iPhone (where the JIT/Framework are not allowed) and also has the added benefits of faster startup times, lower memory usage and it makes it harder for people to decompile your code. You said you were already using Mono, so it should be compatible.
Read up about it at the mono-project.com website and at Miguel de Icaza's blog (and iPhone info).
Note that you cannot use dynamic code or generic interfaces like
interface IFoo<T> {
...
void SomeMethod ();
}
And you will have to compile the DLLs of all the libraries you use.
PS: Make sure to use "Full" AOT for your problem.
2018 Update
At Build 2018, Microsoft announced .Net Core 3.0 roadmap that support Windows desktop applications (Winform & WPF)
2017 Update
For console apps, you can use .net core Self-contained deployments (SCD). Even for a hello world app, your package will 50MB+. You still need to install VC runtime though.
Update
As #jenix's comment, .NET Native is only for Windows Store Apps(UWP). After 3 years of it's announcement, this is still true, .net native for desktop may be dropped by microsoft . So this answer is not applicable anymore.
========
Microsoft Announced .NET Native Preview on Build 2014
With the .NET Native Developer Preview, apps will get deployed on end-user devices as fully self-contained natively compiled code, and will not have a dependency on the .NET Framework on the target device/machine. So, no .NET framework required on the target machine with .NET Native.
Announcing .NET Native Preview
Microsoft .NET Native
There is a project called CrossNet that parses .Net Assemblies and generates unmanaged C++ code, that can be compiled in any standard compiler.
Not really a solution for .NET to native conversion, but maybe this helps: http://www.yoda.arachsys.com/csharp/faq/#framework.required
Not quite sure that there is much you can do besides painstakingly rewrite the application. To ease the already burdening process, you could disassemble the .NET application using something like Reflector (into Microsoft C++), and use that as a base to start and just replace managed C++ references with native ones.

Categories

Resources