Related to my other question
How can I get the address (acctual function pointer) to a CFuncType object? addressof() does not report the correct address.
C code:
extern "C" _declspec(dllexport)
int addr(int (*func)())
{
int r = (int)func;
return r;
}
Python code:
def test():
return 42
t = CFUNCTYPE(c_int)
f = t(test)
print addressof(f)
print dll.addr(f)
Output:
7030864
3411932
trying to call *(7030864) from C causes a crash, but calling *(3411932) works as expected. What's wrong with addressof()?
ctypes — A foreign function library for Python¶. ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
ctypes. CFUNCTYPE (restype, *argtypes, use_errno=False, use_last_error=False) ¶ The returned function prototype creates functions that use the standard C calling convention.
Python integers and Python longs are passed as the platforms default C int type, their value is masked to fit into the C type. Before we move on calling functions with other parameter types, we have to learn more about ctypes data types. ctypes defines a number of primitive C compatible data types :
ctypes allows creating C callable function pointers from Python callables. These are sometimes called callback functions. First, you must create a class for the callback function. The class knows the calling convention, the return type, and the number and types of arguments this function will receive.
cast(f, c_void_p)
gets the correct address from within python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With