C Callback Function using ctypes

OJ olejl77 at gmail.com
Fri Mar 25 22:23:27 EDT 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



More information about the Python-list mailing list