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:

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

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, 
POINTER(c_uint))

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

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

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

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

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

.
.
.etc...

--------------------------/CODE--------------------------------------------

and the function call:

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

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

--------------------------/CODE--------------------------------------------


while it's running my program prints the following and then 
exits/terminates:

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

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

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


--------------------------/CODE--------------------------------------------

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:

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

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

--------------------------/CODE--------------------------------------------


Ideas?

Thanks and best wishes,
Matt



More information about the Python-list mailing list