python - 我可以包装 windows dll 以在 Linux 下的 Python 中使用它吗?

标签 python linux dll

是否可以包装 Windows DLL(特定硬件的驱动程序)以在 Linux 下从 Python 使用它。

如果是,最好的方法是什么?

最佳答案

免责声明:根据上下文,以下肯定不是最佳方法。这只是一种符合描述的可能方法。

我编写了一个小型 Python 模块,用于从 Linux 上的 Python 调用 Windows DLL。它基于常规 Linux/Unix Python 进程和基于 Wine 的 Python 进程之间的 IPC。因为我自己在太多不同的用例/场景中需要它,所以我将它设计为“通用”ctypes module直接替换,在后台自动完成大部分所需的管道。

示例:假设您在 Linux 上使用 Python,安装了 Wine,并且想要调用 msvcrt.dll(Microsoft C 运行时库)。您可以执行以下操作:

from zugbruecke import ctypes
dll_pow = ctypes.cdll.msvcrt.pow
dll_pow.argtypes = (ctypes.c_double, ctypes.c_double)
dll_pow.restype = ctypes.c_double
print('You should expect "1024.0" to show up here: "%.1f".' % dll_pow(2.0, 10.0))

Source code (LGPL) , PyPI package & documentation .

它的边缘仍然有点粗糙(即 alpha 和不安全),但它确实可以处理大多数类型的参数(包括指针)。

我真的很有兴趣了解它在与硬件驱动程序一起使用时的行为和性能。非常欢迎提供反馈!

关于python - 我可以包装 windows dll 以在 Linux 下的 Python 中使用它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13065982/

相关文章:

python - 应用引擎 : put_async doesn't work (at least in the development server)?

python - Py2app 使用控制台或 GUI,但不能同时使用两者?

linux - 如果在 shell 脚本中给出了子 ID,如何找到父 ID

python - J的x型变量: how are they stored internally?

python - 如何在不更改内置 sys 模块的情况下执行脚本?

c# - 将字符串作为 PChar 从 CSharp 传递到 Delphi DLL

c++ - 是否需要注册一个 dll 才能使用它

c++ - 项目包含的lib比dll文件多

linux - 在 Linux Mint 中快速运行许多命令

c++ - Linux 在多线程程序中处理特定线程上的信号