如何在python3中使用ctypes void **指针 [英] How to use ctypes void ** pointer in python3
问题描述
我想通过它的DLL连接一个光谱仪,其中一个功能定义为
I would to connect a spectrometer by its DLL, one of function is defined as
UINT UAI_SpectrometerOpen(unsigned int dev, void** handle, unsigned int VID, unsigned int PID)
来自文档,dev为指定索引光谱仪
的句柄是返回光谱仪句柄的指针
的VID是提供指定的VID
PID是提供指定的PID
dev,VID,PID是已知的,但是我不知道该如何设置句柄。
我当前的代码为
from document, dev is Specify the index for the spectrometer handle is Return to the pointer of handle of the spectrometer VID is Provide specified VID PID is Provide specified PID dev, VID, PID are known, but I don't know how to set handle. my current code is as
import ctypes
otoDLL = ctypes.CDLL('UserApplication.dll')
spectrometerOpen = otoDLL.UAI_SpectrometerOpen
spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(c_void_p),
ctypes.c_uint, ctypes.c_uint)
spectrometerOpen.restypes = ctypes.c_uint
handle = ctypes.c_void_p
errorCode = spectrometerOpen(0, handle, 1592, 2732)
运行上面的代码时,我得到了错误
When I run above code, I got error as
runfile('C:/Users/Steve/Documents/Python Scripts/otoDLL.py', wdir='C:/Users/Steve/Documents/Python Scripts')
Traceback (most recent call last):
File "<ipython-input-1-73fe9922d732>", line 1, in <module>
runfile('C:/Users/Steve/Documents/Python Scripts/otoDLL.py', wdir='C:/Users/Steve/Documents/Python Scripts')
File "C:\Users\Steve\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Users\Steve\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/Steve/Documents/Python Scripts/otoDLL.py", line 5, in <module>
spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(c_void_p),
NameError: name 'c_void_p' is not defined
我对ctypes和C不熟悉,有人可以帮助我解决此问题。
非常感谢。
I am not familiar with ctypes and C, can anyone help me to resolve this matter. Thanks a lot.
推荐答案
根据您的错误输出:
File "C:/Users/Steve/Documents/Python Scripts/otoDLL.py", line 5, in <module>
spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(c_void_p),
您忘记将 ctypes
放在 c_void_p
,因此:
spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p),
ctypes.c_uint, ctypes.c_uint)
根据您的函数签名,句柄参数是指向 void * 的指针,因此您需要像这样传递它:
According to your function signature , the handle parameter is a pointer to a void*
, thus you need to pass it like this:
import ctypes
otoDLL = ctypes.CDLL('UserApplication.dll')
spectrometerOpen = otoDLL.UAI_SpectrometerOpen
spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p),
ctypes.c_uint, ctypes.c_uint)
spectrometerOpen.restypes = ctypes.c_uint
# declare HANDLE type, which is a void*
HANDLE = ctypes.c_void_p
# example: declare an instance of HANDLE, set to NULL (0)
my_handle = HANDLE(0)
#pass the handle by reference (works like passing a void**)
errorCode = spectrometerOpen(0, ctypes.byref(my_handle), 1592, 2732)
注意:这只是一个例子,您应该查看 spectrometerOpen
函数的文档,以了解确切地等待 handle
参数(可以为NULL,确切是什么类型,等等)。
Note: this is just an example, you should check the documentation of the spectrometerOpen
function to see what exactly it is awaiting exactly for the handle
parameter (can it be NULL, what type is it exactly, etc.).
这篇关于如何在python3中使用ctypes void **指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!