问题描述
有一个完全用 C# 编写的针对 .NET Framework 2.0 的程序.有没有一种方法可以以某种方式将托管的 EXE 编译(翻译)为本机的,这样它就可以与 .NET 无关?我知道可能有用于此目的的商业产品......但它们有点贵.
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.
问题是我们要在没有安装 .NET Framework 的运行 Windows XP 的计算机上部署程序.还要求程序的大小不得超过 500Kb(最大 1Mb),因为它是从 Web 服务器下载的(现在大小为 255Kb).这就是为什么我们无法将成熟的 .NET FX(甚至是简化版)附加到下载程序的文件中.
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.
显然,这是一个可怕的软件工程错误,应该早点发现并避免,因此我们可以改用 C++ 等原生技术.
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.
我们目前已经尝试过 Novell 的 Mono - 一种适用于 Linux、MAC 和 Windows 的 .NET Framework 的开源实现.Mono 由 C# 编译器、IDE、运行时 (CLR) 和类库程序集(如 System.dll 和 mscorlib.dll - 很像安装到 GAC 的 .NET 类库程序集)组成.我们试图做的是找到 CLR 文件并将它们与我们的程序文件和一些程序集一起发送.这样,可以通过在用户计算机上运行mono program.exe"(命令提示符)来调用该程序.除了对最终用户 CLR 文件(mono.exe 和 mono.dll)的使用带来的不便之外,最终用户的 CLR 文件(mono.exe 和 mono.dll)总共约为 2.5 Mb,远大于所需的 500 Kb 甚至 1 Mb.
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.
因此,我们别无选择,只能通过编译器将 .NET 应用程序转换为本机应用程序,但问题仍然存在 - 我们应该使用什么编译器以及在哪里可以找到...
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...
目前,我偶然发现了 Microsoft Research 的 Singularity OS 项目.它是一个开源研究操作系统,用托管代码(至少部分)编写.Singularity OS 包括一个 Bartok 编译器,操作系统使用该编译器将托管程序转换为本机程序(x86 32 位).应该注意的是,Bartok 不能将 .NET 2.0 的所有方面都翻译成本机代码,但其中大部分都是.但是我还没有学会如何使用奇点...
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...
如果您能为我提供一些关于该问题的有用提示和建议、您自己使用 Singularity OS 和 Bartok 编译器的经验或解决我忽略的问题的其他方法以及解决方法,我将非常感谢您.
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.
提前非常感谢您!
最后,使用 Mono 的完整 AOT 功能(根据 Callum Rogers 的建议),我设法生成了一个缺少 CLI 标头的 program.exe.dll.所以在我看来它就像一个原生 dll.但是我不知道如何将该 dll 转换为 exe 或使其运行.此外,这个 dll 似乎没有公开任何感兴趣的函数,例如 main 函数.
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.
推荐答案
查看 Mono 项目中的 AOT(Ahead Of Time)编译.这会将您的托管项目编译为本机 exe 或 elf 可执行文件(取决于您的目标系统),不需要 JIT.这是用于将单声道应用程序放到 iPhone 上的技术(不允许使用 JIT/框架),并且还具有更快的启动时间、更低的内存使用量以及使人们更难反编译您的代码的额外好处.你说你已经在使用 Mono,所以它应该是兼容的.
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.
在 mono-project.com 网站和 在 Miguel de Icaza 的博客(和 iPhone 信息).
请注意,您不能使用动态代码或通用接口,例如
Note that you cannot use dynamic code or generic interfaces like
interface IFoo<T> {
...
void SomeMethod ();
}
你必须编译你使用的所有库的 DLL.
And you will have to compile the DLLs of all the libraries you use.
PS:请务必使用Full";AOT 解决您的问题.
PS: Make sure to use "Full" AOT for your problem.
这篇关于将 .NET 应用程序转换为 x86 本机代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!