473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 4371
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
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

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

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. In the Delphi(5) code: type TCallbackFunc =...
1
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...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
0
by: Sanghyeon Seo | last post by:
ctypes is a popular CPython extension planned for inclusion in Python 2.5. It is a foreign function interface library. Homepage: http://starship.python.net/crew/theller/ctypes/ Documentation:...
1
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out how to set values to a variable inside the library. ...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
3
by: waldek | last post by:
Hi, I have module A.py and B.dll which exports C functions by cdecl_ In A.py I pass callback (py callable) to dll. Next, thread inside dll simply calls my callback (in a loop). After few secs...
3
by: waldek | last post by:
Hi, I'm trying to handle data passed to Py Callback which is called from C dll. Callback passes data to another thread using Queue module and there the data are printed out. If data is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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...

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.