Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I wrap windows dll to use it in Python under Linux?

Tags:

python

linux

dll

Is it possible to wrap Windows DLL (driver for specific hardware) to use it from Python under Linux.

If yes, what would be the best approach?

like image 784
minder Avatar asked Mar 02 '26 17:03

minder


1 Answers

Disclaimer: Depending on the context, the following is certainly NOT the best approach. It is just ONE possible approach that kind of fits the description.

I wrote a small Python module for calling into Windows DLLs from Python on Linux. It is based on IPC between a regular Linux/Unix Python process and a Wine-based Python process. Because I have needed it in too many different use-cases / scenarios myself, I designed it as a "generic" ctypes module drop-in replacement, which does most of the required plumbing automatically in the background.

Example: Assume you're in Python on Linux, you have Wine installed, and you want to call into msvcrt.dll (the Microsoft C runtime library). You can do the following:

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.

It's still a bit rough around the edges (i.e. alpha and insecure), but it does handle most types of parameters (including pointers).

I'd be really interested to see how it behaves and performs when used with a hardware driver. Feedback is highly welcomed!

like image 113
s-m-e Avatar answered Mar 05 '26 06:03

s-m-e



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!