1

I am passing an array of doubles to a DLL using CTypes. The code is below.

I need to pass a pointer to the array. I have tried to pass it four different ways, as shown in the code. For simplicity, I am passing the same array three times.

TestArrayType = ctypes.c_double * 1000
arrNew = TestArrayType()

hDLL = ctypes.WinDLL(r"C:/NASM_Test_Projects/Test_Project_Full2/Std_Math_Formulas.dll")

CallTest = hDLL.TryThemAll

CallTest.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double)]

CallTest.restype = ctypes.c_int64

t = type(arrNew)

#Call #1
#ptrA = ctypes.cast(arrNew, ctypes.POINTER(ctypes.c_double))
#retvar = CallTest(ptrA, ptrA, ptrA)

#Call #2
#retvar = CallTest(ctypes.pointer(arrNew), ctypes.pointer(arrNew), ctypes.pointer(arrNew))

#Call #3
#retvar = CallTest(ctypes.POINTER(arrNew), ctypes.POINTER(arrNew), ctypes.POINTER(arrNew))

#Call #4
retvar = CallTest(arrNew, arrNew, arrNew)

The four different calls above give the following error messages:

Call #1: Message=exception: access violation reading 0x000000000000000D

Call #2: ctypes.ArgumentError: argument 1: : expected LP_c_double instance instead of LP_c_double_Array_1000

Call #3: must be a ctypes type

Call #4: Message=exception: access violation reading 0xFFFFFFFFFFFFFFFF

So I'm not doing it right yet. I've done a lot of research, but I still haven't found the right way to pass pointers to the array.

The value of t in the expression t = type(arrNew) is:
t = class Trans_01_Samples_04.c_double_Array_1000

Thanks in advance for any help.

3
  • #4 looks right. See this recent answer. Perhaps show the test C code you are calling? Commented Jan 19, 2018 at 22:23
  • #1 should work as well, but is unnecessary (arrays convert to pointer, similar to the "decay to pointer" of C. #2's type is pointer to double[1000] array, not a simple pointer to double. #3 is passing a type, not an instance. Commented Jan 19, 2018 at 22:46
  • Thanks, Mark. I'm working on this right now and I will post more as soon as I have finished. Commented Jan 20, 2018 at 0:38

1 Answer 1

0

Mark Tolonen is right -- #4 does work, and the problem is in my DLL code. The code is in NASM, and to confirm I created a super-simple DLL which accepted the array pointers and returned the expected value.

For the record, here is the new simplified code:

; Header Section
[BITS 64]

export TryThemAll

section .data

section .text

TryThemAll:
push rdi
push rbp
mov rdi,[rcx]
pop rbp
pop rdi
ret

Now I'm on to run-time debugging for the substantially larger NASM dll code.

For future use by other Stack Overflow readers, this post confirms the correct way to pass pointers to arrays in CTypes.

Thanks very much, Mark

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.