简体   繁体   中英

Returning a Structure using ctypes in Python

I am writing a program in Python, that reads the YV12 Frame data from a IP Camera produced by Hikvision Ltd.

In the SDK, they provided a functional call that allows me to setup a Callback to retrieve the Frame Data.

My Callback function is like this:

def py_fDecodeCallBack(lPort, pBuffer, lSize, pFrameInfo, lReserved1, lReserved2):
    print "lPort: %r" % lPort
    print "lSize: %r   " % lSize
    print pFrameInfo
    print pBuffer
    print "pFrame Info: %r   " % pFrameInfo.nWidth

    return 0

$ The structure of pFramInfo is defined like this:

class FRAME_INFO(Structure):
        _fields_ =[
                   ('nWidth', c_long),
                   ('nHeight', c_long),
                   ('nStamp', c_long),
                   ('nType', c_long),
                   ('nFrameRate', c_long),
                   ('dwFrameNum', wintypes.DWORD)
                   ]

$

I use the following code to setup the callback function:

FSETDECCALLBACK = WINFUNCTYPE(c_bool, c_long, POINTER(wintypes.BYTE), c_long, POINTER(FRAME_INFO), c_long, c_long)
fSetDecCallBack = FSETDECCALLBACK(py_fDecodeCallBack)    

Then the callback function is being called by the SDK and it prints out the following:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 313, in 'calling callback function'
lPort: 0
lSize: 1382400   
<mytypes.LP_FRAME_INFO object at 0x03109CB0>
<wintypes2.LP_c_byte object at 0x03109D00>
  File "C:\Users\Rex\workspace\iSentry\hcnetsdkhelper.py", line 76, in py_fDecodeCallBack
    print "pFrame Info: %r   " % pFrameInfo.nWidth
AttributeError: 'LP_FRAME_INFO' object has no attribute 'nWidth'

Simple types like c_long (lPort and lSize) are being read correctly, but the pFrameInfo Structure does not have the fields that I have defined. I cannot read pFrameInfo.nWidth as it saids there is no such attribute...

My question is: How can I read the attributes in the structure returned from the dll through ctypes. I cannot see any examples on google doing this, and I found a topic in python.org http://bugs.python.org/issue5710 saying this just cannot be done, but the message is written in 2009.

I believe that as pFrameInfo is already being read from the dll, is there any way that I can get back the c_long data stored in the pFrameInfo Structure by cutting and reading the bytes in the memory space of pFrameInfo? Since all attributes of pFrameInfo are c_long, may be reading the structure byte by byte can reconstruct the value of the c_long variables. I am just guessing. Please help, this is a really big problem for me...

I think you need to dereference your pointer to FRAME_INFO:

frameInfo = pFrameInfo.contents
...
print "pFrame Info: %r   " % frameInfo.nWidth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM