簡體   English   中英

C DLL 和 Python 應用程序之間的 IPC 以處理數據

[英]IPC between a C DLL and Python application to process data

我想將消息結構從 DLL 回調函數發送到 python 應用程序,以便我可以記錄消息。

為此,我想使用 ZeroMQ。 遺憾的是,我無法使用 ZeroMQ 提供的示例將消息發送到 python。


動態鏈接庫:

// 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 中的服務器,這樣我就可以處理那里的數據並將它們放入日志文件中。 雖然它似乎不起作用。

如果從未使用過 ZeroMQ,
在這里可以先看看“不到五秒的ZeroMQ原則
在深入了解更多細節之前


簡單幫助我們開始,
而不是繼續迎頭撞向復雜性第一

最好避免所有的復雜性:
- set .setsockopt( zmq.LINGER, 0 ) # ALWAYS ,永遠不知道哪個版本會嘗試加入俱樂部
- 帶有PUSH/PULL原型(它 a)符合規范。+b)不會像所有REQ/REP那樣阻塞在相互死鎖中) -從不共享套接字(是的, requester應該是一個私有的、非共享的實例)
-始終讀入和assert -評估來自 ZeroMQ 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM