Python import dll

pajm picture pajm · Mar 10, 2011 · Viewed 64.1k times · Source

How would I import a winDLL into python and be able to use all of its functions? It only needs doubles and strings.

Answer

David Heffernan picture David Heffernan · Mar 10, 2011

You've tagged the question ctypes and so it sounds like you already know the answer.

The ctypes tutorial is excellent. Once you've read and understood that you'll be able to do it easily.

For example:

>>> from ctypes import *
>>> windll.kernel32.GetModuleHandleW(0)
486539264

And an example from my own code:

lib = ctypes.WinDLL('mylibrary.dll')
#lib = ctypes.WinDLL('full/path/to/mylibrary.dll')
func = lib['myFunc']#my func is double myFunc(double);
func.restype = ctypes.c_double
value = func(ctypes.c_double(42.0))