我们可以创建一个 VC++可以在 32 位和 64 位 Windows 上本机运行的可执行文件吗?

发布于 2024-09-16 01:46:15 字数 164 浏览 1 评论 0原文

有没有办法构建一个 VC++ 项目,以便它创建的 dll/exe 在 32 位 Windows 操作系统上作为 32 位应用程序运行,在 64 位 Windows 操作系统上作为 64 位应用程序运行(不在 WOW64 中)。

我知道对于使用 /ANYCPU 选项的 C# 应用程序来说这是可能的。

Is there any way to build a VC++ project so that the dll/exe created by it will work as a 32 bit application on a 32 bit Windows OS and as a 64 bit application on a 64 bit Windows OS (not in WOW64).

I know that is possible for C# applications using the /ANYCPU option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘴硬脾气大 2024-09-23 01:46:15

CLR 对 /ANYCPU 选项有特殊的加载程序支持。

如果您确实想对本机执行此操作,最好的方法是:

  1. 为 32 位和 64 位构建二进制文件
  2. 作为构建 32 位二进制文​​件的一部分,请包括 64 位二进制文​​件-bit 二进制文件作为资源
  3. 在 32 位机器上,只需运行 32 位二进制文​​件
  4. 在 64 位机器上,当 32 位二进制文​​件运行时,解压 64 位二进制资源,将其写入磁盘,然后从这

就是 Sysinternals 工具的工作原理(将 Process Explorer 下载到64 位机器并运行它:您会看到它将 procexp64.exe 写入磁盘,然后从那里运行它)。这是一个 hack,但它确实有效。

The CLR has special loader support for the /ANYCPU option.

If you really want to do this for native, the best way to do it is to:

  1. Build your binary for both 32- and 64-bit
  2. As part of building the 32-bit binary, include the 64-bit binary as a resource
  3. On 32-bit machines, just run the 32-bit binary
  4. On 64-bit machines, when the 32-bit binary runs, unpack the 64-bit binary resource, write it to disk, and run it from there

This is how the Sysinternals tools work (download Process Explorer onto a 64-bit machine and run it: you'll see that it writes procexp64.exe to disk and then runs it from there). It's a hack, but it works.

凉薄对峙 2024-09-23 01:46:15

不是 AFAIK - 问题是

  1. 你需要单独的代码;这适用于 C#,因为您正在生成 .NET IL,它会在目标系统上转换为本机代码,
  2. Windows PE 格式只有一个图像标头,并且无法稍后链接到另一个标头以将两组代码放入同一个图书馆。

对于 .EXE,您可以做的最好的事情就是发送一个 32 位 .exe,检查它是否正在运行 WOW64,然后生成 64 位版本。不过,我想不出库的等效技巧 - 它必须首先匹配要加载的主机进程的位。

Not AFAIK - the problem is that

  1. you'll need separate code; this works for C# because you're generating .NET IL which gets converted to native code on the target system
  2. the windows PE format only has one image header and no way to chain through to another header later on to put both sets of code in the same library.

The best you could do for an .EXE was to ship a 32-bit .exe that checks if it's running WOW64 then spawns the 64-bit version instead. I can't think of an equivalent trick for libraries, though - it has to match the bits of the host process to load in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文