python - 从 Python 调用具有默认参数的 C++ 函数
问题描述
我从 DLL 中导出了这个简单的函数(我在 MSVC2015、Windows 10、x64 中工作):
// Main.h
#define DLL_SEMANTICS __declspec (dllexport)
extern "C"
{
DLL_SEMANTICS int CheckVal(const int x, const int y = 1);
}
代码:
// Main.cpp
int CheckVal(const int x, const int y)
{ cout << y << endl; return 0; }
}
根据这个SO 线程,使用extern "C"
应该不是问题,实际上当从 exe 文件调用这个函数时,我得到了想要的结果(即控制台的“1”)但是当我从 Python 调用它时使用:
import ctypes
from ctypes import cdll
dllPath = r"C:\MyDll.dll"
lib = cdll.LoadLibrary(dllPath)
lib.CheckVal(ctypes.c_int(1))
我正在打印一些垃圾值(通过 PTVS 调试确认这y
确实是垃圾值。)
我做错了什么或默认参数不能在 Python 中工作吗?
解决方案
默认参数是 C++ 的一个优点。如果您在 C++ 外部调用,则需要传递两个参数。
推荐阅读
- opengl - 为什么在 D3D 中需要 RHW
- javascript - JavaScript Promises 混淆反模式?
- java - 如何从管道内或管道运行之前获取数据流作业 ID?
- r - 由于统计信息导入,无法下载 doebioresearch 包
- python-3.x - 用于读取文件并打印是否存在感兴趣行的 Python 脚本
- amazon-web-services - 为使用 Elastic Beanstalk 和 LightSail 的子域配置 SSL
- html - 如果元素不存在/是可选的,则不要在 CSS 网格中保留空间
- python - 通过没有列表的while循环创建对象?
- python - Dockerizing 一个烧瓶 resfull 应用程序并在 ngnix 上运行
- excel - 如何使用VBA将包含某些文本的行复制到另一个工作表