473,511 Members | 16,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6853
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2563
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
2396
by: Martin P. Hellwig | last post by:
Hey all, I'd like to wrap libpam so that I can use that for authentication and password management. I build ctypes (0.9.9.6) on my platform via ports. Now according to OpenPAM documentation...
12
10077
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
0
1279
by: Oliver Andrich | last post by:
Hi everybody, I am currently playing around with ctypes and a C library (libWand from ImageMagick), and as I want to easily deploy it on Mac, Linux and Windows, I prefer a ctypes solution over a...
7
6987
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
6
12721
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
5
4175
by: castironpi | last post by:
Hi all, I have a mmap and a data structure in it. I know the structure's location in the mmap and what structure it is. It has a ctypes definition. I want to initialize a ctypes object to...
2
1324
by: Sells, Fred | last post by:
Diez wrote... You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters that the c api uses. my ctypes code is below. It just quits running...
3
4511
by: patelss23 | last post by:
Hello All, I am dealing with this weird bug. I have a function in C and I have written python bindings for it using ctypes. I can call this function for couple of times and then suddenly it...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.