ctypes, function pointers and a lot of trouble

Matt mr.edel at gmx.at
Thu May 29 12:17:13 EDT 2008


Okay, thanks a lot for your reply Nick, I think you pushed me back on 
the right way.

Now I started with trying to implement the callback functions and am 
stuck at the following point:

I define my classes/structures/unions:

------------------------------CODE-----------------------------------------

class cdStream(Structure):
     _fields_ = [("contextH", c_uint),
                      ("open", c_void_p),
                      ("close", c_void_p),
                      ("read", c_void_p),
                      ("write", c_void_p),
                      ("seek", c_void_p),
                      ("tell", c_void_p)]

class memunion(Union):
     _fields_ = [("lpszFileName", c_char_p),
                      ("pStream", cdStream)]


class cdStgMedium(Structure):
     _fields_ = [("Type", c_uint),
                      ("u", memunion)]

------------------------------\CODE----------------------------------------

then i define my functions (right now I do just nothing) and at the same 
time I define the datatypes like they're listed in my C sample program:


------------------------------CODE-----------------------------------------

def pystreamopen (contextH, mode, pErr):
     pass

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

def pystreamclose (contextH, pErr):
     pass

cstreamclose = CFUNCTYPE(c_uint, c_uint)

def pystreamread (contextH, pBuf, pBufsize, pErr):
     pass

cstreamread = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

def pystreamtell (contextH, pErr):
     pass

cstreamtell = CFUNCTYPE(c_uint, c_uint)

def pystreamwrite (contextH, origin, offset, pErr):
     print "writing..."
     pass

cstreamwrite = CFUNCTYPE(c_uint, c_void_p, c_uint, c_uint)

------------------------------\CODE----------------------------------------

and now the problem starts: i want the pointers in the cdStream 
Structure point at my functions and tried to do it the following way:


------------------------------CODE-----------------------------------------

data = cdStgMedium()
data.type = 0
data.u.pStream.contextH = c_uint(3) #must be some kind of identifier.
data.u.pStream.open = cstreamopen(pystreamopen)
data.u.pStream.close = cstreamclose(pystreamclose)
data.u.pStream.write = cstreamwrite(pystreamwrite)
data.u.pStream.tell = cstreamtell(pystreamtell)
data.u.pStream.read = cstreamread(pystreamread)

------------------------------\CODE----------------------------------------

unfortunately that doesn't work because Python returns a TypeError: 
incompatible types, CFunctionType instance instead of c_void_p instance

Any ideas/help (please)?

Best regards,
Matt





More information about the Python-list mailing list