预期的 LP_c_double 实例而不是 c_double_Array - python ctypes 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1413851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:11:38  来源:igfitidea点击:

Expected LP_c_double instance instead of c_double_Array - python ctypes error

pythontypesctypes

提问by trayres

I have a function in a DLL that I have to wrap with python code. The function is expecting a pointer to an array of doubles. This is the error I'm getting:

我在 DLL 中有一个函数,我必须用 python 代码包装它。该函数需要一个指向双精度数组的指针。这是我得到的错误:

Traceback (most recent call last):
  File "C:\....\.FROGmoduleTEST.py", line 243, in <module>
    FROGPCGPMonitorDLL.ReturnPulse(ptrpulse, ptrtdl, ptrtdP,ptrfdl,ptrfdP)
ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_c_double instance instead of c_double_Array_0_Array_2

I tried casting it like so:

我试着像这样铸造它:

ptrpulse = cast(ptrpulse, ctypes.LP_c_double)

but I get:

但我得到:

NameError: name 'LP_c_double' is not defined

Any help or direction is greatly appreciated. Thanks all!

非常感谢任何帮助或指导。谢谢大家!

回答by Jason R. Coombs

LP_c_double is created dynamically by ctypes when you create a pointer to a double. i.e.

LP_c_double 由 ctypes 在您创建指向 double 的指针时动态创建。IE

LP_c_double = POINTER(c_double)

At this point, you've created a C type. You can now create instances of these pointers.

此时,您已经创建了一个 C类型。您现在可以创建这些指针的实例。

my_pointer_one = LP_c_double()

But here's the kicker. Your function isn't expecting a pointer to a double. It's expecting an array of doubles. In C, an array of type X is represented by a pointer (of type X) to the first element in that array.

但这是踢球者。您的函数不期望指向双精度值的指针。它期待一系列双打。在 C 中,X 类型的数组由指向该数组中第一个元素的指针(X 类型)表示。

In other words, to create a pointer to a double suitable for passing to your function, you actually need to allocate an array of doubles of some finite size (the documentation for ReturnPulse should indicate how much to allocate), and then pass that element directly (do not cast, do not de-reference).

换句话说,要创建一个指向适合传递给函数的双精度数的指针,您实际上需要分配一个有限大小的双精度数数组(ReturnPulse 的文档应该指出要分配多少),然后直接传递该元素(不要强制转换,不要取消引用)。

i.e.

IE

size = GetSize()
# create the array type
array_of_size_doubles = c_double*size
# allocate several instances of that type
ptrpulse = array_of_size_doubles()
ptrtdl = array_of_size_doubles()
ptrtdP = array_of_size_doubles()
ptrfdl = array_of_size_doubles()
ptrfdP = array_of_size_doubles()
ReturnPulse(ptrpulse, ptrtdl, ptrtdP, ptrfdl, ptrfdP)

Now the five arrays should be populated with the values returnedby ReturnPulse.

现在这五个数组应该用 ReturnPulse返回的值填充。

回答by Mark Rushakoff

Are you writing the wrapper in Python yourself? The error saying "expected LP_c_double instance" means it's expecting a pointer to a single double, not an array as you've suggested.

您是自己用 Python 编写包装器吗?错误说“预期的 LP_c_double 实例”意味着它需要一个指向单个双精度的指针,而不是您建议的数组。

>>> ctypes.POINTER(ctypes.c_double * 10)()
<__main__.LP_c_double_Array_10 object at 0xb7eb24f4>
>>> ctypes.POINTER(ctypes.c_double * 20)()
<__main__.LP_c_double_Array_20 object at 0xb7d3a194>
>>> ctypes.POINTER(ctypes.c_double)()
<__main__.LP_c_double object at 0xb7eb24f4>

Either you need to fix your argtypesto correctly expect a pointer to an array of doubles, or you need to pass in a pointer to a single double like the function currently expects.

要么您需要修复argtypes以正确期望指向双精度数组的指针,要么您需要像函数当前所期望的那样传入指向单个双精度的指针。