Problem with win32api function + using DLL "directly" in windows

Robert Amesz rcameszREMOVETHIS at dds.removethistoo.nl
Sat Apr 14 10:16:21 EDT 2001


Dublanc, David wrote:

>
>I use this script (that I found in the newsgroup) and the result is
>not correct. Is it possible to use Windows DLL libraries  library in
>Python ? Because there is a
>GetDiskFreeSpaceEx that seems to return the good results (thanks to
>Chris Gonnerman who gives me the name of this function).

There's the 'calldll' module, which I got from:

ftp://squirl.nightmare.com/pub/python/python-ext/calldll-python-2.0.zip

The documentation is rather terse, so it took a bit of experimenting to 
get it to work properly. The tricky bits are getting data to and from 
the dll-function, and a mistake there could really lead to spectacular 
crashes: I merely got away with some access violations, and crashing 
Python itself a couple of times. Much of the trouble came from the fact 
that I couldn't get the membuf-object to work. However, using a Python 
character  array solved it. It helps if you read the chapter in the 
Python manuals about C-extensions, because it uses Python's own 
parameter passing system.

Anyway, as an example, here's some of the code I came up with to 
interface with a .dll. It's not supposed to be an example of good 
style, but it works. The dll-function expects the address of a buffer 
filled with data, modifies it in-place, and returns an integer (0 == 
success). The "w" and the "i" 'in call_foreign_function' reflect this.


HTH,
Robert Amesz

- - -
import array, string, sys
import calldll

# Compress Xface data
def MakeXface(xface):
	# Fill buffer, make it 2K & pad with \0's
	buf = array.array('c', xface + '\0' * (2000 - len(xface)))
	
	# DLL-interface
	calldll.load_library('X-Face.dll')
	handle  = calldll.get_module_handle('X-Face.dll')
	address = calldll.get_proc_address(handle, '_compface')

	# Compress X-Face
	if calldll.call_foreign_function(address, "w", "i", (buf,)):
		return None
	
	return buf



More information about the Python-list mailing list