Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > C Callback Function using ctypes

Reply
Thread Tools

C Callback Function using ctypes

 
 
OJ
Guest
Posts: n/a
 
      03-26-2011
Hi I am opening a shared library which has defined the following
callback prototype:
extern void DebugMessage(int level, const char *message, ...);

My implementation in Python looks like this:
DEBUGFUNC = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.POINTER(ctypes.c_char))
def DebugMessage(lvl, msg):
print lvl, msg
return
debug_callback = DEBUGFUNC(DebugMessage)

Which gives me the following when running my python script:
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>
0 <ctypes.LP_c_char object at 0x7f872d5148c0>

How can I get something useful?

If I change the print to:
print lvl, msg[0], it produces an Segfault
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      03-26-2011
On Sat, 26 Mar 2011 05:23:27 +0300, OJ wrote:

> Hi I am opening a shared library which has defined the following
> callback prototype:
> extern void DebugMessage(int level, const char *message, ...);
>
> My implementation in Python looks like this:
> DEBUGFUNC = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.POINTER(ctypes.c_char))
> def DebugMessage(lvl, msg):
> print lvl, msg
> return
> debug_callback = DEBUGFUNC(DebugMessage)
>
> Which gives me the following when running my python script:
> 0 <ctypes.LP_c_char object at 0x7f872d5148c0>


> How can I get something useful?


1. Use c_char_p instead of POINTER(c_char).
2. Use msg.value to obtain a Python string from the pointer.

 
Reply With Quote
 
 
 
 
OJ
Guest
Posts: n/a
 
      03-26-2011
On 26 March 2011 12:55, OJ <(E-Mail Removed)> wrote:
>> 1. Use c_char_p instead of POINTER(c_char).
>> 2. Use msg.value to obtain a Python string from the pointer.
>>

>
> If I change to c_char_p, my program segfaults.
>
> If I use msg.value, I get this error message:
> Traceback (most recent call last):
> *File "_ctypes/callbacks.c", line 295, in 'calling callback function'
> *File "./core.py", line 54, in DebugMessage
> * *print lvl, msg.value
> AttributeError: 'LP_c_char' object has no attribute 'value'
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocating memory to pass back via ctypes callback function Scott Python 1 06-19-2009 11:14 AM
WindowsXP/ CTypes - How to convert ctypes array to a string? dudeja.rajat@gmail.com Python 0 08-19-2008 10:20 AM
ctypes: Setting callback functions in C libraries sjdevnull@yahoo.com Python 1 01-29-2007 11:07 PM
callback for ctypes James Hu Python 3 11-02-2005 01:27 AM
RE: [ctypes-users] [Ann] ctypes 0.9.0 released Henk Punt Python 0 07-23-2004 10:34 PM



Advertisments