windows下,python调用dll例子,展示如何传递字节码流参数到dll接口

 工作上需要用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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值