力86 CLR上的“任何CPU”.NET程序集 [英] Force x86 CLR on an 'Any CPU' .NET assembly

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

问题描述

在.NET中,平台目标:任何CPU编译器选项允许.NET程序集为64&NBSP运行;在x64机器上的位,和32 NBSP;位的x86机器上。也可以强制装配为86用在x64机器上运行平台目标:86。编译器选项

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.

是否有可能运行与'任何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应用程序,可以用(读:注资code到)进行交互的其他正在运行的进程。 64进程仅注入到其它的x64进程,和相同的带86。理想情况下,我想借此JIT编译和的任何CPU的优势的选项,以允许一个单一的应用程序可用于注入无论是64或86的过程(在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的机器,它将为64运行。如果目标进程是86,它应该重新启动本身,迫使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 how 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>

这将使作为运行32位程序的EXE文件。有关如何装配时应运行的信息存储在PE头。见堆栈溢出问题的 如何找到,如果本机的DLL文件编译为64位或86?

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?.

如果你想在运行时注入code,你必须写一个 .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。

You'll need to implement the JitCompilationStarted callback and do your work there. If you're 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.

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

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