I'm trying to register a Python function for callback in a DLL written in C++ and I can't figure out how to do it.
My C++ code header looks like:
extern "C" {
__declspec(dllexport) void RegisterFunc((void (*)());
}
And the implementation is:
void (*funcCallback)() = NULL;
__declspec(dllimport) void RegisterFunc(void(*func)())
{
funcCallback = func;
}
My Cython pyx file looks like:
cdef extern void RegisterFunc(void (*)())
def PyRegisterFunc(func):
RegisterFunc(func)
When compiled this gives the error message: Cannot convert Python object to 'void (*)(void)'
If I change the code to:
def PyRegisterFunc(func):
RegisterFunc(<void (*)()>func)
I get the error message: Python objects cannot be cast to pointers of primitive types
I've also tried &func and get the error message: Cannot take address of Python variable.
What is the correct way of passing this Python function as a pointer to my C code?
UPDATE: I should mention my intended purpose for this Python function. The Python application is written in PyQt and the function will emit a signal. The DLL itself has no knowledge of Qt and is compiled without the Qt libraries. A method in the DLL will call this function indirectly which will emit a signal to the Python application.
请参阅 Cython 文档: 使用来自 C 的 Cython 声明
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.