23

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.

Vlad
  • 239
  • 1
  • 2
  • 7
  • 3
    les dupe: http://stackoverflow.com/questions/45702/is-there-some-way-to-compile-a-net-application-to-native-code – Alastair Pitts Nov 22 '09 at 14:47
  • Are you trying to make this cross platform, or just remove the dependencies on framework libraries? – Rowland Shaw Nov 22 '09 at 14:50
  • I'm just trying to remove the dependencies of .NET Framework so it could be ran on Windows XP w/o .NET FX installed. – Vlad Nov 22 '09 at 14:52
  • 3
    another duplicate: http://stackoverflow.com/questions/1188006/turning-net-executable-into-native-executable – Mauricio Scheffer Nov 22 '09 at 15:00
  • 1
    That's a very good question to think of, I can even add that Windows 7 is shipped with .NET 3.5 SP1 installed. Sadly, but sometimes there are cases when legacy OS are found on target computers with legacy (at best) or no (at worst) .NET installed. – Vlad Nov 22 '09 at 16:50

5 Answers5

10

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.

Community
  • 1
  • 1
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
  • Thank your for your answer. It seems I have earlier tried compiling a sample Hello World assembly this way, but it failed. Possibly because it [Mono's Full AOT] isn't compatible with every processor (I've got Intel Core 2 Duo). However, I shall examine Full AOT thoroughly as chances are I did something wrong – Vlad Nov 22 '09 at 21:55
  • Full AOT works at least for x86 and x86-64, as well as the ARM and PPC processors. – Gonzalo Nov 23 '09 at 14:18
  • Finally I've managed to produce a program.exe.dll that lacks a CLI header. So it looks 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 – Vlad Nov 28 '09 at 15:15
  • 1
    Even when using Full AOT, the used portions of Mono would be compiled into the .exe, bloating it FAR beyond 500k or 1 Mb. So actually this will run without .NET, but it won't meet the size restrictions given. I fear there is no way than to re-write or force installation of .NET Framework on the target computers. – Sebastian P.R. Gingter Nov 28 '09 at 15:33
  • Has anyone had actually any success in doing Full AOT on Windows? It works on Linux on x86, but seemingly is not supported on Windows x86. – Camilo Martin Mar 16 '13 at 00:32
9

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

prime23
  • 3,362
  • 2
  • 36
  • 52
3

There is a project called CrossNet that parses .Net Assemblies and generates unmanaged C++ code, that can be compiled in any standard compiler.

leandrosa81
  • 138
  • 8
1

Not really a solution for .NET to native conversion, but maybe this helps: http://www.yoda.arachsys.com/csharp/faq/#framework.required

Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • Thank you. I will pay attention to the programs mentioned in the list. However, as far as I know these programs are not very cheap, yet there can be a significant payload, namely the size of our executable will exceed the desired figure. – Vlad Nov 22 '09 at 21:13
  • the link appears to be dead. – Brian Reichle Nov 24 '15 at 20:35
0

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.

blparker
  • 343
  • 4
  • 12
  • 1
    I hope there will be found a way to avoid rewriting the entire app in C++ as it will take quite a lot of time and most importantly debugging time. – Vlad Nov 22 '09 at 21:25