ctypes, function pointers and a lot of trouble

Matt mr.edel at gmx.at
Mon Jun 2 17:00:37 CEST 2008


okay, thanks now my DLL function seems to accept the functions given, 
but instead of running my program keeps crashing (with windows address 
violation errors). I did some further investigations on that and figured 
out that the contextH variable is not just an identifier as I thought, 
but a quite complicated datastreamhandler.

Now to get it to work I additionally need to implement the following c 


/* The internal data structure object of a stream */
typedef    struct    tagMemStreamData
     cdChar                        mode;
     cdInt32                        lPos;
     cdUInt32                    dwVisibleSize;
     cdUInt32                    dwBufferSize;
     cdChar                        *cpBuffer;

typedef    struct    tagFilStreamData
     cdChar                        szFileName[MAX_PATH];
     HANDLE                        hFile;


This function just creates the pStream filestream (which is what I have 
to do too)


BOOL    CreateMyFilStream(    cdStream    *pStream,
                             cdChar        *szFileName )
     FilStreamData    *pFilStrm;

     /* The domain for data is secured. */
     pFilStrm = new FilStreamData;
     if( pFilStrm == NULL )
         return    FALSE;

     /* Data is changed the first stage. */
     pFilStrm->hFile = INVALID_HANDLE_VALUE;
     strcpy( pFilStrm->szFileName, szFileName );

     pStream->contextH = (cdContext)pFilStrm;
     pStream->close = _CloseMyFilStream;
     pStream->open = _OpenMyFilStream;
     pStream->read = _ReadMyFilStream;
     pStream->seek = _SeekMyFilStream;
     pStream->tell = _TellMyFilStream;
     pStream->write = _WriteMyFilStream;

     return    TRUE;


Now my solution is the following:


class MemStreamData(Structure):
     _fields_ = [("mode", c_byte),
                      ("lPos", c_uint),
                      ("dwVisibleSize", c_uint),
                      ("dwBufferSize", c_uint),
                      ("cpBuffer", POINTER(c_char))]

class FilStreamData (Structure):
     _fields_ = [("szFileName", c_char * 30),
                      ("hFile", c_uint)]


and the code to fill all that with the right data is the following:


     datainfo = cdReleaseImageInfo()

     databuf = c_char * 100000
     cbuffer.mode = c_byte(0)
     cbuffer.lPos = c_uint(0)
     cbuffer.dwVisibleSize = c_uint(100000)
     cbuffer.dwBufferSize = c_uint(100000)

#this line does not work, wrong datatype!?
     cbuffer.cpBuffer = POINTER(databuf)

     cpointer = cast(cbuffer, POINTER(c_uint))
     stream = cdStream()
     stream.open = cstreamopen(pystreamopen)
     stream.close = cstreamclose(pystreamclose)
     stream.write = cstreamwrite(pystreamwrite)
     stream.tell = cstreamtell(pystreamtell)
     stream.read = cstreamread(pystreamread)

     data = cdStgMedium()
     data.Type = c_uint(1)
     data.u.pStream = POINTER(cdStream)(stream)


Does anyone see where the errors are?

Best regards and thanks a lot,

More information about the Python-list mailing list