首页 > 解决方案 > C++ DLL(本地挂钩)和 C# 应用程序之间的合适 IPC

问题描述

对于注入第三方进程和 C# 应用程序的 C++ DLL,最好的进程间通信是什么?这是目前的情况:

// This gets executed within the target process memory region
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
    if (code > 0)
    {
        auto csharpApplicationFunctionPointerAddress = 0xdeadbeef;
        auto csharpApplicationFunctionResult = call csharp function with N parameters here

        // Do something with the result
        if (csharpApplicationFunctionResult == "foo")
        {
            
        }
    }

    return CallNextHookEx(hookInstance, code, wParam, lParam);
}

我遇到了这个问题并发现我需要一个 RPC,但是我似乎找不到最适合这个问题的 RPC,因为这种通信需要传递 N 个参数并尽快返回结果。

笔记:

  1. 套接字不在意,因为我不想处理超时
  2. 命名管道会在这里工作还是只适用于字符串消息?(不考虑序列化/反序列化)
  3. 我不想对远程线程使用消息轮询,因为这会占用太多 CPU

甚至还有什么选择吗?请随时纠正我上面的注释。

标签: c++comhookipcrpc

解决方案


我最终创建了一个不可见的虚拟窗口,作为通过SendMessageDLL 调用接收消息的中心点。


推荐阅读