【发布时间】:2020-07-21 04:13:49
【问题描述】:
我的 C++ DLL 中有两个包装函数。
函数CreateObj()创建一个其他类的对象并返回一个指向该对象的指针
在第二个函数DoSomething 中,我将先前创建的指针作为第一个参数传递,第二个参数是无符号字符的 RGB 图像 (1200, 1200,3)。
在 python 端,我加载 DLL 并希望调用两个包装函数对我在 python 端加载的 RGB 图像进行一些处理。在从 python 调用 DoSomething 的最后一步中,我得到了一个 OSError。
任何帮助将不胜感激。
//The C++ library code
class Obj
{
/*..../*
};
extern "C"
{
#ifdef BUILD_AS_DLL
__declspec(dllexport)
#endif
Obj* CreateObj();
int DoSomething( Obj* ptr, uint8_t* t[1200][1200][]); //ptr was created using CreateObj and second argument is an image array of unsigned char of shape [1200][1200][3]
#ifdef BUILD_AS_DLL
__declspec(dllexport)
#endif
}
调用上述DLL的Python代码
from ctypes import *
import numpy as np
import numpy.ctypeslib as npct
import cv2
path = os.path.join(os.getcwd(), "mwe.dll")
lib = ctypes.CDLL(path)
create_func = lib.CreateObj
create_func.restype = c_void_p
obj = c_void_p(create_func())
dosomething_func = lib.DoSomething
ucharPtr = npct.ndpointer(dtype=np.uint8,
ndim=3,
shape = (1200,1200,3),
flags='CONTIGUOUS')
dosomething_func.argtype = (c_void_p,
ucharPtr)
dosomething_func.restype = c_int
img = cv2.imread("path/to/image")
img_ctype = byref(img.ctypes.data_as(c_void_p))
dosomething_func(byref(obj), img_ctype) // ERROR on this line
我收到错误
File "mwe.py", line 24, in <module>
res = dosomething_func(
OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF
【问题讨论】:
-
创建一个minimal reproducible example。您可以实现接受所需参数的小型虚拟 C 函数和实际重现故障的 Python 代码,并删除不相关的代码,如导入图像......只需创建一个您想要直接传递的大小的数组。您的 Python 代码引用了
seg并且没有定义它。 -
我已经处理了您的评论.. seg 是一个错字
-
这是一个最小的工作示例。我制作的 DLL 包含两个公开的函数
lib.CreateObj和lib.DoSomething。请重新考虑否决 -
并非如此,重现错误消息不起作用。理想情况下,将您实际编写的 DLL 代码发布为最少的代码以重现确切的问题。重新输入语法不正确的部分代码没有帮助。
标签: python python-3.x numpy ctypes