Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cython:在DLL中注册用于回调的Python函数_Python_C++_Cython - Fatal编程技术网

Cython:在DLL中注册用于回调的Python函数

Cython:在DLL中注册用于回调的Python函数,python,c++,cython,Python,C++,Cython,我试图在C++中编写的DLL中登记一个Python函数,但我不能理解如何做。 < >我的C++代码头看起来像: extern "C" { __declspec(dllexport) void RegisterFunc((void (*)()); } cdef extern void RegisterFunc(void (*)()) def PyRegisterFunc(func): RegisterFunc(func) 具体实施如下: void (*funcCallbac

我试图在C++中编写的DLL中登记一个Python函数,但我不能理解如何做。 < >我的C++代码头看起来像:

extern "C" {
    __declspec(dllexport) void RegisterFunc((void (*)());
 }
cdef extern void RegisterFunc(void (*)())

def PyRegisterFunc(func):
    RegisterFunc(func)
具体实施如下:

void (*funcCallback)() = NULL;
__declspec(dllimport) void RegisterFunc(void(*func)())
{
    funcCallback = func;
}
我的Cython pyx文件如下所示:

extern "C" {
    __declspec(dllexport) void RegisterFunc((void (*)());
 }
cdef extern void RegisterFunc(void (*)())

def PyRegisterFunc(func):
    RegisterFunc(func)
编译时,会显示错误消息: 无法将Python对象转换为“void(*)(void)”

如果我将代码更改为:

def PyRegisterFunc(func):
    RegisterFunc(<void (*)()>func)
def PyRegisterFunc(func):
注册表函数(func)
我收到错误消息: Python对象不能转换为基元类型的指针

我还尝试了&func,得到了错误消息:cannottakeaddressofpython变量

将此Python函数作为指向我的C代码的指针传递的正确方法是什么


更新:我应该提到这个Python函数的预期用途。Python应用程序是用PyQt编写的,函数将发出一个信号。DLL本身不了解Qt,在编译时没有Qt库。DLL中的一个方法将间接调用此函数,该函数将向Python应用程序发出信号。

请参阅Cython文档:

我认为您的问题在于C中的回调函数原型与Python风格的函数原型(具有类似argc/argv的机制)不匹配。我从来没有使用过cython,我总是直接链接python库。如果你真的想强制转换一个可调用c函数指针的任意python,那么你不能在cython中这样做(但你可以使用ctypes/cffi)。如果您有一个特定的函数要使用,请在Cython中使用cdef/cpdef。文档中有可能有用的部分回调。这似乎是一个仅链接的答案。