使用Python Ctypes将struct指针传递给DLL函数

我试图使用Python ctypes访问DLL文件中的函数.提供的功能描述如下.

Prototype: Picam_ConnectDemoCamera( PicamModel model,
                                    const pichar* serial_number,
                                    PicamCameraID* id )

Description: Virtually connects the software-simulated 'model' with 'serial_number'
and returns the camera id in `_id_`
Notes: `_id_` is optional and can be null

该函数引用DLL中定义的一些变量类型.接下来描述这些变量

PicamModel
Type: enum
Description: The camera model.

PicamCameraID
Type: Struct
- PicamModel model: _model_ is the camera model
- PicamComputerInterface computer_interface: computer_interface is the method of
communication
- pichar sensor_name [PicamStringSize_SensorName]: sensor_name contains the name of
the sensor in the camera
- pichar serial_number [PicamStringSize_SerialNumber]: serial_number contains the
unique serial number of the camera

请注意,’pichar’在其中一个提供的头文件中定义如下:

typedef          char   pichar; /* character native to platform   

这似乎很简单,但由于某种原因,我不能解决这个问题.

我的理解如下:我传递函数三个变量:

1)一个模型,它真的是一个枚举.我被告知python ctypes本身并不支持枚举,所以我传给它一个整数,2,映射到特定的模型类型.

2)指向序列号的指针(我可以做到这一点:任意字符串)

3)指向PicamCameraID的指针,PicamCameraID是DLL中定义的变量类型,基于结构类型

阅读完SO后,我发现了一些很好的起点,但我仍然没有运气.到目前为止,这是我最好的投篮

def pointer(x):
      PointerToType = ctypes.POINTER(type(x))
      ptr = ctypes.cast(ctypes.addressof(x), PointerToType)
      return ptr

class PicamCameraID(ctypes.Structure):
      pass
PicamCameraID._fields_=[("model",ctypes.c_int),
                    ("computer_interface",ctypes.c_int),
                    ("sensor_name",ctypes.c_char_p),
                    ("serial_number",ctypes.c_char_p)]

myid = PicamCameraID()
model = ctypes.c_int(2)
serialnum = ctypes.c_char_p('asdf')
print picam.Picam_ConnectDemoCamera(model, pointer(serialnum), ctypes.byref(myid))

正如您所希望看到的那样,我正在尝试创建与DLL中定义的PicamCameraID结构相对应的变量类型.我的直觉是在将参数传递给函数时使用ctypes.pointer命令来引用它,但我发现使用ctypes.byref而不是elsewhere的指示.

代码运行没有错误(返回0),但是,当我尝试访问结构PicamCameraID的属性时,我得到不同的结果:

myid.model返回“2”,这是我指定的伟大
myid.computer_interface返回“1”,这很好

但问题是myid.serial_number返回

Traceback (most recent call last):   File "<pyshell#28>", line 1, in
<module>
   myid.serial_number ValueError: invalid string pointer 0x2820303031207820

和myid.sensor_name返回:

Traceback (most recent call last):   File "<pyshell#29>", line 1, in
<module>
    myid.sensor_name ValueError: invalid string pointer 0x3034333120563245

由于某些原因,这些字符串没有正确填充?

任何想法将不胜感激,我是python ctypes(和c)的新手

亲切的问候

2013年6月1日加入

typedef enum PicamStringSize {
    PicamStringSize_SensorName     =  64,
    PicamStringSize_SerialNumber   =  64,
    PicamStringSize_FirmwareName   =  64,
    PicamStringSize_FirmwareDetail = 256 } PicamStringSize; 

typedef struct PicamCameraID {
    PicamModel             model;
    PicamComputerInterface computer_interface;
    pichar                 sensor_name[PicamStringSize_SensorName];
    pichar                 serial_number[PicamStringSize_SerialNumber];
} PicamCameraID;
最佳答案
您对PicamCameraID结构的定义不正确:sensor_name和serial_number是arrays

"""
struct PicamCameraID {
  PicamModel             model;
  PicamComputerInterface computer_interface;
  pichar                 sensor_name[PicamStringSize_SensorName];
  pichar                 serial_number[PicamStringSize_SerialNumber]; 
};
"""
import ctypes as c

PicamStringSize_SensorName = PicamStringSize_SerialNumber = 64
PicamModel = PicamComputerInterface = c.c_int
pichar = c.c_char

class PicamCameraID(c.Structure):
    _fields_ = [("model", PicamModel),
                ("computer_interface", PicamComputerInterface),
                ("sensor_name", pichar * PicamStringSize_SensorName),
                ("serial_number", pichar * PicamStringSize_SerialNumber)]

看起来第二个参数只是一个字符串,所以你不需要对它应用指针().这是Picam_ConnectDemoCamera()函数的ctypes原型:

"""
Picam_ConnectDemoCamera(PicamModel model, const pichar* serial_number,
                        PicamCameraID* id)
"""
pichar_p = c.c_char_p # assume '\0'-terminated C strings
Picam_ConnectDemoCamera.argtypes = [PicamModel, pichar_p,
                                    c.POINTER(PicamCameraID)]
Picam_ConnectDemoCamera.restype = c.c_int # assume it returns C int

打电话给它:

picam_id = PicamCameraID()
rc = Picam_ConnectDemoCamera(2, "serial number here", c.byref(picam_id))
print(rc)
print(picam_id.sensor_name.value) # C string
print(picam_id.sensor_name.raw)   # raw memory

转载注明原文:使用Python Ctypes将struct指针传递给DLL函数 - 代码日志