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))
答案 0 :(得分:2)
ctypes.POINTER(MemoryBuffer)
。这是(或应该在语法上)不正确: POINTER 返回指针类型,而你需要一个unsigned char
数组(这是一个对象或类型的实例)要解决此问题,请设置Memory_Params.Data
成员的类型:ctypes.POINTER(ctypes.c_ubyte)