CTYPES structure passing

Peter West peter at 4doptics.com
Thu Jun 3 06:54:13 EDT 2010


Hi

I'm hoping someone here can tell me what I'm doing wrong as I've spent
the best part of three days reading docs and trying code.

I want to port a windows DLL that controls a USB camera to Python
using C types.  I've happily converted a lot of the functions but am
stuck with one vital function that requires a structure to be passed
to the DLL for filling with data.

I have the C declarations

CAM_API BOOL LUCAM_EXPORT LucamGetFormat(HANDLE hCamera,
LUCAM_FRAME_FORMAT *pFormat, FLOAT *pFrameRate);

where the LUCAM_FRAME_FORMAT is defined as

typedef struct {
   ULONG xOffset; 	// x coordinate on imager of top left corner of
subwindow in pixels
   ULONG yOffset; 	// y coordinate on imager of top left corner of
subwindow in pixels
   ULONG width; 	// width in pixels of subwindow
   ULONG height; 	// height in pixels of subwindow
   ULONG pixelFormat; // pixel format for data
   union
   {
      USHORT subSampleX;	// sub-sample ratio in x direction in pixels
(x:1)
      USHORT binningX;	// binning ratio in x direction in pixels (x:1)
   };
   USHORT flagsX; // LUCAM_FRAME_FORMAT_FLAGS_*
   union
   {
      USHORT subSampleY;  // sub-sample ratio in y direction in pixels
(y:1)
      USHORT binningY;	// binning ratio in y direction in pixels (y:1)
   };
   USHORT flagsY; // LUCAM_FRAME_FORMAT_FLAGS_*
} LUCAM_FRAME_FORMAT;

In my Python code I have

#------------- Frame format --------------------
class FRAME_FORMAT_UNION(Union):
    __fields__ = [("subSample", c_ushort),  #  sub-sample ratio in x
direction in pixels (x:1)
                 ("binning", c_ushort )]    #  binning ratio in x
direction in pixels (x:1)


class LUCAM_FRAME_FORMAT(Structure):
   __fields__ = [( "xOffset", c_ulong),  # x coordinate on imager of
top left corner of subwindow in pixels
                ( "yOffset", c_ulong),   # y coordinate on imager of
top left corner of subwindow in pixels
                ( "width", c_ulong),     # width in pixels of
subwindow
                ( "height", c_ulong),    # height in pixels of
subwindow
                ( "pixelFormat", c_ulong), #pixel format for data
                ( "XUnion", FRAME_FORMAT_UNION),
                ( "flagsX", c_ushort),   # LUCAM_FRAME_FORMAT_FLAGS_*
                ( "YUnion", FRAME_FORMAT_UNION),
                ( "flagsY", c_ushort)]

LP_FRAME_FORMAT = POINTER(LUCAM_FRAME_FORMAT)

and make the call like this

   FrameRate = c_float(0)
   FrameFormat = LUCAM_FRAME_FORMAT()

   FrameFormat.xOffset = 0
   FrameFormat.yOffset = 0
   FrameFormat.width = 0
   FrameFormat.height = 0
   FrameFormat.pixelFormat = 0
   FrameFormat.XUnion = 0
   FrameFormat.flagsX = 0
   FrameFormat.YUnion = 0
   FrameFormat.flagsY = 0

   lucam = windll.lucamapi
   error = bool()
   GetFormat = lucam.LucamGetFormat
   GetFormat.argtypes = ( HANDLE, LP_FRAME_FORMAT, POINTER(c_float) )
   GetFormat.restype = BOOL
   error = GetFormat (hCamera, FrameFormat, FrameRate )

On return the FrameRate parameter is correct but the FrameFormat
structure no longer has the any field attributes.  Further more it
often generates an access violation, apparently in python26.dll.  I
guess the call is writing to unallocated memory but I have no idea
why.

Can anyone help?



More information about the Python-list mailing list