使用 EasyHook 我设置了以下结构:
APP <--> 接口(interface) <--> DLL
当我按下应用程序中的按钮时,我试图在注入(inject)的 DLL 中运行一些代码。
我设法让 DLL 使用以下代码向外部发送消息:
((EntryPoint)HookRuntimeInfo.Callback).Interface.WriteLine("");
但是我怎样才能真正让代码在注入(inject)的 DLL 中运行呢?
最佳答案
您需要配置双向IPC接口(interface)。有多种不同的方法可以实现这一点。下面是一个使用 .NET Remoting 的示例。
首先看一下EasyHook remote file monitor tutorial作为创建接口(interface)将消息从 DLL 发送回 APP 的起点,即 APP <- 接口(interface) <- DLL。
要允许来自APP -> 接口(interface) -> DLL 的消息,需要在 DLL 中配置一个新 channel IEntryPoint constructor :例如
#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 -> 接口(interface) -> DLL 实现 IPC,请查看“Client”中的 Disconnect
方法和 Disconnected
事件Direct3DHook project 的附带事件” 、CaptureInterface.Disconnect
、CaptureInterface.Disconnected
和 ClientCaptureInterfaceEventProxy.Disconnected
,全部位于 CaptureInterface.cs 中。除了接口(interface)类之外,此方法还使用从 MarshalByRefObject
继承的客户端事件代理类,并允许在 DLL 中的其他位置调用事件处理程序,以响应 APP 调用方法。您需要仔细查看链接的代码,需要考虑一些额外的兴趣点(例如事件处理程序生命周期),该接口(interface)实现了每个事件的包装器以“安全”方式触发它.
最后,Disconnected
事件的处理程序附加在 DLL 的 IEntryPoint
Run 方法中:
_interface.Disconnected += _clientEventProxy.DisconnectedProxyHandler;
_clientEventProxy.Disconnected += () =>
{
// This code in the DLL will run when APP calls CaptureInterface.Disconnect
};
关于c# - EasyHook 和通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44438799/