ctypes, function pointers and a lot of trouble

Nick Craig-Wood nick at craig-wood.com
Wed May 28 11:30:18 EDT 2008


Matt <mr.edel at gmx.at> wrote:
>  Okay so well, I have quite a problem right now with a file stream. What 
>  I am doing is to use the Cannon SDK dlls to get control over my old 
>  Cannon A60 Camera for some surveillance useage.
> 
>  By using ctypes it all worked well until now. I am able to load the 
>  dlls, use a lot of functions, am able to connect to the camera, read out 
>  some params, send commands etc... but now I am stuck with reading the 
>  picture data.
> 
>  In C the code looks as follows (found in an supplemental *.h SDK file:
[snip]
>  So, since I'm no C-Professional, I can only guess what that code does. 
>  With some previous commands I tell the camera to make a picture. This 
>  picture is then automatically moved to the PCs RAM and with the function 
>  above (CDGetReleasedData) I should be able to access this stream. Now I 
>  havn't accessed any stream with ctypes yet so I have only a rough idea 
>  how it could work.
> 
>  The following are the relevant parts of my code that don't work:
> 
> 
>  # Definitions:
> 
>  class cdReleaseImageInfo(Structure):
>       _fields_ = [("SequenceID", c_uint),
>                        ("DataType", c_uint),
>                        ("Format", c_ubyte),
>                        ("DataSize", c_uint),
>                        ("Filename", c_char * 2)]
> 
> 
>  class cdStream(Structure):
>       _fields_ = [("contextH", c_uint),
>                        ("open", c_uint),
>                        ("close", c_uint),
>                        ("read", c_uint),
>                        ("write", c_uint),
>                        ("seek", c_uint),
>                        ("tell", c_uint)]

These c_uints would be better as c_void_p at minimum

Howerver I suspect you are going to have to implement these callback
functions - read this section of the manual

  http://docs.python.org/lib/ctypes-callback-functions.html

I suspect it will call back your functions for the given
functionality, eg "open", "close" etc...

>  class memunion(Union):
>       _fields_ = [("lpszFileName", c_char),

This should be c_char_p I suspect...

>                        ("pStream", cdStream)]

and this should be POINTER(cdStream)

>  class cdStgMedium(Structure):
>       _fields_ = [("Type", c_uint),
>                        ("u", memunion)]
> 
> 
> 
> 
>  # command:
> 
>  datainfo = cdReleaseImageInfo()
>  data = cdStgMedium()
>  errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct), 
>  c_uint(1), c_uint(1), byref(datainfo), byref(data))
> 
>  The function "cdsdk.CDGetReleasedData" itself gets executed correctly, 
>  but returns an "Invalid Parameter" errorcode. there's also no useful 
>  data whereas datainfo gets written correctly. I know that my cdStream 
>  can't work, facing the C-code, but what'd be the right cdStream class? 
>  What can I do? Any ideas?

I've noted some obvious problems above.

To get this to work will require some C knowledge.  If they supply
some example C code I'd work through that translating it line by line
to python+ctypes.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list