在python ctypes中声明变量并传递给dll函数 例如,我有一个C++变量声明和函数(假定它是一个DLL函数)< /P> 国际货币单位宽度、货币单位高度、步幅; somefunction(&img_宽度、img_高度和步幅) { .... }

在python ctypes中声明变量并传递给dll函数 例如,我有一个C++变量声明和函数(假定它是一个DLL函数)< /P> 国际货币单位宽度、货币单位高度、步幅; somefunction(&img_宽度、img_高度和步幅) { .... },c++,python-2.7,ctypes,C++,Python 2.7,Ctypes,同样的事情,我们如何使用PythoncTypes呢 我试着用下面的方法 img_width, img_height, stride = c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int) dll.somefunction(img_width, img_height, stride) dll.somefunction.restype = c.c_void_p dll.somefunction.argtypes = [c.POINTER(c_in

同样的事情,我们如何使用PythoncTypes呢

我试着用下面的方法

img_width, img_height, stride = c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int) dll.somefunction(img_width, img_height, stride) dll.somefunction.restype = c.c_void_p dll.somefunction.argtypes = [c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int)] dll.somefunction(img_width, img_height, stride) img_宽度,img_高度,步幅=c.指针(c_int),c.指针(c_int),c.指针(c_int) dll.somefunction(img_宽度、img_高度、步幅) 这导致了以下异常

ctypes.ArgumentError: argument 1: : Don't know how to convert parameter 1) NameError: global name 'img_width' is not defined ctypes.ArgumentError:参数1::不知道如何转换参数1) 我也试着用下面的方法

img_width, img_height, stride = c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int) dll.somefunction(img_width, img_height, stride) dll.somefunction.restype = c.c_void_p dll.somefunction.argtypes = [c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int)] dll.somefunction(img_width, img_height, stride) dll.somefunction.restype=c.c\u void\u p dll.somefunction.argtypes=[c.POINTER(c_int)、c.POINTER(c_int)、c.POINTER(c_int)] dll.somefunction(img_宽度、img_高度、步幅) 在这里,我以以下异常结束

ctypes.ArgumentError: argument 1: : Don't know how to convert parameter 1) NameError: global name 'img_width' is not defined NameError:未定义全局名称“img_width”
您正在创建需要创建整数实例并通过引用传递的类型:

from ctypes import *

# Declare the argument types
dll.somefunction.argtypes = POINTER(c_int), POINTER(c_int), POINTER(c_int)

# Create instances of the types needed
img_width, img_height, stride = c_int(), c_int(), c_int()

# Pass by reference
dll.somefunction(byref(img_width), byref(img_height), byref(stride))