Python ctypes 无法在结构中传递内存数组

Python ctypes Unable to pass memory array in a structure

Python 2.7.8, Windows 7、飞腾USB编程器的DLL

我被 Data 元素困住了,它应该是一个大内存数组,我尝试使用几种不同的定义,但我无法理解这些错误试图告诉我什么。我遇到的大部分错误都是类型错误,下面这段代码是我最接近的,好像是在调用函数,但是因为错误没有处理。

C API:

typedef struct tagACI_Memory_Params
{
  UINT   Size;             // (in)  Size of structure, in bytes
  UINT   BufferNumber;     // (in)  Number of buffer of interest, the first buffer number is 0
  UINT   LayerNumber;      // (in)  Number of layer of interest, the first layer number is 0
  DWORD  AddressLow;       // (in)  Low 32 bits of address, in layer units (natural to device address)
  DWORD  AddressHigh;      // (in)  High 32 bits of address, in layer units (natural to device address)
  PVOID  Data;             // (in || out) Pointer to data to read to or write from
  DWORD  DataSize;         // (in)  Size of data to read or write, in layer units, max. 16 MB (0x1000000)
  DWORD  FillValue;        // (in)  Value to fill buffer with, used by ACI_FillLayer() only
} ACI_Memory_Params;

我的python代码:

MaxMemorySize = 1024
MemoryBuffer = ctypes.c_ubyte * MaxMemorySize 

class Memory_Params(ctypes.Structure):
    _fields_ =  [("Size", ctypes.wintypes.UINT),
                 ("BufferNumber", ctypes.wintypes.UINT),
                 ("LayerNumber", ctypes.wintypes.UINT),
                 ("AddressLow", ctypes.wintypes.DWORD),
                 ("AddressHigh", ctypes.wintypes.DWORD),
                 ("Data", MemoryBuffer ),
                 ("DataSize", ctypes.wintypes.DWORD),
                 ("FillValue", ctypes.wintypes.DWORD)
                ]

WriteLayer = ctypes.windll.ACI.ACI_WriteLayer
WriteLayer.argtypes = [ctypes.POINTER(Memory_Params)]
WriteLayer.restype = ctypes.HRESULT

WData = Memory_Params(ctypes.sizeof(Memory_Params),0,0,0,0,ctypes.POINTER(MemoryBuffer),15,0)
for i in range(10):
    WData.Data[i] = i

print 'write result', WriteLayer(ctypes.byref(WData))

API 调用 returns a 1 表示:定义 ACI_ERR_INVALID_PARAMS_SIZE 1 // ACI 函数中的无效结构大小

更新: 我错过了关键的事情:定义后创建和使用 MemoryBuffer 对象,以及如何正确定义指针。 如果其他人有类似情况,这里是工作代码:

MaxMemorySize = 1024
MemoryBuffer = ctypes.c_ubyte * MaxMemorySize

class Memory_Params(ctypes.Structure):
    _fields_ =  [("Size", ctypes.wintypes.UINT),
                 ("BufferNumber", ctypes.wintypes.UINT),
                 ("LayerNumber", ctypes.wintypes.UINT),
                 ("AddressLow", ctypes.wintypes.DWORD),
                 ("AddressHigh", ctypes.wintypes.DWORD),
                 ("Data", ctypes.POINTER(ctypes.c_ubyte) ),
                 ("DataSize", ctypes.wintypes.DWORD),
                 ("FillValue", ctypes.wintypes.DWORD)
                ]
WriteLayer = ctypes.windll.ACI.ACI_WriteLayer
WriteLayer.argtypes = [ctypes.POINTER(Memory_Params)]
WriteLayer.restype = ctypes.wintypes.UINT

PData = MemoryBuffer()
WData = Memory_Params(ctypes.sizeof(Memory_Params),0,0,0,0,PData,for i in range(10):
    PData[i] = i
WriteLayer(ctypes.byref(WData))
  • 初始化WData对象时,第6th参数是ctypes.POINTER(MemoryBuffer) 。这是(或者在语法上应该是)不正确的:POINTER returns 一个指针 type,而你需要一个 unsigned char 数组(它是一个 object 或一个类型的 instance
  • 您得到的大小错误是因为 Python 结构与 C 结构不匹配。 Data成员C是一个PVOID,它是一个大小为8(或4 如果你有 32bit Python), 而在 Python它具有大小为 1024
  • MemoryBuffer 类型

要更正此问题,请将您的 Memory_Params.Data 会员类型设为: ctypes.POINTER(ctypes.c_ubyte)