views:

952

answers:

4

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.

A: 

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

Konamiman
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.
Vladimir
A: 

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.

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.
Vladimir
+1  A: 

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.

Callum Rogers
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
Vladimir
Full AOT works at least for x86 and x86-64, as well as the ARM and PPC processors.
Gonzalo
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
Vladimir
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
A: 

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

leandrosa81
Does it parse assemblies or code? Sorry to ask but the project seems confusing to me.
Camilo Martin