Dynamic DLL Access
As we mentioned in Chapter 13, Sam Rushing (http://www.nightmare.com) has written an
extension that allows Python to dynamically load and call any
function in any DLL, not just special Python extensions. For the C
programmers among you, these are equivalent to using the Windows API
functions LoadLibrary() and
GetProcAddress().
These tools offer a great deal of power but also carry risks. When using Python extension modules such as the Win32 extensions, you can be fairly confident the worst that will happen is a Python exception. When dynamically accessing a DLL, you are responsible for ensuring the arguments you pass are the correct type; an error in doing so can corrupt the stack and lead to either seriously misleading results or a crash.
Using these tools, Sam has built a comprehensive ODBC module that allows access to every function in the ODBC API and to a GUI application framework sitting directly on top of the Windows API.
In general, the safest way to access existing C libraries is to build
Python extension modules. However, there may be occasions when a C
compiler isn’t available or when dynamic loading and unloading
are required. A case study mentioned in Chapter 1,
uses Calldll/Windll to interface to an encoding
translation library that moves megabytes of data between operating
systems each day; for political reasons, C/C++ development was not an
option in this case.
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