Expected LP_c_double instance instead of c_double_Array - ctypes python error

I have a function in a DLL that I have to wrap using python code. The function expects a pointer to an array of doubles. This is the error I get:

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 to do it like this:

 ptrpulse = cast(ptrpulse, ctypes.LP_c_double) 

but I get:

 NameError: name 'LP_c_double' is not defined 

Any help or guidance is appreciated. Thanks everyone!

+2
source share
2 answers

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

 LP_c_double = POINTER(c_double) 

At this point, you created type C. Now you can instantiate these pointers.

 my_pointer_one = LP_c_double() 

But here is the kicker. Your function does not expect a pointer to double. He expects an array of doubles. In C, an array of type X is represented by a pointer (of type X) to the first element in this array.

In other words, in order to create a double pointer suitable for passing your function, you really need to allocate an array of doubles of some finite size (the documentation for ReturnPulse should indicate how much to allocate), and then pass this element directly (do not drop, do not remove the link) .

i.e.

 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 five arrays should be populated with the values โ€‹โ€‹returned by ReturnPulse.

+4
source

Do you write a wrapper in Python yourself? The error saying "expected instance of LP_c_double" means that it expects a pointer to a single double, not an array, as you suggested.

 >>> 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 argtypes to correctly expect a pointer to an array of doubles, or you need to pass a pointer to a single double, as currently expected.

+2
source

Source: https://habr.com/ru/post/1497882/


All Articles