免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1980 | 回复: 1
打印 上一主题 下一主题

Python调用C语言编译的DLL 结构体指针作为参数的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-12-05 14:57 |只看该作者 |倒序浏览
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 不是太熟悉。




论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
2 [报告]
发表于 2016-12-07 13:50 |只看该作者
ctypes 的帮助文档认真看看, 莫浮躁
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP