O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using WinDll

As an example we’ve built an extremely basic DLL called simple.dll. This exports two functions, Min(a, b) and Max(a, b), which return the minimum and maximum of two numbers. All arguments and return types are integers. The following example shows how to load and use the DLL, presuming it’s in c:\temp. You can omit a path if it’s on the Windows path:

>>> from dynwin.windll import *
>>> mod1 = module('c:\\temp\\simple')  # loads the DLL
>>> mod1.handle         # it can report its location in memory
22806528
>>> mod1.Min(27, 28)    # loads and executes Min function
27
>>> mod1.Min            # we now have a 'callable function' object...
<callable function "Min">
>>> mod1.Min.address    #...which knows its address too
22836704

WinDLL is doing a lot of work behind the scenes here, using Python’s abilities to introspect and trap attribute access. Go to windll.py`s source to see how it works.

WinDLL can transparently handle any integer or pointer arguments. The vast majority of Windows API calls have arguments that are either an integer, a pointer to a string, or some other structure, all of which require four bytes of memory. If you need to handle other types of arguments, it may be necessary to drop down a level and use the lower-level argument-formatting functions in CallDLL.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required