首页 > 解决方案 > Python调用c++ DLL:依赖其他dll调用c++函数时的访问冲突写法

问题描述

我正在尝试生成一个需要在 python 中调用的 c++ dll。

c++ dll 中的一些函数调用来自第三方静态库的函数。(在 mvc 中,我在 Additional include 目录中添加了所需的内容;Additional Library Directories and Additional Dependencies (xxx.lib) )

在 c++ 中一切正常。

在python中导出我在.h中做的

#define EXPORT_SYMBOL __declspec(dllexport)

#ifdef __cplusplus
extern "C" {
#endif
    EXPORT_SYMBOL  void func1(void);
    EXPORT_SYMBOL  void func2(void);
    EXPORT_SYMBOL  float testAdd(float x, float y);

#ifdef __cplusplus
}
#endif

testAdd 只是一个基本的返回 x+y。func1 是一个做基本工作的函数,最后是 puts("xxx"),不依赖于第三方库。func2 依赖于第三方库中的函数。

在蟒蛇中:

import ctypes
mydll = ctypes.CDLL("mydll.dll")
mydll.Add.argtypes = [ctypes.c_float,ctypes.c_float]
mydll.Add.restype = ctypes.c_float
print(mydll.Add(4,5))
print(mydll.func1())

返回 9.0 和 xxx。

但是当我这样做时: mydll.func2() 我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: exception: access violation writing 0x00000000B3C24F00

如果我第二次调用它,它“工作”,该函数不会返回我想要的,但它会返回一些东西。所以首先我有一个我无法识别的不一致问题。

其次,从 c++ 或 python 调用时,c++ func2 中某些第三方函数的返回似乎完全不同,这似乎根本不合逻辑。

但是当我在func2中做一些printf。我从 c++ 控制台程序中调用 .dll 我有一些东西,而从 python 中,我没有相同的结果。

标签: pythonc++

解决方案


推荐阅读