C Structure rebuild with ctypes

Georg nobody at nowhere.org
Wed Jan 27 22:17:08 CET 2010


Hi Mark,

many thanks for your hints.

> --------------- func.py ------------------
> import ctypes as c
>
> # int func (int numVars, char **varNames, int *varTypes)
>
> INT = c.c_int
> PINT = c.POINTER(INT)
> PCHAR = c.c_char_p
> PPCHAR = c.POINTER(PCHAR)
>
> func = c.CDLL('func').func
> func.restype = None
> func.argtypes = [INT,PPCHAR,PINT]
>
> numVars = 3
> varNames = (PCHAR * numVars)('abc','def','ghi')
> varTypes = (INT * numVars)(1,2,3)
>
> func(numVars,varNames,varTypes)

The function to call has the following signiture:

int SetVarLabel(int handle, const char *varName, const char *varLabel)

I did as you suggested by doing the following:

INT = c_int
PCHAR = c_char_p

libc.argtypes = [INT,PCHAR,PCHAR]

vn = create_string_buffer("var1")
vl = create_string_buffer("Label")

print "vn: ", vn.value
print "vl: ", vl.value

returnCode = libc.SetVarLabel(h,byref(vn), byref(vl))
print "Return Code: ", returnCode

The return code is always "variable not found". Thus my call to the C 
library is not working.

What do I do wrong?

Best regards

Georg





More information about the Python-list mailing list