I am having problem with understanding what FEATURE_PAL compiler directive means in .net 4.0 source code. It is used almost in every class that access unmanaged code.
PAL = Platform Adaptation Layer. It is first and foremost a detail of the CLR, insulating it from the operating system implementation. You'll indeed see it used in the Reference Source copy of the source code for the .NET 4 classes. It appears in any code that has a strong dependency on the underlying operating system implementation, bypassing such code since it cannot work on an operating system other than Windows.
You may be familiar with the use of the DEBUG and RELEASE directives to wrap blocks of code specific for testing/production code.
The .NET 4.0 framework uses the FEATURE_PAL compiler directive to execute code on newer platforms. It has to adapt to the platform by calling native methods found in native dlls. Hence the name of the feature: 'Platform adaptation layer'.
I came across this post after looking at the code for the Process class. One can image that killing a process works different on a given platform.
Think of the your code running on a newer version of Windows installed on a device that uses an ARM processor. When killing a process you need to call a method inside a native dll tailored to that platform.
So when compiling the framework for such platforms the FEATURE_PAL compiler directive is set as 'conditional compilation symbol'.
Related
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.
Is there any way to compile a .NET assembly into native code (i.e for x86) output, that is, without MSIL.
For what I understand if you just specify x86 as architecture this would only change the PE Header to point that out to the JIT, but still the assembly will contain MSIL and will be JITTed as needed.
NGen does produce assembly files for the specified architecture but it's not a compiler, it's designed to improve performance but you do still need the original DLL, the presence of the native image only serves to avoid JIT compiling the assembly but you just can't get that native image and use it, can you?
So, is there any way to actually compile from .NET to native machine code?
Spoon Studio (was named Xenocode before) seems to be able to do that: http://spoon.net/Studio/Features.aspx
RemoteSoft also have a product but the website looks quite old: http://www.remotesoft.com/linker/
You can do this using the new precompilation technology called .NET Native. Check it out here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative
Currently it is only available for Windows Store Apps. It performs single component linking. So .NET Framework libraries are statically linked into your app. Everything is compiled to native and IL assemblies are no longer deployed.
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.
Is it possible to force the C# compiler to pull all the referenced calls out of the framework and pack them into dlls or even a single executable?
I like writing quick 'one-off' applications with C#, however I don't want to have to install the whole framework on the target machine once it's ready to go.
You ask a loaded question. C# is merely a language and does not require the .NET Framework. The process of compiling it requires a compiler, which may or may not itself take a dependency on the .NET Framework (Microsoft's C# compiler does not -- it is written in native code). Your program will need to reference some assembly where types, classes, and methods can be found for your use. You can remove system.dll and mscorlib.dll from your references list and reference your own assemblies. So you can avoid dependencies on the .NET Framework if you really work at it. But in the end, unless you have a C# compiler that compiles programs to native code you still have a dependency on the CLR.
That's a very technical way of saying... almost nothing. But it answers your question. :) More practically useful however is how to get your C# programs to run with a minimum of dependencies. mkbundle from mono will actually let you compile it all into an .exe with virtually no dependencies.
But if you want to stick with the Microsoft .NET Framework, you can achieve a much lighter footprint and faster install of the dependencies you commonly need by using the Client profile of .NET 3.5 SP1. You can read about it here:
http://msdn.microsoft.com/en-us/library/cc656912.aspx
Look at mkbundle using Mono.
It is now possible to compile C# to native code using Microsoft .NET Native:
https://msdn.microsoft.com/en-us/library/dn584397(v=vs.110).aspx
It automatically compiles the release version of apps that are written in managed code (C# or Visual Basic) and that target the .NET Framework and Windows 10 to native code.
...
For users of your apps, .NET Native offers these advantages:
•Fast execution times
•Consistently speedy startup times
•Low deployment and update costs
•Optimized app memory usage
This only works with Visual Studio .NET 2015.
Take a look at the .NET client profile.
This will allow you to package a minimum install on the client machine.. which will later be updated by windows update to the full framework.
This depends, of course, on your app only using libraries that are contained in the client profile ...
Some info here: http://blogs.windowsclient.net/trickster92/archive/2008/05/21/introducing-the-net-framework-client-profile.aspx
It's said it is possible, using 3rd-party tools such as http://www.remotesoft.com/linker/
Not possible. Your "compiled" C# application is a language which the .Net CLR interprets (should have said JITed, Reads the IL, compiles to native code, and then invokes the compiled native code) at runtime.
FYI .net 2.0 is a standard install on xp SP2 and vista, so you won't be paying that much of a penalty.
You could look into mono, but this still involves running some kind of framework on your target machine.
This dependency which unfortunately frequently breaks or is missing in the real world is a big reason why C# has not had a wider adoption. On the flip side most development does have dependencies.. look at C++ & Java for example.
I don't think we will really get away from these dependency issues anytime soon, so I recommend that if you want to use C#, that you make a wrapper for installation which checks for the .net framework version dependency you need, and if missing notify the user that they need this to run your app.
Some C# features are bound to interfaces of the .NET framework.
For example:
yield return requires the IEnumerable interface
using (x) {} requires the IDisposable interface
I just want the ECMA language translated into native code with the fundamental runtime (garbage collector) etc. I'm not talking about .NET just the language specification of C#.
Using C# like any other native compiling language as a langauge alternative for Delphi, D or C++ because it offers generics, expanded types, garbage collection and many other nice features. As a langauge it's pretty nice.
But i don't like .NET nor do i find it very portable (.NET for PA-RISC's HP-UX anyone?).
So a compilation to C99 would be much, much better then compilation to native (that’s how it works very well for Eiffel).
The Mono framework has support for native compilation, which they call Ahead Of Time (AOT) compilation. More here: http://www.mono-project.com/AOT
Maybe the Salamander .NET Mini-Deployment tool may help you, it does convert the code to native and has an embedded runtime, so that you don't need the .NET framework for deployment.
Singularity RDK uses Bartok. Also the .Net Micro Framework has several native compilers (last I used the Micro Framework it did not have a native x86 compiler).
A third party option would be to use the Salamander .NET Linker
Microsoft's Native Image Generator (NGEN) is capable of converting .NET assemblies into native programs. However, it only means that the JIT compiler is being bypassed, and the .NET Framework is still required to be installed.
The Native Image Generator (Ngen.exe)
is a tool that improves the
performance of managed applications.
Ngen.exe creates native images, which
are files containing compiled
processor-specific machine code, and
installs them into the native image
cache on the local computer. The
runtime can use native images from the
cache instead of using the
just-in-time (JIT) compiler to compile
the original assembly.
Perhaps you could elaborate on the context in which you need native code for your .NET assemblies? Depending on the purpose, NGEN may be good enough, though a tool that actually eliminates dependencies on the .NET Framework may be what you want.