3

I am new in python and recently using python to program CCD camera to taking pictures. I want to call a function in a .so file using ctypes. The prototype of the function is:

INT is_ImageFile (HIDS hCam, UINT nCommand, void* pParam, UINT cbSizeOfParam)

HIDS is uint type. An example given officially with c code is :

is_ImageFile(m_hCam, IS_IMAGE_FILE_CMD_SAVE, (void*)&ImageFileParams,
                   sizeof(ImageFileParams));

where ImageFileParams is a struct type:

typedef struct{
wchar_t* pwchFileName;
UINT     nFileType;
UINT     nQuality;
char**   ppcImageMem;
UINT*    pnImageID;
BYTE     reserved[32];}IMAGE_FILE_PARAMS;

In my .py file, I tried to define the struct in this way:

class IMAGE_FILE_PARAMS(Structure):
_fields_=[("pwchFileNmae",c_wchar_p),("nFileType",c_uint),("nQuality",c_uint),("ppcImageMem",POINTER(c_char_p)),("pnImageID",POINTER(c_char_p)),("reserved",c_byte*32)]

and define some of the members in this way:

ImageFileParams.pwchFileName = c_wchar_p("/home/user/aa.jpg")
ImageFileParams.nQuality=c_uint(80)
ImageFileParams.nFileType=c_uint(1)

then call the function:

is_ImageFile(hCam, IS_IMAGE_FILE_CMD_SAVE, cast(pointer(ImageFileParams),c_void_p),c_uint(sizeof(ImageFileParams)))

But I always get an error indicating invalid parameter. What's the problem?

Lukas
  • 31
  • 1
  • Show a minimal complete example that reproduces the error, and give the full traceback. – Mark Tolonen Jun 12 '17 at 04:26
  • Please, have a look at [SO: Python & Ctypes: Passing a struct to a function as a pointer to get back data](https://stackoverflow.com/questions/4351721/python-ctypes-passing-a-struct-to-a-function-as-a-pointer-to-get-back-data). I believe, you have to call the C function with `CDLL()`. IMHO, the Python ctypes API is not able to check whether your Python binding matches the C function signature. (If it doesn't you simply have undefined behavior i.e. everything inclusive: nothing recognizable -> wrong results -> crash.) If Python reports type errors this is just related to the "Python side". – Scheff's Cat Jun 12 '17 at 07:52

1 Answers1

0

I believe you are getting the invalid parameter error because of the last parameter:

c_uint(sizeof(ImageFileParams))

I checked the docs, and some example codes on their IDS website and I think you need to call it like this (assuming you import their ueye library:

is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, ImageFileParams,ueye.sizeof(ImageFileParams))
Tom
  • 2,545
  • 5
  • 31
  • 71