calldll & PyArg_ParseTuple strings

Gordon McMillan Gordon.McMillan at p98.f112.n480.z2.fidonet.org
Sat Jul 3 09:09:27 EDT 1999


From: "Gordon McMillan" <gmcm at hypernet.com>

Scott McClellan writes:
> 
> I'm attempting to use Sam Rushing's calldll module to call a C
> function in a dll, with the following prototype:
> 
> long __declspec(dllexport)func(long *x, long *y, long *z)
> 
> I expect the function to give certain values back in x, y, and z,
> but so far have been unable to make that happen. Let's say that I
> have three long integers in Python that I initialize as follows:
> 
> x=y=z=0L

A Python int is a C long. You won't find a Python long in C.

> My question is, in the following call from Python,
> 
> call_foreign_function(pFunc, 'lll', 'l', (<?>,<?>,<?>))
> 
> what should the tuple (<?>,<?>,<?>) contain? 

params = calldll.membuf(12)
params.write('\000'*12)
px = params.address()
call_foreign_function(pFunc, 'lll', 'l', (px, px+4, px+8))
rslt = params.read(0,12)
x,y,z = struct.unpack('lll', rslt)

Actually, Sam's got a slicker way of doing it, but I don't understand 
how it works, so I'll stick with the dumb-but-effective method.

> If I pass (x,y,z), the
> Python interpreter (Idle in my case) crashes, and this is
> understandable, since it results in the contents of x, y, and z
> being used as addresses to write to.  I've tried
> (id(x),id(y),id(z)), since id() is supposed to return the address of
> a Python object - Python didn't crash as before, but nothing was
> copied into x, y, and z, either.

id returns the address as a string. You wouldn't want this anyway. 
The Python int 0 is not a slot - it IS the one and only int object 
with value 0. If the foreign function were to return a 3 into the 
Python int (which it wouldn't - it would clobber something else in 
the struct) you would then have

>> 3 + 0
6

(The most commonly used ints are static singletons - try 
 sys.getrefcount(0))

- Gordon





More information about the Python-list mailing list