工作上需要用python调用dll解析码流输出到文件,如何调用dll很多博客都有描述,请参考如下blog:
如何调用请参考:
http://blog.csdn.net/lf8289/article/details/2322550
WinDLL和CDLL的选择,请参考:
http://blog.csdn.net/jiangxuchen/article/details/8741613
传递自定义的结构,请参考:
http://www.jb51.net/article/52513.htm
但是如何将一串字符串当做字节码流传给dll的接口呢,请看本文档的代码:
#encoding:utf-8
‘‘‘
Created on 2015年3月5日
@author: Administrator
‘‘‘
import ctypes,string
if __name__ == "__main__":
#两个字符代表一个字节的字符串码流
hexstring = ‘000c40808300000600080003404105001a00504f170a16e65b110748100bf664f01080001ff43748065805f070c040115264f01000005c0a00570220003103e5e03e11035758a6200b601404ef6523021e242ca040080402600400021f025d0104e0c1004300060064f0100000006440080064f0100183bcb000864001300060000607c0f4374806‘
hexstring_len = len(hexstring)
#接口原型:pLTEDllRnlcDecode(ubyte,uint,uint,ubyte*,ubyte,char*)
dll = ctypes.CDLL(‘LTE_DE_000_110729.dll‘)
parser = dll.LTEDllRnlcDecode
parser.argtypes = [ctypes.c_ubyte, ctypes.c_uint, ctypes.c_uint, ctypes.c_char_p, ctypes.c_uint, ctypes.c_char_p]
parser.restypes = ctypes.c_void_p
data_id = ctypes.c_ubyte()
data_id.value = 0
msg_id = ctypes.c_uint()
msg_id.value = 57612
data_len = ctypes.c_uint()
data_len.value = hexstring_len / 2
reserved = ctypes.c_uint()
reserved.value = 0
#将码流字符串按2字节转换为byte串
char_array = [‘0‘]*data_len.value
i = 0
j = 0
while i < hexstring_len:
byte = string.atoi(hexstring[i:i+2],16)
char_array[j] = chr(byte)
i = i + 2
j = j + 1
print char_array
ss = ‘‘.join(char_array)
#byte串转成接口需要的byte*
data_buffer = ctypes.c_char_p()
data_buffer.value = ss
# for i in range(len(ss)):
# char_array[i]=ord(ss[i])
# print char_array
#buffer = ctypes.POINTER(ctypes.c_ubyte)
#buffer.value =
outfile = ctypes.c_char_p( )
outfile.value = "out.txt"
parser(data_id, msg_id, data_len,data_buffer,reserved,outfile)
原文:http://blog.csdn.net/xidwong/article/details/44085139