如何使用Python 2.7.2将C++枚举转换为ctypes.Structure?

KCR*_*eed 4 c++ enums ctypes python-2.7

我搜索过并搜索过,但是我还没有找到一个能够做我需要做的事情的例子.
我发现 如何用Python代表'Enum'? 这里是SO,但它不包括ctypes.Structure.我还在 SO上找到了 在ctypes.Structure中使用枚举,但它包含了我不熟悉的指针.

我有一个包含typedef枚举的头文件,我需要在Python文件中的ctypes.Structure中使用它.

C++头文件:

typedef enum {

        ID_UNUSED,
        ID_DEVICE_NAME,
        ID_SCSI,
        ID_DEVICE_NUM,
} id_type_et; 
Run Code Online (Sandbox Code Playgroud)

Python文件(我目前的方式):

class IdTypeEt(ctypes.Structure):

        _pack_ = 1
        _fields_ = [ ("ID_UNUSED", ctypes.c_int32),
            ("ID_DEVICE_NAME", ctypes.c_char*64),
            ("ID_SCSI", ctypes.c_int32),
            ("ID_DEVICE_NUM", ctypes.c_int32) ]
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.越简单越好.

Rei*_*ica 5

An enum不是结构,它是具有预定义值集(枚举器常量)的整数类型.用它来代表它是没有意义的ctypes.Structure.你正在寻找这样的东西:

from ctypes import c_int

id_type_et = c_int
ID_UNUSED = id_type_et(0)
ID_DEVICE_NAME = id_type_et(1)
ID_SCSI = id_type_et(2)
ID_DEVICE_NUM = id_type_et(3)
Run Code Online (Sandbox Code Playgroud)