Chinaunix

标题: Python调用C语言编译的DLL 结构体指针作为参数的问题 [打印本页]

作者: linny959    时间: 2016-12-05 14:57
标题: Python调用C语言编译的DLL 结构体指针作为参数的问题
C语言结构体定义

//读结构开始。

typedef struct tagT_tagMaskInfo
{
        unsigned int  isUsed;
        unsigned char mb;   //内存区域
        unsigned char offset;
        unsigned char match; //正向匹配  反向匹配
        unsigned char dataMasklen;//掩码和数据的长度,长度单位为字节长度,默认掩码和数据长度是一样的
        unsigned char mask[32];  //掩码
        unsigned char data[32];  //数据
}T_tagMaskInfo;

typedef struct tagT_readOpInf
{
        unsigned char opId;  //本次读的标识
        unsigned char mb;
        unsigned char offset;
        unsigned char len;
}T_readOpInf;

typedef struct T_readPara
{
        unsigned int        antennaId;
        unsigned int    readOpNum;
        T_tagMaskInfo        tagMaskInfo[2];//如果使用必须先使用第一个,不能直接使用第二个
        T_readOpInf                readop[MAX_OP_NUM];
}T_readPara;

//读结构结束  结构具有通用性

//单次读结果开始
//由于读的区域不停返回的数据有不同,针对user0区,所以区别对待一下




typedef union ut2result
{
        unsigned int     opType;//0 代表OP读,1代表user0读数据
        unsigned int     opType1;
}T_readAllResult;

typedef struct t1result
{
        unsigned int    antennaId;
        unsigned int    opNum;
        unsigned char   tidData[8];
    ut2result ut2r[8];  
}T_tagReadResult;



typedef struct tagT_readResult
{
        unsigned       int tagNum;
        t1result t1r[64];
}T_readResult;
//单次读结果结束

DLL 接口
int READ(int fd, T_readPara* readPara, T_readResult* readResult)
{

for ( int p=0; p<32;p++)
//打印readPara->tagMaskInfo[0].data[]
{

        printf ("readPara->tagMaskInfo[0].data[");
        printf ("%d",p);
        printf ("]");
        printf ("%s\n",readPara->tagMaskInfo[0].data[p]);
}

Python code


dll = cdll.LoadLibrary('test6.dll');
reader = dll.READ
#set the return type
reader.restype = c_int;
#set the argtypes
reader.argtypes = [c_int, c_void_p,c_void_p];

if __name__ == "__main__":
fd = c_int(1000)
am1 = struct.pack('=IBBBBpp', 6, 1, 4, 4, 7, ('11111111111111000000000000000000000000'),
                  ('12345678000000000000000000000000'))
am2 = struct.pack('=IBBBB32p32p', 8, 1, 4, 4, 4, ("abcdefgabcdefgabcdefgabcdefgabcd"),
                  ("abcdefgabcdefgabcdefgabcdefgabcd"))
am_array = struct.pack('=72s72s', am1, am2)
trf1 = struct.pack('=BBBB', 2, 2, 2, 2)
trf_array = struct.pack('4s4s4s4s4s4s4s4s', trf1, trf1, trf1, trf1, trf1, trf1, trf1, trf1)
tr = struct.pack('II144s256s', 8, 8, am_array, trf_array)

result = create_string_buffer(8000)
reCode = reader(fd, byref(tr), byref(result))

现在的问题是 我想看看参数传进去正确与否,打印了下 am1.isUsed = c_uint(6)
   readPara->tagMaskInfo[0].mb
    readPara->tagMaskInfo[0].offset
    readPara->tagMaskInfo[0].match
    readPara->tagMaskInfo[0].dataMasklen
   但是当打印 readPara->tagMaskInfo[0].data[i]
就都是null
readPara->tagMaskInfo[0].data[0](null)


请教下,为什么我的参数没有正确传入呢?万分感谢。刚接触PYTHON 不是太熟悉。





作者: bskay    时间: 2016-12-07 13:50
ctypes 的帮助文档认真看看, 莫浮躁




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2