在“任何 CPU".NET 程序集上强制 x86 CLR [英] Force x86 CLR on an 'Any CPU' .NET assembly

查看:21
本文介绍了在“任何 CPU".NET 程序集上强制 x86 CLR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET 中,平台目标:任何 CPU"编译器选项允许 .NET 程序集在 x64 机器上以 64 位运行,在 x86 机器上以 32 位运行.还可以使用平台目标:x86"编译器选项强制程序集在 x64 机器上作为 x86 运行.

In .NET, the 'Platform Target: Any CPU' compiler option allows a .NET assembly to run as 64 bit on a x64 machine, and 32 bit on an x86 machine. It is also possible to force an assembly to run as x86 on an x64 machine using the 'Platform Target: x86' compiler option.

是否可以运行带有Any CPU"标志的程序集,但要确定它应该在 x86 还是 x64 CLR 中运行?通常这个决定是由 CLR/OS 加载器(根据我的理解)根据底层系统的位数做出的.

Is it possible to run an assembly with the 'Any CPU' flag, but determine whether it should be run in the x86 or x64 CLR? Normally this decision is made by the CLR/OS Loader (as is my understanding) based on the bitness of the underlying system.

我正在尝试编写一个 C# .NET 应用程序,它可以与其他正在运行的进程交互(读取:将代码注入).x64进程只能注入其他x64进程,x86也一样.理想情况下,我想利用 JIT 编译和 Any CPU 选项来允许使用单个应用程序注入 x64 或 x86 进程(在 x64 机器上).

I am trying to write a C# .NET application that can interact with (read: inject code into) other running processes. x64 processes can only inject into other x64 processes, and the same with x86. Ideally, I would like to take advantage of JIT compilation and the Any CPU option to allow a single application to be used to inject into either x64 or x86 processes (on an x64 machine).

这个想法是将应用程序编译为任何 CPU.在 x64 机器上,它将作为 x64 运行.如果目标进程是 x86,它应该重新启动自己,强制 CLR 作为 x86 运行它.这可能吗?

The idea is that the application would be compiled as Any CPU. On an x64 machine, it would run as x64. If the target process is x86, it should relaunch itself, forcing the CLR to run it as x86. Is this possible?

推荐答案

您可以使用 CorFlags 应用程序.要了解应用程序将如何运行,请使用:

You can find out how an application will run and change it statically using the CorFlags application. To find out how the application will run, use:

corflags <PathToExe>

要更改应用程序的运行方式,请使用:

To change how the application will run, use:

corflags /32bit+  <PathToExe>

这将使 EXE 文件作为 32 位进程运行.有关程序集应如何运行的信息存储在 PE 标头中.请参阅堆栈溢出问题如何查找本机 DLL 文件是编译为 x64 还是 x86?.

This will make the EXE file run as a 32-bit process. The information about how the assembly should run is stored in the PE header. See Stack Overflow question How to find if a native DLL file is compiled as x64 or x86?.

如果你想在运行时注入代码,你必须编写一个.NET C++/COM 中的分析器.请参阅.NET 内部结构:分析 API分析(非托管 API 参考) 了解更多详情.

If you want to inject code at run time, you have to write a .NET profiler in C++/COM. See .NET Internals: The Profiling API and Profiling (Unmanaged API Reference) for more details.

您需要实现 JitCompilationStarted 回调并在那里完成您的工作.如果朝这个方向发展,则必须将注入的 DLL 文件构建为 x86 和 x64.一旦设置了以下环境变量,CLR 将加载本机 DLL 文件:

You'll need to implement the JitCompilationStarted callback and do your work there. If you go in this direction, you'll have to build the injecting DLL file both as x86 and x64. The native DLL files will be loaded by the CLR once the following environment variables will be set:

Cor_Enable_Profiling=0x1
COR_PROFILER={CLSID-of-your-native-DLL-file}

如果设置正确,则 64 位版本将看到"64 位进程,而 32 位版本将看到"32 位进程.

If you have it set correctly then the 64-bit version will 'see' the 64 bit processes and the 32-bit version will 'see' the 32-bit processes.

这篇关于在“任何 CPU".NET 程序集上强制 x86 CLR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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