#1 add support for accessing functions via pointer

closed
nobody
None
5
2005-03-17
2004-09-30
Patrick Mueller
No

I have a requirement to be able to call functions that
I can only get to via pointer. ctypes itself does not
appear to support this, but _ctypes does. I ended up
doing something like below, which appears to work fine.
I'd like to see first class support for this in ctypes
itself. Would also need to support the Windows variant
as well (but probably not the Python one).

import ctypes
import _ctypes

#--------------------------------------------------------------------
#
#--------------------------------------------------------------------
class FunctionPtr(_ctypes.CFuncPtr):
_flags_ = _ctypes.FUNCFLAG_CDECL
_restype_ = ctypes.c_int

#--------------------------------------------------------------------
#
#--------------------------------------------------------------------
functionPtr = 0 # get a pointer to the function somehow

function = FunctionPtr(functionPtr)

result = function(arg1,arg2)

Discussion

  • Thomas Heller
    Thomas Heller
    2005-03-17

    • status: open --> closed
     
  • Thomas Heller
    Thomas Heller
    2005-03-17

    Logged In: YES
    user_id=11105

    Sorry, but I won't change this. CFuncPtr is an
    implementation detail of ctypes internals. You are of
    course allowed to use it at your own risk. , but I won't
    document it and I reserve the right to change it ;-)

    Wait (after looking into the code) - it seems you can use
    prototype = CFUNCTYPE(c_int, None)
    and then
    function = prototype(<pointer to function as integer>)

    for the same effect. Still undocumented, though.