将 CLR 主机注入正在运行的进程 - 可能吗?
我知道,这条路充满了疯狂。
我有一个可执行文件(我无权访问的源代码),我想通过 .NET 扩展它。 它是本机可执行文件,因此我需要注入 CLR 主机才能执行此操作。 我的基本想法是通过 .NET 语言(例如 C#)提供类似脚本的功能,并在目标可执行文件中提供挂钩以供脚本操作,反之亦然。
我知道我需要使用各种技术来实现这一点 - DLL 注入、一些运行时 ASM 注入等,但我想知道的是:我所说的可能吗? 更好的是——以前有人做过这样的事情吗?
This way lies madness, I know.
I have an executable (the source code to which I do not have access) that I would like to extend via .NET. It's a native executable, so I would need to inject a CLR host in order to do this. My basic idea is to provide scripting-like functionality via a .NET language (C#, for example) and provide hooks in the target executable for the scripts to manipulate, and vice versa.
I know I'll need to use various techniques to achieve this - DLL injection, some runtime ASM injection, etc., but what I'd like to know is this: is what I'm talking about possible? Better still - has anyone done something like this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的本机可执行文件能够使用 COM 对象,则可以使用 COM 互操作来执行此操作。 如果您注册 .NET 程序集以进行互操作,那么您的本机可执行文件可以像“普通”COM 对象一样使用您的 .NET 类,并且当创建第一个类时,它将在本机进程内启动 CLR。 类似地,如果您可以从本机可执行文件公开 COM 对象,那么如果您创建互操作程序集(或者甚至没有类型库,如果您仅使用 IDispatch),则可以从 .NET 代码中使用它们。
基础知识很简单,但我只是触及了表面 - 对于像这样的严肃项目,您需要认真的参考。 我强烈推荐 Adam Nathan 撰写的 .NET 和 COM 完整互操作性指南。 这是一本大部头的书,内容丰富,而且还提供了大量有关设计 .NET 和 COM 类以实现干净互操作的重要信息。 它还解释了如何直接在本机应用程序中托管 CLR,但如果不访问源代码,该选项可能不实用。 我肯定会从 COM 互操作路线开始,并且如果没有其他选择,则仅在本机托管 CLR。
You can do this using COM interop, if your native executable is capable of using COM objects. If you register your .NET assembly for interop then your native executable can use your .NET classes just like "ordinary" COM objects, and when the first one is created it will spin up the CLR inside the native process. Similary if you can expose COM objects from the native executable then you can use them from your .NET code if you create an interop assembly (or even without a type library, if you use only IDispatch).
The basics are straightforward but I'm just scratching the surface - for a serious project like this you need a serious reference. I highly recommend .NET and COM, The Complete Interoperability Guide by Adam Nathan. It's a big book that doesn't leave much unsaid, and it also has a lot of great information on designing .NET and COM classes to interop cleanly. It also explains how to host the CLR directly inside a native app, but that option may not be practical without access to the source code. I would definitely start with the COM interop route, and only host the CLR natively if you have no other option.
实际上,我们为自动化框架做了非常类似的事情。 您可能想查看 CodePlex 上的 EasyHook 项目。 它有很多不错的功能,包括用于跨进程通信的内置 IPC Communicator。 它需要一些跑腿工作,但应该完全符合您的要求。
We actually do something very similar for an automation framework. You might want to take a look at the EasyHook project on CodePlex. Its got a ton of nice features including a built in IPC Communicator for cross-process communication. It required a bit of leg work but should do exactly what you are looking for.