469,898 Members | 1,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Working with ctypes and char** data type

I'm a bit of a python newbie and I need to wrap a C library.

I can initialise the library using CDLL('mcclient.so')

and I can call functions correctly inside the library but I need to
invoke one function which has this function definition:

char ** CAPAPI McSearch(HMCLINK Handle,
short nSearchType,
short nNoSelect,
char **pSelect,
short nNoWhere,
char **pWhere,
char **pData,
int iTimeout);

For **pSelect I want to pass in an array of char points, which in C
would be declared as

char *pData[] = { "ADDR", "POSTCODE" };

Can someone tell me how use pointers + char pointers together in
python with ctypes please?

Thank you

Phill
Jul 24 '08 #1
2 6100
Philluminati schrieb:
I'm a bit of a python newbie and I need to wrap a C library.

I can initialise the library using CDLL('mcclient.so')

and I can call functions correctly inside the library but I need to
invoke one function which has this function definition:

char ** CAPAPI McSearch(HMCLINK Handle,
short nSearchType,
short nNoSelect,
char **pSelect,
short nNoWhere,
char **pWhere,
char **pData,
int iTimeout);

For **pSelect I want to pass in an array of char points, which in C
would be declared as

char *pData[] = { "ADDR", "POSTCODE" };

Can someone tell me how use pointers + char pointers together in
python with ctypes please?
# create an array that holds two pointers to 'char *', and fill it with data:
pData = (c_char_p * 2)()
pData[0] = "ADDR"
pData[1] = "POSTCODE"

# Another way:
pData = (c_char_p * 2)("ADDR", "POSTCODE")

Thomas
Jul 24 '08 #2
On Jul 24, 4:03*pm, Thomas Heller <thel...@python.netwrote:
Philluminati schrieb:
I'm a bit of a python newbie and I need to wrap a C library.
I can initialise the library using CDLL('mcclient.so')
and I can call functions correctly inside the library but I need to
invoke one function which has this function definition:
char ** CAPAPI McSearch(HMCLINK Handle,
* * * * * * * * * * * * short nSearchType,
* * * * * * * * * * * * short nNoSelect,
* * * * * * * * * * * * char **pSelect,
* * * * * * * * * * * * short nNoWhere,
* * * * * * * * * * * * char **pWhere,
* * * * * * * * * * * * char **pData,
* * * * * * * * * * * * int iTimeout);
For **pSelect I want to pass in an array of char points, which in C
would be declared as
char *pData[] = { "ADDR", "POSTCODE" };
Can someone tell me how use pointers + char pointers together in
python with ctypes please?

# create an array that holds two pointers to 'char *', and fill it with data:
pData = (c_char_p * 2)()
pData[0] = "ADDR"
pData[1] = "POSTCODE"

# Another way:
pData = (c_char_p * 2)("ADDR", "POSTCODE")

Thomas
Thank you Thomas for your reply. I also got it working in a slightly
less sophisticated manor like this:

SelectType = c_char_p * 2
select = SelectType("ADDR", "POSTCODE")
ptr = pointer(select)

Anyway, thanks for taking the time to reply!

Phill
Jul 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Heller | last post: by
3 posts views Thread by Martin P. Hellwig | last post: by
12 posts views Thread by p.lavarre | last post: by
reply views Thread by Oliver Andrich | last post: by
6 posts views Thread by Jack | last post: by
5 posts views Thread by castironpi | last post: by
2 posts views Thread by Sells, Fred | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.