简体   繁体   English

将.NET App转换为x86本机代码

[英]Converting .NET App to x86 native code

There's a program written entirely in C# that targets .NET Framework 2.0. 有一个完全用C#编写的程序,它以.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? 有没有办法我可以以某种方式编译(翻译)托管EXE到本机的EXE,因此它可能是.NET不可知的? 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. 问题是我们要在运行没有安装.NET Framework的Windows XP的计算机上部署该程序。 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). 还要求程序的大小不得超过500Kb(最大1Mb),因为它是从Web服务器下载的(现在大小为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. 这就是为什么我们无法将完整的.NET FX(甚至是简化的)附加到下载程序的文件中。

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. 显然,这是一个可怕的软件工程错误应该早先检测到并避免,所以我们可以使用像C ++这样的本机技术。

We have tried for now Novell's Mono - an open-source implementation of .NET Framework for Linux, MAC and Windows. 我们现在已经尝试过Novell的Mono - 一个用于Linux,MAC和Windows的.NET Framework的开源实现。 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). Mono由C#编译器,IDE,运行时(CLR)和类库程序集组成(如System.dll和mscorlib.dll - 非常类似于安装到GAC的.NET的类库程序集)。 What we tried to do is to locate CLR files and ship those along with our program's file and a few assemblies. 我们尝试做的是找到CLR文件并将它们与我们程序的文件和一些程序集一起发送。 This way the program can be invoked by running "mono program.exe" (command prompt) on a user's computer. 这样,可以通过在用户的计算机上运行“mono program.exe”(命令提示符)来调用该程序。 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. 除了给最终用户CLR文件(mono.exe和mono.dll)的这种使用带来的不便之外,总共大约2.5 Mb,远远大于所需的500 Kb甚至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... 所以,我们没有其他选择,只能通过编译器将我们的.NET应用程序转换为本机应用程序,但问题仍然存在 - 我们应该使用什么编译器以及我们在哪里可以找到...

For now I have stumbled upon a Singularity OS Project by Microsoft Research. 目前我偶然发现了微软研究院的Singularity OS项目。 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). Singularity OS包括一个Bartok编译器,操作系统使用该编译器将托管程序转换为本机程序(x86 32位)。 It should be noted that Bartok can't translate all the aspects of .NET 2.0 to a native code, but most of them. 应该注意的是,Bartok无法将.NET 2.0的所有方面都转换为本机代码,但大部分都是如此。 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. 如果你能提供一些有关问题的有用提示和建议,你自己使用Singularity OS和Bartok Compiler的经验,或者我忽略的问题的另一种方法以及解决方法,我将非常感谢你。

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. 最后,使用Mono的Full AOT功能(在Callum Rogers的建议下)我设法生成了缺少CLI头的program.exe.dll。 So it looks to me like a native dll. 所以它看起来就像一个原生的dll。 However I can't figure out how to convert that dll into exe or make it operational. 但是,我无法弄清楚如何将该DLL转换为exe或使其运行。 Also this dll doesn't seem to expose any functions of interest such as main function. 此dll似乎也没有暴露任何感兴趣的函数,如main函数。

Check out AOT (Ahead Of Time) Compilation from the Mono project. 从Mono项目中查看AOT(Ahead Of Time)编译。 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 . 这会将您的托管项目编译为本机exe或elf可执行文件(取决于您定位的系统), 而不需要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. 这是用于将单声道应用程序添加到iPhone(不允许使用JIT / Framework)的技术,并且还具有更快的启动时间,更低的内存使用率以及使人们更难以反编译代码的额外好处。 You said you were already using Mono, so it should be compatible. 你说你已经在使用Mono,所以它应该是兼容的。

Read up about it at the mono-project.com website and at Miguel de Icaza's blog (and iPhone info ). 在mono-project.com网站Miguel de Icaza的博客 (和iPhone信息 )上阅读相关内容。

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. 并且您将必须编译您使用的所有库的DLL。

PS: Make sure to use "Full" AOT for your problem. PS:确保为您的问题使用“完整”AOT。

2018 Update 2018年更新

At Build 2018, Microsoft announced .Net Core 3.0 roadmap that support Windows desktop applications (Winform & WPF) 在Build 2018,微软发布了支持Windows桌面应用程序的WinNet 3.0路线图 (Winform和WPF)

2017 Update 2017年更新

For console apps, you can use .net core Self-contained deployments (SCD) . 对于控制台应用程序,您可以使用.net核心自包含部署(SCD) Even for a hello world app, your package will 50MB+. 即使是一个hello world应用程序,你的软件包也会达到50MB +。 You still need to install VC runtime though. 您仍然需要安装VC运行时。

Update 更新

As @jenix's comment , .NET Native is only for Windows Store Apps(UWP). 正如@ jenix的评论 ,.NET Native仅适用于Windows应用商店应用(UWP)。 After 3 years of it's announcement, this is still true, .net native for desktop may be dropped by microsoft . 经过3年的宣布,这仍然是正确的,桌面的.net native可能会被microsoft删除。 So this answer is not applicable anymore. 所以这个答案不再适用了。

======== ========

Microsoft Announced .NET Native Preview on Build 2014 Microsoft在Build 2014上宣布了.NET Native Preview

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. 使用.NET Native Developer Preview,应用程序将作为完全独立的本机编译代码部署在最终用户设备上,并且不会依赖目标设备/计算机上的.NET Framework。 So, no .NET framework required on the target machine with .NET Native. 因此,使用.NET Native的目标计算机上不需要.NET框架。

Announcing .NET Native Preview 宣布.NET Native Preview
Microsoft .NET Native Microsoft .NET Native

有一个名为CrossNet的项目解析.Net程序集并生成非托管C ++代码,可以在任何标准编译器中编译。

不是真正的.NET到本机转换的解决方案,但也许这有帮助: 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. 为了简化已经加重的过程,您可以使用Reflector(进入Microsoft C ++)之类的东西来反汇编.NET应用程序,并将其作为基础来启动,并将托管C ++引用替换为本机引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM