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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access