Skip to main content
 首页 » 编程设计

python之如何获取 dll 消息调度程序并重定向到 python stdout

2024年10月01日7jyk

我正在尝试弄清楚如何进行,以及它是否可行。

我使用外部 DLL 来控制我的机械延迟线。

此 API 具有在单独窗口中输出消息的内部过程。我强烈希望捕获此消息流并将其呈现在我的 python (PyQT5) 编写的应用程序中。

在API描述中有一个函数:

int LS_SetProcessMessagesProc(void *pProc); 

如果没有错误或存在错误,函数分别返回 0 或 1。

根据dll描述 它可以替换 LStep API 的内部消息调度程序。

LStep API 在等待主线程消息中的 LStep 确认期间进行处理。如果您想切换 Message-Dispatching 或替换为您的 onw 代码,您可以使用 SetProcessMessagesProc 来使用回调过程。

pProc 必须是指向不带参数的 stdcall 过程的指针:

void MyProcessMessages() {...} 

例子: LS.SetProcessMessagesProc(&MyProcessMessages);

例如,如果我们采用 python stdout,我如何向它发送消息?

请您参考如下方法:

我将在以下方面说明一切:

这是您需要的(稍微复杂一点的示例):在外部 .dll 中定义的函数需要调用另一个自定义函数(由您在 Python), 通过 [Python]: ctypes module (在 Win 上)。

代码:

import ctypes 
from ctypes import wintypes 
 
try: 
    from win32gui import GetWindowText 
    pywin32_present = True 
except ImportError: 
    pywin32_present = False 
 
 
def enum_windows_proc(hwnd, l_param): 
    print("HWND: {}\n".format(hwnd)) 
    if pywin32_present: 
        txt = GetWindowText(hwnd) 
        if txt and "MSCTFIME UI" not in txt and "Default IME" not in txt: 
            print("  Window text: {}\n".format(txt)) 
    return 1 
 
 
def main(): 
    user32_dll = ctypes.windll.LoadLibrary("user32.dll") 
    enum_windows = user32_dll.EnumWindows 
 
    WND_ENUM_PROC_TYPE = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM) 
    enum_windows.argtypes = (WND_ENUM_PROC_TYPE, wintypes.LPARAM) 
    enum_windows.restype = wintypes.BOOL 
 
    enum_windows(WND_ENUM_PROC_TYPE(enum_windows_proc), wintypes.LPARAM(0)) 
 
 
if __name__ == "__main__": 
    main() 

注意事项:

  • import年代:
    • wintypes 是一个 ctypes 子模块,它定义了一堆 Ms 特定数据(常量、结构、枚举...)
    • [Python]: pywin32是 Python 对 C Win 函数的包装,它基本上是 ctypes 的更高级(和 pythonic)方法做。 Python 默认不自带,必须手动安装;在我们的示例中,它是可选的
  • def enum_windows_proc(hwnd, l_param): :
    • 它是 BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam); 的 Python 形式
    • 为每个窗口打印hwnd(窗口句柄)(注意会有很多这样的窗口,因为它们中的大多数对用户是“不可见的”)
    • 如果安装了pywin32 模块,它将用于提取每个窗口的标题(caption)。当然,这也可以用 ctypes 来完成,但有点复杂
    • 标题过滤是为了避免打印无用的文本(对于大多数窗口)。如果您需要更多详细信息,请查看:[SO]: Get the title of a window of another program using the process name
  • main功能:
    • 首先,需要加载包含函数的.dll(在我们的例子中是user32.dll)。这是使用 [MSDN]: LoadLibrary function 完成的(Ux:[man]: dlopen)。此外,返回对象 ( user32_dll ) 的内部结构(需要下一行看起来很简单)被初始化
    • 正在使用 [MSDN]: GetProcAddress function 检索 ( enum_windows ) 函数(或更好:指向它的指针) (Ux: [man]: dlsym )
    • 接下来的 3 行代码用于让 Python 了解加载函数指针的详细信息(返回类型和参数类型)。请注意(在某些情况下)有一种更简单的方法(代码方式)来完成所有这些,但出于学习目的,完成整个过程是可以的
    • 最后,调用外部函数,将我们的自定义函数作为参数

因为会有很多输出(而且大部分是内存地址),所以我不会在这里粘贴。

针对您的问题,根据您在问题中粘贴的函数 header ,我们可以采用相同的方法(请注意代码将不复制/粘贴OOTB):

import ctypes 
from ctypes import wintypes 
 
def my_process_messages(): 
    # Your code here (delete the next (`pass`) line) 
    pass 
 
dll_name = "your dll path (full or relative)" 
dll_object = ctypes.windll.LoadLibrary(dll_name) 
ls_set_process_messages_proc = dll_object.LS_SetProcessMessagesProc 
 
PROCESS_MESSAGES_TYPE = ctypes.WINFUNCTYPE(None) 
ls_set_process_messages_proc.argtypes = (PROCESS_MESSAGES_TYPE,) 
ls_set_process_messages_proc.restype = ctypes.c_int 
 
print("ls_set_process_messages_proc returned: {}\n".format(ls_set_process_messages_proc(PROCESS_MESSAGES_TYPE(my_process_messages)))) 

注意:示例是基于外部.dll:

  • 是 Win 风格(使用 stdcall 调用约定)。如果那不是真的(它使用 cdecl),您需要更改(为了严谨起见,我要说两件事):

    • ctypes.windll至 ctypes.cdll
    • ctypes.WINFUNCTYPE至 ctypes.CFUNCTYPE
  • 导出 C 风格的函数(不是 C++ 会破坏函数名称),我几乎 100% 确定。但是,如果不是这种情况,那么抱歉,这里无事可做。有关此主题的更多详细信息,请查看:[SO]: Excel VBA, Can't Find DLL Entry Point from a DLL file .