C++支持传递参数
// C++ 中编译c格式的函数,如果用c语言编译就不需要(文件后缀名.c)
// __declspec(dllexport)函数导出到库中
#include <stdio.h>
extern "C" __declspec(dllexport) void TestCtyps(int x, float y, bool isNum)
{
printf("In C TestCtypes %d %f %d\n", x, y, isNum);
if (isNum)
{
printf("true");
}
else
{
printf("false");
}
}python添加函数的调用
print("Test Ctypes")
from ctypes import *
#导入库 windows中dll后缀名不用加
lib = CDLL("C:\\Users\\Administrator\\Desktop\\testctypes\\x64\\Debug\\testctypes")
try:
lib.TestCtyps(101, 99.1, True)
except Exception as ex:
print("testCtypes Error", ex)
# 等待用户输入,程序不退出
input()添加调试参数

运行:补货了这个异常
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o5BdgfDV-1651753449813)(02 - 副本.assets/image-20220505200725476.png)] Python&C++相互混合调用编程全面实战-04传递数字参数_开发语言_02](https://file.cfanz.cn/uploads/png/2022/06/15/2/9Fa54XEP55.png)
也就是说,整型是可以直接转换的,第二个参数浮点型不能够直接转换,
需要使用c_float
try:
lib.TestCtyps(101, c_float(99.1), True)
except Exception as ex:
print("testCtypes Error", ex)
# 等待用户输入,程序不退出
input()运行成功输出结果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L8tJnlrq-1651753449814)(02 - 副本.assets/image-20220505202013947.png)] Python&C++相互混合调用编程全面实战-04传递数字参数_开发语言_03](https://file.cfanz.cn/uploads/png/2022/06/15/2/G051O6N84D.png)










