简体   繁体   中英

Passing pointer to ndarray in a struct within a struct (ctypes)

I have a ctypes function that has a struct, containing another struct, as one of its arguments. I have created the structs as follows;

class ChannelDataBuffers(Structure):
_pack_ = PACK
_fields_ = [
            ("bufferMax",  c_void_p),
            ("bufferMin", c_void_p),
            ("dataType", c_int32)
            ]


class DataBuffers(Structure):
_pack_ = PACK
_fields_ =  [
                ("channel",               c_int32),
                ("waveform",              c_uint64),
                ("downSampleRatioMode",   c_int32),
                ("read",                  c_int32),
                ("buffers",               ChannelDataBuffers),
                ("nbuffers",              c_uint32),
                ("nDistributionPoints",   c_uint32)
            ]
def __init__(self, channel, waveform, ratiomode, read, ref_buffermax, ref_buffermin, datatype, nbuffers, nDistributionPoints):

    self.channel = c_int32(channel)
    self.waveform = c_uint64(waveform)
    self.downSampleRatioMode = c_int32(ratiomode)
    self.read = c_int32(read)
    self.buffers.bufferMax = (ref_buffermax)
    self.buffers.bufferMin = (ref_buffermin)
    self.buffers.datatype = c_int32(datatype)
    self.nbuffers = c_uint32(nbuffers)
    self.nDistributionPoints = c_uint32(nDistributionPoints)

The bufferMax and bufferMin are both numpy arrays, passed using the .ctypes argument - ie defined as follows:

bufMax = self._buffers[bufIndex].data.ctypes

This results in an object being passed into the DataBuffers init function of

numpy.core._internal._ctypes object at 0x08D99D90

Which seems sensible. However I then hit an exception of

 cannot be converted to pointer <type 'exceptions.TypeError'>

at the line

self.buffers.bufferMax = (ref_buffermax) 

Anyone know what I can do to fix this??

可能您需要使用ctypes.cast

self.buffers.bufferMax = ctypes.cast(ref_buffermax, c_void_p)

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