C Callback Function using ctypes

Discussion in 'Python' started by OJ, Mar 26, 2011.

  1. OJ

    OJ Guest

    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
     
    OJ, Mar 26, 2011
    #1
    1. Advertising

  2. OJ

    Nobody Guest

    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.
     
    Nobody, Mar 26, 2011
    #2
    1. Advertising

  3. OJ

    OJ Guest

    On 26 March 2011 12:55, OJ <> 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'
    >
     
    OJ, Mar 26, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    425
    Henk Punt
    Jul 23, 2004
  2. James Hu

    callback for ctypes

    James Hu, Nov 1, 2005, in forum: Python
    Replies:
    3
    Views:
    406
    Peter Hansen
    Nov 2, 2005
  3. Replies:
    0
    Views:
    539
  4. Scott
    Replies:
    1
    Views:
    453
    Scott
    Jun 19, 2009
  5. jamadagni
    Replies:
    8
    Views:
    180
    Dan Stromberg
    May 26, 2013
Loading...

Share This Page