c# - EasyHook 和通讯

标签 c# dll easyhook

使用 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.DisconnectCaptureInterface.DisconnectedClientCaptureInterfaceEventProxy.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/

相关文章:

c# - 在 .NET MVC 2 中创建图形面包屑或进程跟踪

c# - 是否应将关键字添加到 C# 以引发事件?

c# - 尽管 WebJobs SDK 处理程序已成功完成,但服务总线消息仍被放弃

c# - SQLite 更新后 : Unable to find an entry point named 'sqlite3_changes_interop' in DLL 'SQLite.Interop.dll'

java - Android 上 Java lib 和 C++ dll 之间的性能

dll - WinXP下如何修复DWMAPI.DLL延迟加载依赖?

c# - 我需要 Hook 什么功能(使用简易 Hook )以防止最小化第三方应用程序?

code-injection - Windows - 如何在应用程序启动前将代码注入(inject)应用程序的内核?

c++ - 如何使用非托管 EasyHook 从进程中的任何线程 Hook 方法?

c# - 如何在C#进程和非托管C进程之间共享快速内存?