C Structure rebuild with ctypes
Mark Tolonen
metolone+gmane at gmail.com
Sat Jan 9 00:28:21 EST 2010
"Georg" <nobody at nowhere.org> wrote in message
news:7qo9avFivmU1 at mid.individual.net...
> Hi Mark,
>
>> Are you passing in these values, or are they being returned? To me the
>> depth of the pointer references implies numVars, varNames, and varTypes
>> are out parameters. I'll assume that for now. If they are in/out
>> parameters let me know.
>
> If these parameters were in parameters. What would I have to do different?
If you are passing in the values, you can remove a level of * referencing,
since the parameters aren't being modified. Note the syntax to create an
array: (type * length)(initializers...):
--------------- func.c --------------------
#include <stdio.h>
__declspec(dllexport) void func(int numVars, char **varNames, int *varTypes)
{
int i;
printf("numVars = %d\n",numVars);
for(i = 0; i < numVars; i++)
printf("%d: %s\n",varTypes[i],varNames[i]);
}
--------------- 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)
--------------- OUTPUT ---------------
numVars = 3
1: abc
2: def
3: ghi
--------------------------------------------
-Mark
More information about the Python-list
mailing list