辉煌仪奇 2022-03-30 13:29 采纳率: 48.2%
浏览 179
已结题

python调用dll内函数,如何接受返回函数

c++生成动态dll,python调用该dll如何传参
这是dll链接


下面是c++函数代码

double* disp11(unsigned char* rev)
{
    union gyro
    {
        char ch[4];
        float f;
        int si;
    };
    union gyro Gyrox[4];
    unsigned short int data_temp1;
    int i, j;
    static double angleRT[16];
    unsigned short int YearMonth;     //年
    unsigned short int Product_ID_T; //产品编号

    i = 2;
    Product_ID_T = rev[2] + rev[3] * 256; //产品编号
    YearMonth = rev[4] + rev[5] * 256;      //年

    i = 11;
    j = 0;
    Gyrox[0].ch[0] = rev[i++]; //经度
    Gyrox[0].ch[1] = rev[i++];
    Gyrox[0].ch[2] = rev[i++];
    Gyrox[0].ch[3] = rev[i++];
    angleRT[0] = Gyrox[0].f;

    i = 15;
    j = 0;
    Gyrox[0].ch[j++] = rev[i++]; //纬度
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    angleRT[1] = Gyrox[0].f;
    i = 19;
    j = 0;
    Gyrox[0].ch[j++] = rev[i++]; //高度,m
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    angleRT[2] = Gyrox[0].f;

    i = 23;
    j = 0;
    Gyrox[0].ch[j++] = rev[i++]; //航向
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    angleRT[3] = Gyrox[0].f;

    i = 27;
    j = 0;
    Gyrox[0].ch[j++] = rev[i++]; //步数
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    Gyrox[0].ch[j++] = rev[i++];
    angleRT[4] = Gyrox[0].f;

    //步幅度,低字节
    data_temp1 = rev[32] * 256 + rev[31];
    angleRT[5] = (double)data_temp1;

    angleRT[6] = (double)rev[33];       //卫星数
    angleRT[7] = (double)rev[34];       //状态字1
    angleRT[8] = (double)rev[35];       //状态字2
    angleRT[9] = (double)Product_ID_T; //状态字2

    angleRT[10] = (double)YearMonth; //年
    angleRT[11] = (double)rev[6];     //月
    angleRT[12] = (double)rev[7];     //日
    angleRT[13] = (double)rev[8];     //时
    angleRT[14] = (double)rev[9];     //分
    angleRT[15] = (double)rev[10];     //秒

    return angleRT;
}

这是我使用python调用的代码

from ctypes import *
dll = CDLL('./Dll1.dll')
print(dir(dll))
print(dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF]))

img

返回应该是个数组,但是接受到却是一个整数,我该如何取出dll返回参数

  • 写回答

8条回答 默认 最新

  • 急速光粒 2022-04-01 10:27
    关注

    没有环境,没测试过,试试下面代码:

    dll = CDLL('./Dll1.dll')
    print(dir(dll))
    dll.disp11.argtypes = [POINTER(c_char)]
    dll.disp11.restype = POINTER(c_double)
    res = dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF])
    
    print(res[0])
    print(res[1])
    print(res[2])
    ...
    print(res[15])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • ·星辰大海 2022-03-30 14:11
    关注

    先用argtypes确定各个参数的类型,然后传的时候再指定编码方式(encode写到参数项里就好)

    评论
  • 封尘绝念丶 2022-03-30 14:12
    关注

    array=dll.displl([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF])

    评论
  • greatofdream 2022-03-30 14:41
    关注

    你需要先检查一下cdll内函数返回值类型,看起来类型不对

    dll.disp11.restype
    

    如果上面的类型是int,说明有问题,需要手动修改成指针

    dll.disp11.restype=POINTER(c_double)
    dll.disp11.argtypes=[POINTER(c_double)]
    

    然后按照正常索引即可

    result = dll.disp11((c_double*37)(xx,xx,xx))
    result[0]
    result[1]
    
    评论 编辑记录
  • ~白+黑 Python领域新星创作者 2022-03-30 17:29
    关注
    from ctypes import *
    
    ten = c_char*37#构造数组对象
    
    a=CDLL(r"D:\Users\Desktop\Dll1.dll")
    
    arg1=(
        0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,
        0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,
        0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,
        0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,
        0x02,0x00,0x00,0x01,0xEF)
    
    arg2=(170, 85, 173, 2, 188, 8, 1, 6,
          8, 0, 0, 8, 51, 214, 66, 240,
          151, 239, 65, 36, 24, 183, 67,
          239, 105, 86, 67, 0, 0, 80, 65,
          168, 2, 0, 0, 1, 239)
    
    li=ten(*arg1)
    
    a.disp11.restype = c_long*15
    
    x=a.disp11(li)
    
    
    print(list(x))
    
    评论
  • 歇歇 2022-03-31 02:08
    关注

    返回的数据是数组,如解决了问题,请采纳

    from ctypes import *
    dll = CDLL('./Dll1.dll')
    print(dir(dll))
    #print(dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF]))
    pfloat = dll.disp11([0xAA,0x55,0xAD,0x02,0xBC,0x08,0x01,0x06,0x08,0x00,0x00,0x08,0x33,0xD6,0x42,0xF0,0x97,0xEF,0x41,0x24,0x18,0xB7,0x43,0xEF,0x69,0x56,0x43,0x00,0x00,0x50,0x41,0xA8,0x02,0x00,0x00,0x01,0xEF])
    i = 0
    while i < 16:
        print(pfloat.contents[i])
        i += 1
    
    评论
  • 仰望星空的代码 博客专家认证 2022-03-31 17:08
    关注
    评论
  • 落798. 2022年度博客新星评选TOP 6 2022-03-30 16:13
    关注

    类中声明一个__str__函数,用来返回类的字符串输出格式
    class face():
    featurename = none
    index = 1
    instancename ='a-bushing_s-20151111-1'
    isreferencerep = false
    pointon = ((-1.268087, -19.208438, -16.0),)

    def __str__(self):
        #这里你可以根据需要拼装s变量
        s = "({'featurename': none, 'index': 1, 'instancename': 'a-bushing_s-20151111-1', 'isreferencerep': false, 'pointon': ((-1.268087, -19.208438, -16.0),)})"
        return s
    

    a = [face(),face()]
    print(a[1])

    评论
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 4月9日
  • 已采纳回答 4月1日
  • 创建了问题 3月30日

悬赏问题

  • ¥20 arcgis制做交通拥堵时变图
  • ¥15 AD20 PCB板步线 这个要怎么步啊
  • ¥50 关于《奇迹世界》1.5版本,数据修改
  • ¥15 请问这个问题如何解决(关键词-File)
  • ¥50 visual studio 2022和EasyX图形化界面
  • ¥15 找一下报错原因,纠正一下
  • ¥50 Cox回归模型Nomogram图制作报错
  • ¥20 SQL如何查询多级用户的数据
  • ¥15 给车牌识别代码加一个识别轮廓长宽比的代码
  • ¥30 商品价格预测的transformer模型优化