AppDomain.CreateInstanceFromAndUnwrap - 无法施展透明代理 [英] AppDomain.CreateInstanceFromAndUnwrap - Unable to cast transparent proxy
问题描述
我正在写一个.NET库注入DLL的管理到外部进程。我目前的做法是:
I'm writing a .NET library to inject managed DLLs into external processes. My current approach is:
- 使用
远程线程
来强制目标进程调用调用LoadLibrary
在非托管引导DLL。从这一点我们在目标进程中执行代码。 - 我的引导DLL然后创建CLR和调用的实例
ExecuteInDefaultAppDomain
就可以了,它执行的管理助手DLL的方法。 - 此方法创建一个新的AppDomain,并呼吁
AppDomain.CreateInstanceFromAndUnwrap
传递执行了我的负载DLL,铸造结果为IInjectionPayload
。 - 的想法是,我的有效载荷DLL公开它实现了一个类
IInjectionPayload
,所以助手DLL可以简单地调用payload.Run()
。
- Use
CreateRemoteThread
to force the target process to callLoadLibrary
on an unmanaged bootstrap DLL. From this point we're executing code in the target process. - My bootstrap DLL then creates an instance of the CLR and calls
ExecuteInDefaultAppDomain
on it, which executes a method in a managed helper DLL. - This method creates a new AppDomain and calls
AppDomain.CreateInstanceFromAndUnwrap
to pass execution into my payload DLL, casting the result as anIInjectionPayload
. - The idea is that my payload DLL exposes a class which implements
IInjectionPayload
, so the helper DLL can simply callpayload.Run()
.
我在做这种方式通过 AppDomain.Unload
(后简单地调用,使有效载荷的代码可以完全卸载。信令它来清理)
I'm doing it this way so that the payload code can be completely unloaded by simply calling AppDomain.Unload
(after signalling it to clean up).
本方法适用 - 在我载荷DLL类是越来越实例化目标的过程,所以代码的可以的执行 - 但我不能投用 CreateInstanceFromAndUnwrap
返回到对象的 IInjectionPayload
;它抛出以下异常:
This approach works - the class in my payload DLL is getting instantiated in the target process, so code can be executed - but I can't cast the object returned by CreateInstanceFromAndUnwrap
to an IInjectionPayload
; it throws the following exception:
无法施展透明代理键入blah.Blah.IInjectionPayload
Unable to cast transparent proxy to type 'blah.Blah.IInjectionPayload'.
我试过使用 CreateInstanceAndUnwrap
和 Activator.CreateInstanceFrom
然后按 Object.Unwrap
,但是这两种方法也导致同样抛出异常。
I've tried using CreateInstanceAndUnwrap
, and Activator.CreateInstanceFrom
followed by Object.Unwrap
, but both of these methods also cause the same exception to be thrown.
我的有效载荷类的签名是:
The signature of my payload class is:
公共类节目:MarshalByRefObject的,IInjectionPayload
我很为难,因为有效载荷DLL肯定是越来越加载和类被实例化,如预期。 。任何帮助将非常感激。
I'm stumped because the payload DLL is definitely getting loaded and the class is being instantiated, as intended. Any help would be much appreciated.
推荐答案
找到修复此问题在这里:的 http://www.west-wind.com/WebLog/posts/601200.aspx
Found the fix for this problem here: http://www.west-wind.com/WebLog/posts/601200.aspx
它看起来像在.NET框架的错误。该解决方案是一个处理程序添加到 AppDomain.CurrentDomain.AssemblyResolve
其中手动加载&安培;返回大会 args.Name
。然后,你可以调用 CreateInstanceFromAndUnwrap
没有它抛出一个异常。
It looks like a bug in the .NET framework. The solution is to add a handler to AppDomain.CurrentDomain.AssemblyResolve
which manually loads & returns the assembly at args.Name
. Then you can call CreateInstanceFromAndUnwrap
without it throwing an exception.
这篇关于AppDomain.CreateInstanceFromAndUnwrap - 无法施展透明代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!