ctypes, function pointers and a lot of trouble

Matt mr.edel at gmx.at
Tue Jun 3 18:28:04 CEST 2008

> The docs say CFUNCTYPE(restype, *argtypes), so:
> cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)
> is saying that the result type is c_uint, not void. I think you need:
> cstreamopen = CFUNCTYPE(None, c_uint, c_ushort, c_uint)
> instead.

Hm, thanks, now I can access my data in the functions and also write 
them but the program keeps terminating right at the point when the 
"open" function finishes. Unfortunately everything closes and I get no 
error messages.

I did some additional work in the meantime and changed my code so it has 
the correct datatypes now:


def pystreamopen (contextH, mode, pErr):
     print "opening..."
     print contextH.contents.dwBufferSize #just to check the structure
     print mode 	#tells about what the DLL wants to do with this stream
     contextH.contents.mode = c_byte(5) #5=Permission to read and write
     contextH.contents.lPos = c_uint(0) #start position

     print pErr.contents
     pErr.contents = c_uint(0)

cstreamopen = CFUNCTYPE(None, POINTER(MemStreamData), c_ushort, 

def pystreamclose (contextH, pErr):
     print "closing..."

cstreamclose = CFUNCTYPE(None, POINTER(MemStreamData), POINTER(c_uint))

def pystreamread (contextH, pBuf, pBufsize, pErr):
     print "reading..."

cstreamread = CFUNCTYPE(None, POINTER(MemStreamData), c_uint, c_uint, 

def pystreamtell (contextH, pErr):
     print "telling...



and the function call:


errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct), 
c_uint(0), c_uint(0), byref(datainfo), POINTER(cdStgMedium)(data))


while it's running my program prints the following and then 

 >>> opening...990001
 >>> 2
 >>> c_ulong(0L)
 >>> Script terminated.  ##This is a message by my editor (SPE)

I also tried different editors, but get the same result...

Anyway, meanwhile  decided to try a different approach. Maybe I have 
more luck by having the function write the data directly into a file on 
the HDD.
Doe anyone know how to translate the following into Python/ctypes?

I googled quite a lot before but all topic-related I found was my own 
posting here in this NG :S


pFilStrm->hFile = CreateFile(	pFilStrm->szFileName,							 
dwDesiredAccess,								dwShareMode,									NULL,									 
dwCreationDisposition,								FILE_ATTRIBUTE_NORMAL,								NULL );


This function is obviously a default C-function that generates a file 
and returns a c-handle to that file. In my case I'd need exactly such a 
handle to get things working...

Furthermore there's a writefile function I'd need to translate too:


WriteFile( pFilStrm->hFile, pBuf, dwNumberOfBytesToWrite, pBufsize, NULL );



Thanks and best wishes,

More information about the Python-list mailing list