By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,712 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,712 IT Pros & Developers. It's quick & easy.

ctypes and reading value under pointer passed as param of a callback

P: n/a
Hi,

I'm using C dll with py module and wanna read value (buffer of bytes)
returned in py callback as parameter passed to dll function.

--------------------------------------------------
def mycallback(data, size):
# how to read data buffer here ?
return 0

cbfunc = CFUNCTYPE(c_int, POINTER(c_uint8), c_int)

mydll = cdll.somedll
mdll.foo(cbfunct)
---------------------------------------------------------------

Question: How to get bytes from the buffer passed to mycallback ???
Let's assume that the buffer consist of 4 bytes 4 bytes 8 bytes.

I tried unpack("ii8s", data[0]) and nothing. I tried different ctypes
passed to callback and nothing.

Any sugestions ?

Thanks,
Waldek
Jul 24 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
waldek schrieb:
Hi,

I'm using C dll with py module and wanna read value (buffer of bytes)
returned in py callback as parameter passed to dll function.
The callback receives a pointer instance. You can dereference the pointer
to read individual bytes in this way:
print data[0], data[5], data[42]
or use slicing to read a bunch of bytes:
print data[0:42]

So, you probably want something like this:
--------------------------------------------------
def mycallback(data, size):
# how to read data buffer here ?
print data[:size]
return 0

cbfunc = CFUNCTYPE(c_int, POINTER(c_uint8), c_int)

mydll = cdll.somedll
mdll.foo(cbfunct)
---------------------------------------------------------------

Question: How to get bytes from the buffer passed to mycallback ???
Let's assume that the buffer consist of 4 bytes 4 bytes 8 bytes.

I tried unpack("ii8s", data[0]) and nothing. I tried different ctypes
passed to callback and nothing.
Thomas
Jul 24 '08 #2

P: n/a
On Jul 24, 5:01 pm, Thomas Heller <thel...@python.netwrote:
waldekschrieb:
Hi,
I'm using C dll with py module and wanna read value (buffer of bytes)
returned in py callback as parameter passed to dll function.

The callback receives a pointer instance. You can dereference the pointer
to read individual bytes in this way:
print data[0], data[5], data[42]
or use slicing to read a bunch of bytes:
print data[0:42]

So, you probably want something like this:
--------------------------------------------------
def mycallback(data, size):
# how to read data buffer here ?

print data[:size]
return 0
cbfunc = CFUNCTYPE(c_int, POINTER(c_uint8), c_int)
mydll = cdll.somedll
mdll.foo(cbfunct)
---------------------------------------------------------------
Question: How to get bytes from the buffer passed to mycallback ???
Let's assume that the buffer consist of 4 bytes 4 bytes 8 bytes.
I tried unpack("ii8s", data[0]) and nothing. I tried different ctypes
passed to callback and nothing.

Thomas
Cool, works fine now.

.... and how to convert a list of bytes to py string or int ??

i.e
data[:4] -py int
data[4:8] -py int
data[8:] -py strng

I tried unpack and unpack_from but the buff should be an string or
buffer not a list of bytes as data is.

any suggestions?
Jul 25 '08 #3

P: n/a

"waldek" <wa*******************@gmail.comwrote in message
news:de**********************************@u12g2000 prd.googlegroups.com...
On Jul 24, 5:01 pm, Thomas Heller <thel...@python.netwrote:
>waldekschrieb:
Hi,
I'm using C dll with py module and wanna read value (buffer of bytes)
returned in py callback as parameter passed to dll function.

The callback receives a pointer instance. You can dereference the
pointer
to read individual bytes in this way:
print data[0], data[5], data[42]
or use slicing to read a bunch of bytes:
print data[0:42]

So, you probably want something like this:
--------------------------------------------------
def mycallback(data, size):
# how to read data buffer here ?

print data[:size]
return 0
cbfunc = CFUNCTYPE(c_int, POINTER(c_uint8), c_int)
mydll = cdll.somedll
mdll.foo(cbfunct)
---------------------------------------------------------------
Question: How to get bytes from the buffer passed to mycallback ???
Let's assume that the buffer consist of 4 bytes 4 bytes 8 bytes.
I tried unpack("ii8s", data[0]) and nothing. I tried different ctypes
passed to callback and nothing.

Thomas

Cool, works fine now.

... and how to convert a list of bytes to py string or int ??

i.e
data[:4] -py int
data[4:8] -py int
data[8:] -py strng

I tried unpack and unpack_from but the buff should be an string or
buffer not a list of bytes as data is.

any suggestions?
Try:

cbfunc = CFUNCTYPE(c_int, c_char_p, c_int)

ctypes understands c_char_p as a null-terminated character string and will
pass a string to your callback instead of a list of ints.

--Mark

Jul 25 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.