The only example of getting called from C language in Python is the example that returns the int type of qsort. It seems that there was a way to return a difficult Windows structure that couldn't be found.
CBFUNC=CFUNCTYPE(c_char_p,c_char_p)
cbfunc=CBFUNC(py_cb_func)
Normally
msg = "Hello, Trusterd.this is Python."
buf = create_string_buffer(msg.encode('UTF-8'))
return buf
Then
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 302, in 'converting callback result'
TypeError: bytes or integer address expected instead of c_char_Array_32 instance
Exception ignored in: <function py_cb_func at 0x1041dd510>
Although the return value can be received on the C language side, I couldn't receive the intended string.
msg = "Hello, Trusterd.this is Python."
buf = create_string_buffer(msg.encode('UTF-8'))
c = cast(buf, POINTER(c_char))
return addressof(c.contents)
With this, the intended character string can be returned to the C language side.
By the way, the specification of the argument when calling the normal C language side is
hogefunc(create_string_buffer(msg.encode('UTF-8')))
You can do it without any problem.
-I tried ctypes with Blender on OSX
Recommended Posts