使用 EasyHook 我设置了以下结构:
APP <--> 接口<--> DLL
当我按下 APP 中的按钮时,我试图在注入的 DLL 中运行一些代码。
我设法让 DLL 使用以下代码向外部发送消息:
((EntryPoint)HookRuntimeInfo.Callback).Interface.WriteLine("");
但是我怎样才能真正让代码在注入的 DLL 中运行呢?
您需要配置双向 IPC 接口。有多种不同的方法可以实现这一点。下面是一个使用 .NET Remoting 的示例。
首先看一下 EasyHook远程文件监控教程,作为创建接口以将消息从 DLL 发送回 APP 的起点,即APP <- interface <- DLL。
要允许来自APP -> 接口 -> DLL的消息,需要在 DLL IEntryPoint 构造函数中配置一个新通道:例如
#region Allow client event handlers (bi-directional IPC)
// Attempt to create a IpcServerChannel so that any event handlers on the client will function correctly
System.Collections.IDictionary properties = new System.Collections.Hashtable();
properties["name"] = channelName;
properties["portName"] = channelName + Guid.NewGuid().ToString("N"); // random portName so no conflict with existing channels of channelName
System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider binaryProv = new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider();
binaryProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
System.Runtime.Remoting.Channels.Ipc.IpcServerChannel _clientServerChannel = new System.Runtime.Remoting.Channels.Ipc.IpcServerChannel(properties, binaryProv);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(_clientServerChannel, false);
#endregion
要从APP -> 接口 -> DLL实现 IPC,请查看Direct3DHook 项目的“客户端事件”中的方法和事件,Disconnect
以及CaptureInterface.cs中 的所有内容。除了接口类之外,此方法还使用客户端事件代理类,该类继承自并允许在 DLL 中的其他位置调用事件处理程序,以响应 APP 调用方法。您需要仔细查看链接的代码,还有一些额外的兴趣点需要考虑(例如事件处理程序的生命周期),接口实现了每个事件的包装器以“安全”的方式触发它.Disconnected
CaptureInterface.Disconnect
CaptureInterface.Disconnected
ClientCaptureInterfaceEventProxy.Disconnected
MarshalByRefObject
最后,Disconnected
事件的处理程序附加在 DLL 的IEntryPoint
Run 方法中:
_interface.Disconnected += _clientEventProxy.DisconnectedProxyHandler;
_clientEventProxy.Disconnected += () =>
{
// This code in the DLL will run when APP calls CaptureInterface.Disconnect
};