Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: converting strings for use with ctypes.c_void_p()

given a string:

msg="hello world"

How can I define this as a ctypes.c_void_p() data type?

the following code yields a "cannot be converted to pointer" exception:

data=ctypes.c_void_p(msg)

data is required to be a void* type in C, because it is being passed to a DLL.

I'm assuming there is a way to pack/unpack the string using the struct package, but unfortunately I am very unfamiliar with this process.

like image 513
Thedaego Avatar asked Nov 25 '08 16:11

Thedaego


1 Answers

Something like this? Using ctypes.cast?

>>> import ctypes
>>> p1= ctypes.c_char_p("hi mom")
>>> ctypes.cast( p1, ctypes.c_void_p )
c_void_p(11133300)
like image 142
S.Lott Avatar answered Oct 12 '22 02:10

S.Lott