gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:22:24 27 4
gpt4 key购买 nike

我想将消息结构从 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 Principles in less than Five Seconds"
在深入了解更多细节之前


<小时/>

简单性帮助我们开始,
而不是一头扎进复杂性第一

最好避免所有复杂性:
- set .setsockopt( zmq.LINGER, 0 ) # 永远,永远不知道哪个版本会尝试加入俱乐部
- 具有PUSH/PULL原型(prototype)(a)符合规范。+b)不会在mutual deadlock中阻塞正如所有 REQ/REP 所做的那样)- 永远不要共享套接字(是的,请求者应该是私有(private)的、非共享的实例)
- 始终读入并断言-评估 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;
}

关于python - C DLL 和 Python 应用程序之间的 IPC 来处理数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60300831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com