ctypes, function pointers and a lot of trouble

Matt mr.edel at gmx.at
Mon Jun 2 11:00:37 EDT 2008


Hi,

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 
Structures:

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

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

typedef    struct    tagFilStreamData
{
     cdChar                        szFileName[MAX_PATH];
     HANDLE                        hFile;
}FilStreamData;

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

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

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

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;
}


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



Now my solution is the following:


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

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)]


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

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

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

     datainfo = cdReleaseImageInfo()

     databuf = c_char * 100000
     cbuffer=MemStreamData()
     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.contextH=cpointer
     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)

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

Does anyone see where the errors are?

Best regards and thanks a lot,
Matt









More information about the Python-list mailing list