Calling dlls with Calldll/WinDll or Ctypes or...

Myles myles at geocities.com
Wed Jan 14 01:57:41 CET 2004


> Pardon my ignorance here.  I would like to talk to someone that has
> had success in calling ddls in Windows NT/2000/XP.

caldll works, but the more recent ctypes is easier to use.

The ctypes documentation has nice examples for calling functions
exported by standard Windows DLLs (i.e. Microsoft ones that are
supplied with Windows).
Below are a couple of examples of calling third-party DLLs.

Regards, Myles.

# ------------------ snip ------------------------

from ctypes import *
import time

# ------------------------------------------------

anotherdll = windll.AutoItDLL  # loads AutoItDLL.dll (in same directory)

# AutoItDLL.dll exports a function AUTOIT_ClipPut
anotherdll.AUTOIT_ClipPut(c_char_p("something"))

# AutoItDLL.dll exports a function AUTOIT_ClipGet
s = c_buffer(16384)  # size of clipboard buffer
time.sleep(2)  # apparently it takes a while to create that buffer ?
anotherdll.AUTOIT_ClipGet(s)
print repr(s.value)

# ------------------------------------------------

somedll = windll.FreeImage  # loads FreeImage.dll (in same directory)

# some DLLs export function names strangely 
# (open the .dll in a capable text editor to read the function names)
GetVersion = getattr(somedll, "_FreeImage_GetVersion at 0")
GetVersion.restype = c_char_p
print GetVersion()

GetFileType = getattr(somedll, "_FreeImage_GetFileType at 8")
filename = "/temp/screen01.jpg"
imgtype = GetFileType(filename, 0)

Load = getattr(somedll, "_FreeImage_Load at 12")
image_ptr = Load(imgtype, filename, 0)

GetWidth = getattr(somedll, "_FreeImage_GetWidth at 4")
print GetWidth(image_ptr)

# --------------- snip ---------------------------



More information about the Python-list mailing list