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

时间:2015-04-30 16:25:03

标签: python windows dll ctypes

Python 2.7.8,Windows 7,Phyton 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调用返回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))

1 个答案:

答案 0 :(得分:2)

  • 初始化 WData 对象时,6 th 参数为ctypes.POINTER(MemoryBuffer)。这是(或应该在语法上)不正确: POINTER 返回指针类型,而你需要一个unsigned char数组(这是一个对象或类型的实例
  • 您获得的大小错误是因为 Python 结构与 C 结构不匹配。 C 中的 Data 成员是 PVOID ,它是一个大小为8的指针(如果你有 32位,则为4) Python ),而在 Python 中,它具有 MemoryBuffer 类型,其大小为1024

要解决此问题,请设置Memory_Params.Data成员的类型:ctypes.POINTER(ctypes.c_ubyte)