EasyHook 和通信

EasyHook and communication

使用 EasyHook 我设置了以下结构:

APP <--> 界面 <--> DLL

当我按下 APP 中的按钮时,我正在尝试 运行 注入的 DLL 中的一些代码。

我设法让 DLL 使用此代码向外部发送消息:

((EntryPoint)HookRuntimeInfo.Callback).Interface.WriteLine("");

但是我怎样才能真正在注入的 DLL 中编写代码 运行?

您需要配置一个双向IPC接口。有多种不同的方法可以实现这一点。以下是一个使用 .NET Remoting 的示例。

首先看一下 EasyHook remote file monitor tutorial 作为创建接口以将消息从 DLL 发送回 APP 的起点,即 APP <- interface <- DLL

要允许来自 APP -> 界面 -> DLL 的消息,需要在 DLL 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 -> 接口 -> DLL 实现 IPC,请查看 [=39] 中的 Disconnect 方法和 Disconnected 事件=] 的 Direct3DHook project, CaptureInterface.Disconnect, CaptureInterface.Disconnected and ClientCaptureInterfaceEventProxy.Disconnected, all in CaptureInterface.cs。除了接口 class 之外,此方法还使用继承自 MarshalByRefObject 的客户端事件代理 class 并允许在 DLL 的其他位置调用事件处理程序以响应 APP 调用一个方法。您需要仔细查看链接的代码,还有一些额外的兴趣点需要考虑(例如事件处理程序生命周期),该接口实现了每个事件的包装器以在 "Safe" 举止。

最后,Disconnected 事件的处理程序附加到 DLL 的 IEntryPoint 运行 方法中:

    _interface.Disconnected += _clientEventProxy.DisconnectedProxyHandler;

    _clientEventProxy.Disconnected += () =>
            {
                // This code in the DLL will run when APP calls CaptureInterface.Disconnect
            };