使用python分配内存以传递给dll

inb24sb2  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(288)

我得到了一个dll,它需要一个指向c类型字节数组的内存指针。dll将读取和修改数组,并将在数组末尾放置一些额外的数据。
如何在python中将1mb内存分配为c类型字节数组并获取指针?
如何将python中的c类型字节数组写入该指针?
您可能想知道我为什么要这样做,但不幸的是,这是这个dll的唯一接口,我必须用python来完成。
以下是我当前的设置:


# bytes_buffer = b'Hello World! Hello World!' # this is just for testing, it's actually a grpc buffer

size_in = len(bytes_buffer)
print(bytes_buffer)

# buffer_in = ctypes.c_char * BUFFER_LENGTH

# buffer_in = ctypes.cast(bytes_buffer, ctypes.POINTER(ctypes.c_char*1000) )

buffer_in = ctypes.c_char_p(bytes_buffer)

# buffer_in = ctypes.create_string_buffer(BUFFER_LENGTH)

# all of these will later result in: exception: access violation reading 0xFFFFFFFFFFFFFFFF

address = id(buffer_in)

# parse 64 bit pointer into two uint32

pointer_data_in_hi = ctypes.c_uint32(address)
pointer_data_in_lo = ctypes.c_uint32(address >> 32)

# save pointer and data length into dll:

fmu.setInteger([vrs['data_in_lo']], [pointer_data_in_lo.value])
fmu.setInteger([vrs['data_in_hi']], [pointer_data_in_hi.value])
fmu.setInteger([vrs['data_size']], [size_in])

# here the dll will read and modify the data in the byte array and trigger an access violation:

fmu.doStep(currentCommunicationPoint=time, communicationStepSize=step_size)

我希望你能帮助我:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题