ctypes和c++中的输出不匹配



我有一个带有自定义类型变量的dll,该变量在通过ref传递到getstatus函数后填充。

c++:

#pragma pack(push, 1)
typedef struct Status_ {
unsigned char mode;
unsigned char state;                        
unsigned int info;                       
unsigned int errorCode;                     
unsigned char selected;                 
char id[25];                            
}Status;
#pragma pack(pop)
Status status = { 0 };
int ret = GetStatus(someid, &status)

Python:

class Status(Structure):
_fields_ = [("mode", c_ubyte),      
("state", c_ubyte),                  
("info", c_uint),
("errorCode", c_uint),
("selected", c_ubyte),               
("id", c_char * 25)]   
status = Status()
getStatus = dll.GetStatus
getStatus.argtypes = [c_int, POINTER(Status)]
getStatus.restype = c_int
ret = getStatus(someid, byref(status));

我不知道什么是错的,但我得到不同的值在C++和python的状态字段。

编辑:添加了dll代码中缺少的pragma预处理器

如果标题中有说明,请确保设置包装;否则,在不与自然地址边界对齐的字段之间会添加额外的填充字节。例如,4字节的"info"参数通常会在其前面添加两个字节的填充,因此其在结构中的偏移量将是4的倍数。

添加_pack_ = 1以删除所有打包字节。

class Status(Structure):
_pack_ = 1
_fields_ = (("mode", c_ubyte),      
("state", c_ubyte),                  
("info", c_uint),
("errorCode", c_uint),
("selected", c_ubyte),               
("id", c_char * 25))   
status = Status()
getStatus = dll.GetStatus
getStatus.argtypes = c_int, POINTER(Status)
getStatus.restype = c_int
ret = getStatus(someid, byref(status))