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,表示:define 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对象时,第6个参数是ctypes.POINTER(MemoryBuffer)。这是(或从语法上来说)不正确:POINTER返回一个指针类型,而您需要一个无符号字符数组(这是一个对象或类型的实例)
  • 您得到的大小错误是因为Python结构与C语言不匹配。 C中的Data成员是PVOID,它是一个大小为8的指针(如果您有32位Python,则为4),而在Python中,它的MemoryBuffer类型为1024

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

    关于python - Python ctypes无法在结构中传递内存数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29972991/

    10-11 19:30
    查看更多