I'm trying to wrap a C function that requires a C function pointer as argument. I've wrote a pyrex extension with a dictionary where every key is a C function name, and every entry is a C function pointer, stored as a long. I call the C function from python passing the function name and the necessary data.<br>
<br>An example:<br><br>-------- c_funcs.h ---------<br><br>int returnFoo(int);<br><br>int returnBar(int*);<br><br>char* returnFooBar(char*);<br><br>int callFoo(int(*)(int), int);<br><br>int callBar(int(*)(int*), int*);<br>
<br>char* callFooBar(char*(*)(char*), char*);<br><br><br>------- c_funcs.c -------<br>#include <c_funcs.h><br><br>int returnFoo(int foo){<br> return foo;<br>}<br><br>int returnBar(int* bar){<br> if (bar)<br> return *bar;<br>
}<br><br>char* returnFooBar(char* fooBar){<br> if (fooBar)<br> return fooBar;<br>}<br><br>int callFoo(int(*func)(int), int foo){<br> return (*func)(foo);<br>}<br><br>int callBar(int(*func)(int*), int* bar){<br> return (*func)(bar);<br>
}<br><br>char* callFooBar(char*(*func)(char*), char* FooBar){<br> return (*func)(FooBar);<br>}<br><br>------- funcs.pyx ----------<br>cdef extern from "c_funcs.h":<br> int callFoo(int(*)(int), int)<br> int callBar(int(*)(int*), int*)<br>
char* callFooBar(char*(*)(char*), char*)<br><br>cdef class funcs:<br> cdef object funcsDict<br> def __init__(self):<br> cdef unsigned long fooPointer<br> fooPointer = <unsigned long>&callFoo<br> self.funcsDict = {}<br>
self.funcsDict['returnFoo'] = fooPointer<br> # Here the python interpreter prints an integer, not a long as expected!!!<br> print self.funcsDict['returnFoo']<br> <br> def pyCallFoo(self, foo):<br>
# The call to C function produces a Segmentatio Fault<br> return callFoo(<int(*)(int)>self.funcsDict['returnFoo'], foo)<br><br>How can I solve the problem? Every advice about alternative implementation is also appreciated :)<br>
<br><br>