Python调用C++类库 (踩坑日志)

起因

开发自动化工具过程中,须要应用业余的测试射频参数的仪器IQmeasure,厂家提供的API只用C ++版本。
客户端应用python(wxpython)开发,所以遇到了python调用C ++类库的问题。

环境

  • PyCharm2020
  • python3.6.12  32位   (python3.8无奈应用,64位python无奈应用;其余版本未测试)
  • IQmeasure_SCPI.dll (供应商给的dll包)
  • IQmeasure.chm (dll对应的文档)

参考资料

ctypes的python官网文档

应用

应用前请先浏览ctypes官网文档

  1. 创立一个新我的项目(python应用3.6.12 32位版本)
  2. 将dll文件放入根目录
  3. 根目录创立main.py

目录构造如下

test
├── IQmeasure_SCPI.dll
├── venv
└── main.py

根底应用办法如下(main.py):

from ctypes import *# 加载dll包iq = cdll.LoadLibrary('./IQmeasure_SCPI.dll')# 调用办法init_result = iq.LP_Init(c_int(0), c_int(1))print('init_result:', init_result)  # init_result: 0# 该办法中,返回0为胜利# 原C++文档中,该办法如下:# int LP_Init(int IQtype = IQTYPE_XEL,int testerControlMethod = 1);

知识点

  • 应用ctypes.cdll.LoadLibrary()加载dll包
  • 参数须要转换为c的类型(如c_int(),不了解的先看一下参考资料 ctypes的python官网文档 )
  • 在C++中的可选参数,在应用python调用时不可省略,请传入办法中的默认值

有返回值

from ctypes import *# 加载dll包iq = cdll.LoadLibrary('./IQmeasure_SCPI.dll')# 有返回值# 设置返回值类型iq.LP_GetErrorString.restype = c_char_p# 设置初始值类型iq.LP_GetErrorString.argtypes = [c_int]msg = iq.LP_GetErrorString(c_int(10))print(msg)  # b'Invalid analysis type'# 转换为stringstr_msg = msg.decode("utf-8")print(str_msg) # VSA number is out of range. Try 1-4.# 原C++文档中,该办法如下:# char* LP_GetErrorString(int err) 

知识点

  • 有返回值时,应用 .办法名.restype 设置返回值类型
  • 参数值也可应用相似办法设置,应用 .办法名.argtypes 设置参数类型

参数值为 *类型

简略得查了材料,带 如同是指针类型吧,开发焦急所以没有深刻学习了,相似int , char * 这样的

# ...省略加载dll# 参数值为*类型# 应用byref(),包装对应类型即可iq.LP_SetTesterMode(c_int(0), byref(c_int(1)), c_int(1))# 原C++文档中,该办法如下:# int LP_SetTesterMode( int signalMode = UP_TO_80MHZ_SIGNAL, int *selectedModules = NULL, int numOfSelectedModules = 1 );

知识点

  • 参数为*类型的参数,应用byref包装

援用类型参数

# ...省略加载dllversion = create_string_buffer(4096)iq.LP_GetVersion(version, 4096)version_result = version.value.decode("utf-8")# 原C++文档中,该办法如下:# 该办法会扭转*buffer,python中须要读取*buffer的值# bool LP_GetVersion(char *buffer, int buf_size);

知识点

  • 办法参数传入后,办法会扭转原参数,须要读取改参数的新值
  • 先创立一个buffer
  • 调用办法
  • 将buffer转换回来

残缺测试代码

from ctypes import *# 加载dll包iq = cdll.LoadLibrary('./IQmeasure_SCPI.dll')# 调用办法init_result = iq.LP_Init(c_int(0), c_int(1))print('init_result:', init_result)  # init_result: 0# 该办法中,返回0为胜利# 原C++文档中,该办法如下:# int LP_Init(int IQtype = IQTYPE_XEL,int testerControlMethod = 1);# 有返回值# 设置返回值类型iq.LP_GetErrorString.restype = c_char_p# 设置初始值类型iq.LP_GetErrorString.argtypes = [c_int]msg = iq.LP_GetErrorString(c_int(10))print(msg)  # b'Invalid analysis type'# 转换为stringstr_msg = msg.decode("utf-8")print(str_msg)  # VSA number is out of range. Try 1-4.# 原C++文档中,该办法如下:# char* LP_GetErrorString(int err)# 参数值为*类型# 应用byref(),包装对应类型即可iq.LP_SetTesterMode(c_int(0), byref(c_int(1)), c_int(1))# 原C++文档中,该办法如下:# int LP_SetTesterMode( int signalMode = UP_TO_80MHZ_SIGNAL, int *selectedModules = NULL, int numOfSelectedModules = 1 );version = create_string_buffer(4096)iq.LP_GetVersion(version, 4096)version_result = version.value.decode("utf-8")# 原C++文档中,该办法如下:# 该办法会扭转*buffer,python中须要读取*buffer的值# bool LP_GetVersion(char *buffer, int buf_size);

总结

因为对C ++不甚了解,对C ++局部的解释、名词有偏差,程度限度勿怪,欢送私信纠正。

内容为自己和骆小萍同学在开发实际中失去

以上