To return char * in a callback function using ctypes in Python

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.

This callback type

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.

The result of trial and error

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.

Related article

-I tried ctypes with Blender on OSX

Recommended Posts

To return char * in a callback function using ctypes in Python
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
To execute a Python enumerate function in JavaScript
Create a function in Python
Use callback function in Python
How to execute a command using subprocess in Python
[Python] How to call a c function from python (ctypes)
How to unit test a function containing the current time using freezegun in python
A memo of writing a basic function in Python using recursion
I tried to make a stopwatch using tkinter in python
Try running a function written in Python using Fn Project
Precautions when pickling a function in python
Log in to Slack using requests in Python
How to get a stacktrace in python
Draw a tree in Python 3 using graphviz
How to generate a new loggroup in CloudWatch using python within Lambda
How to get a value from a parameter store in lambda (using python)
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
Try to calculate a statistical problem in Python
Automatically register function arguments to argparse in Python
How to clear tuples in a list (Python)
Get the caller of a function in Python
I want to create a window in Python
How to create a JSON file in Python
A clever way to time processing in Python
Steps to develop a web application in Python
View drug reviews using a list in Python
Get pointers to struct members in Python ctypes
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Create a MIDI file in Python using pretty_midi
How to Mock a Public function in Pytest
[Python] Smasher tried to make the video loading process a function using a generator
Parse a JSON string written to a file in Python
Create a data collection bot in Python using Selenium
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Try to make a Python module in C language
How to set up a Python environment using pyenv
Function to return multi columns to single column in DataFrame
(Python) Try to develop a web application using Django
I want to write in Python! (2) Let's write a test
[Road to Python Intermediate] Define __getattr__ function in class
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
I tried to implement a pseudo pachislot in Python
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
I want to work with a robot in python.
Things to note when initializing a list in Python
How to make a Python package using VS Code
Convert / return class object to JSON format in Python
How to exit when using Python in Terminal (Mac)
Consider a conversion from a Python recursive function to a non-recursive function
Introduction to Linear Algebra in Python: A = LU Decomposition
[Python] Created a method to convert radix in 1 second