C Callback Function using ctypes

Nobody nobody at nowhere.com
Sat Mar 26 05:43:43 EDT 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.




More information about the Python-list mailing list