C DLL 和 Python 应用程序之间的 IPC 来处理数据

问题描述 投票:0回答:1

我想将消息结构从 DLL 回调函数发送到 python 应用程序,以便我可以记录消息。

为此我想使用 ZeroMQ。遗憾的是,我无法使用 ZeroMQ 提供的示例将消息发送到 python。


DLL:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <zmq.h>
HHOOK tHook;
HMODULE hinstDLL;
void* requester;
void* context;
LRESULT CALLBACK meconnect(int code, WPARAM wParam, LPARAM lParam) {
    if (code == HC_ACTION) {
        LPMSG data = (LPMSG)lParam;
        UINT message = data->message;
        switch (message)
        {
        case WM_POINTERUPDATE:
            if (!IS_POINTER_INCONTACT_WPARAM(wParam))
                break;
        case WM_POINTERDOWN:
        case WM_POINTERUP:
            POINTER_INFO pointerInfo = {};
            GetPointerInfo(GET_POINTERID_WPARAM(wParam), &pointerInfo);        
            int request_nbr;
            for (request_nbr = 0; request_nbr != 10; request_nbr++) {
                char buffer[10];
                printf("Sending Hello %d…\n", request_nbr);
                zmq_send(requester, data, 5, 0);
                zmq_recv(requester, buffer, 10, 0);
                printf("Received World %d\n", request_nbr);
            }        
        }
    }
    return(CallNextHookEx(tHook, code, wParam, lParam));
}
extern "C" __declspec(dllexport) BOOL ConnectServer() {
    printf("Connecting to hello world server…\n");
    static void* context = zmq_ctx_new();
    static void* requester = zmq_socket(context, ZMQ_REQ);
    zmq_connect(requester, "tcp://127.0.0.1:5555");
    printf("connected");
    return TRUE;
}
extern "C" __declspec(dllexport) BOOL DisconnectServer() {
    zmq_close(requester);
    zmq_ctx_destroy(context);
    return TRUE;
}
extern "C" __declspec(dllexport) BOOL SetHook()
{
    tHook = SetWindowsHookEx(WH_GETMESSAGE, meconnect, hinstDLL, 0);

    if (tHook == NULL)
        return FALSE;
    else
        return TRUE;
}
extern "C" __declspec(dllexport) BOOL UnHook()
{
    return UnhookWindowsHookEx(tHook);
}


BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        hinstDLL = hModule;
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

Python:

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://127.0.0.1:5555")

def message_msg_loop():
    while True:
        #  Wait for next request from client
        message = socket.recv()
        print("Received request: %s" % message)

        #  Do some 'work'
        time.sleep(1)

        #  Send reply back to client
        socket.send(b"World")

def pointer_msg_loop():
    global lib
    lib = cdll.LoadLibrary(r'C:\Users\Braun\Documents\BA_Thesis\ba-oliver-braun-logging-tool-code\MessagesDll\x64\Release\HOOKDLL.dll')
    print(lib)
    res = lib.ConnectServer()
    res = lib.SetHook()
    pythoncom.PumpMessages()
    res = lib.UnHook()

基本上我的计划是通过 Windows 消息检测某个事件,并将消息结构从 DLL 回调传递到 Python 中的服务器,这样我就可以在那里处理数据并将它们放入日志文件中。但似乎不起作用。

python c winapi ipc zeromq
1个回答
2
投票

简单性帮助我们开始,
而不是继续陷入复杂性第一

最好避免所有复杂性:

  • 设置
    .setsockopt( zmq.LINGER, 0 ) # ALWAYS
    ,永远不知道什么版本会尝试加入俱乐部
  • 原型
    PUSH/PULL
    (它a)满足规范。+b)不会像所有REQ/REP那样阻塞在
    相互死锁
    中)
  • 永远不要共享套接字(是的,
    requester
    应该是私有的、非共享的实例)
  • always读入和
    assert
    -evalZeroMQ API调用的返回代码(现场检测许多问题)

你能POSACK/证明这两个模块级声明吗

...
void* requester;
void* context;
LRESULT CALLBACK meconnect(...) {...}
...

实际上按预期工作,还是

ConnectServer(){...}
的内部范围内声明掩盖了这两个全局


extern "C" __declspec(dllexport) BOOL ConnectServer() {
    printf("Connecting to hello world server…\n");
    static void* context = zmq_ctx_new();                   // shadows out void* context
    static void* requester = zmq_socket(context, ZMQ_REQ); //  shadows out void* requester
    zmq_connect(requester, "tcp://127.0.0.1:5555");
    printf("connected");
    return TRUE;
}
© www.soinside.com 2019 - 2024. All rights reserved.