为Windows编写跨平台(32位和64位兼容的)程序(如值为anycpu在.NET) [英] Writing a Cross-Platform (32-bit and 64-bit compatible) Program for Windows (like AnyCPU in .NET)

查看:631
本文介绍了为Windows编写跨平台(32位和64位兼容的)程序(如值为anycpu在.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它已经莫名其妙我如何在.NET中的值为anycpu功能的工作原理:它加载的可执行文件作为本地32位,如果系统是32位和64位,如果系统是64位的(你可以用任务管理器很容易确认)。所以,很显然,这是不可能的。

It's been baffling me how the "AnyCPU" feature in .NET works: It loads the executable as native 32-bit if the system is 32-bit, and as 64-bit if the system is 64-bit (which you can easily confirm with Task Manager). So obviously, this isn't impossible.

现在的问题是,究竟微软没有做到这一点?的Windows本来不知道的.NET框架,所以在Windows PE加载器不可能期待可以为CLR头PE头在任何额外的功能;此功能必须已经添加了某种内核模式扩展。但是,.NET框架似乎安装没有这样的事情......我在一个完全丧失了如何同一个可执行文件可以是原生32位和64位的同时,特别是因为没有按MSCorEE.dll中拆卸'吨甚至表现出引用未公开的原生功能。

The question is, how exactly did Microsoft do this? Windows originally didn't know about the .NET framework, so the Windows PE Loader can't possibly look for any extra features in PE headers for the CLR header; this feature must have been added by some sort of kernel-mode extension. But the .NET framework seems to install no such thing... I'm at a complete loss at how the same executable can be native 32-bit and 64-bit at the same time, especially since a disassembly of mscoree.dll doesn't even show references to undocumented native functions.

没有人有任何的知识和/或合理的猜测,这是怎么做的?这显然​​是可能的(所以没有说这样的话这是不可能的),这让我想尝试写的本机的跨平台的EXE ...

Does anyone have any knowledge and/or reasonable guesses as to how this was done? It's obviously possible (so no saying things like "it's not possible"), and it makes me want to try to write a native cross-platform EXE...

编辑:

作为一个侧面说明,考虑如何你不能在64位Windows PE环境中运行32位可执行文件...还有的的一些方法来扩展或修改PE装载器用某种插件,对吧?

As a side note, consider how you can't run 32-bit executables in a 64-bit Windows PE environment... there's got to be some way to extend or modify the PE loader with some sort of "plugin", right?

推荐答案

您的问题是基于一种误解。下面是错误的:

Your question is based on a misunderstanding. Here's the mistake:

的Windows本来不知道的.NET框架

Windows originally didn't know about the .NET framework

其实,自从Windows XP,<一个href="http://blogs.msdn.com/b/rmbyers/archive/2009/06/09/anycpu-exes-are-usually-more-trouble-then-they-re-worth.aspx"相对=nofollow> Windows是知道的.NET可执行格式的。和XP是Windows中的第一个版本,支持64位。

Actually, since Windows XP, Windows IS aware of the .NET executable format. And XP was the first version of Windows to support 64-bits.

因此​​PE头标32位和本地导入表引用32位 mscoree ,它在Windows 2000和更早的版本,导致32位.NET来被加载。 的DllMain mscoree 开始JITting应用code和修改入口点的主应用程序。

So the PE header is marked 32-bit and the native import table references the 32 bit mscoree, which on Windows 2000 and earlier, causes 32-bit .NET to be loaded. DllMain for mscoree starts JITting the application code and modifies the entrypoint for the main application.

Windows XP和之后,意识到了.NET元数据的,认识到这是值为anycpu 并加载相应的框架。

Windows XP and later, being aware of the .NET metadata, recognizes that it is AnyCPU and loads the appropriate framework.

这里可能更比你想要知道的这个过程

因此​​,没有,没有提供原生值为anycpu exe文件。尽管可以在32位PE嵌入一个16位DOS程序,你不能有一个组合的32位和64位的.exe

So no, there is no native AnyCPU exe. Although you can embed a 16-bit DOS program in a 32-bit PE, you can't have a combined 32-bit and 64-bit .exe

这篇关于为Windows编写跨平台(32位和64位兼容的)程序(如值为anycpu在.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆