Python调用C语言产生的dll,使用struct

baidu_36953968 2016-12-07 02:18:04
新人请教一个问题
跪求解答


class T_readPara(Structure):
_fields_ = [("antennaId", c_uint), ("readOpNum", c_uint), ("tagMaskInfo", tagT_tagMaskInfo * 2),
("readop", tagT_readOpInf * 8)]

class tagT_readOpInf(Structure):
_fields_ = [("opId", c_ubyte), ("mb", c_ubyte), ("offset", c_ubyte), ("len", c_ubyte)]

class tagT_tagMaskInfo(Structure):
_fields_ = [("isUsed", c_uint), ("mb", c_ubyte), ("offset", c_ubyte), ("match", c_ubyte), ("dataMasklen", c_ubyte),
("mask", c_ubyte * 32), ("data", c_ubyte * 32)]

c 语言的调用
int READ(int fd, T_readPara* readPara, T_readResult* readResult)
{
for ( int p=0; p<32;p++)
{

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

python code
s=(68,70,69,71)
s1=c_ubyte*4
s1=s1(*s)
am1data=struct.pack('4B',*s1)
print len(am1data),"len(am1data)"
print am1data,"am1data"
am1 = struct.pack('=IBBBB4s', 6, 1, 4, 4, 8,am1data)
print len(am1 ),"len(am1 )"
print dir(am1)
print repr(am1),"repr(am1)"
print am1,"am1"



am_array = struct.pack('=II12s12s',8,8, am1, am1)
print repr(am_array)
trf1 = struct.pack('=BBBB', 73, 74, 75, 76)
print trf1,"trf1"
print len( trf1),"len( trf1)"
trf_array = struct.pack('4s4s4s4s4s4s4s4s', trf1, trf1, trf1, trf1, trf1, trf1, trf1, trf1)
print len (trf_array),"trf_array"
tr = struct.pack('II124s32s', 8, 8, am_array, trf_array)
print tr,"tr"
print repr(tr),"repr(tr)"
tr1=struct.unpack('II124s32s',tr)
result = create_string_buffer(8000)
reCode = reader(fd, cast(tr,POINTER(T_readPara)), byref(result))

我的问题是 我要用PYTHON 调用C语言产生的DLL dll 的参数是一个复杂的结构体 怎么才能把参数T_readPara* readPara 传进去呢 T_readResult* readResult是返回的结果 不用管

我目前的问题是打印readPara->tagMaskInfo[0].isUsed等都正确 但是打印printf ("%s\n",readPara->tagMaskInfo[0].data[p]); 都是null
...全文
294 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_36953968 2016-12-07
  • 打赏
  • 举报
回复
我使用CTYPE 但是总是内存访问错误,被这个问题折腾10来天了 要疯了 谁能帮我解答下,谢谢 antennaId = c_uint(1) readOpNum = c_uint(2) am1 = tagT_tagMaskInfo() isUsed = c_uint(1) mb = c_ubyte(0) offset = c_ubyte(4) match = c_ubyte(0) dataMasklen = c_ubyte(4) mask = (1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0) array_m=c_ubyte*32 array_m=array_m(*mask) data = (9, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0) array_d=c_ubyte*32 array_d=array_d(*data) am1 =tagT_tagMaskInfo(isUsed ,mb,offset,match,dataMasklen,array_m,array_d) print repr (am1),"repr (am1)" print am1.data[0],"am1.data[0]" print dir (am1) amd= (am1,am1) array_amd=tagT_tagMaskInfo * 2 array_am = array_amd(*amd) opId = c_ubyte(8) mb = c_ubyte(0) offset = c_ubyte(4) len = c_ubyte(4) trf1 = tagT_readOpInf(opId,mb,offset,len) array_trf = (tagT_readOpInf * 8)(trf1, trf1, trf1, trf1,trf1, trf1, trf1, trf1) tr = T_readPara(antennaId,readOpNum,array_am,array_trf) fd = c_int (999) result = create_string_buffer(8000) reCode = reader(fd, byref(tr), byref(result)) 一调用 reCode = reader(fd, byref(tr), byref(result)) 就出错了, reCode = reader(fd, byref(tr), byref(result)) WindowsError: exception: access violation reading 0x00000009 有谁能帮我看看 万分万分感谢。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧