2

I'm trying to execute my Python Code that must call a C function, which execute some calculation and save the value in a pointer that must be accessible from Python code. I'd like to do that because i'm constructing a DLL and i want to validate the algebra inside the DLL function, therefore i'd like to use a python code to validate the DLL. The Python Code

from ctypes import *


if __name__ == '__main__':

    mydll = cdll.LoadLibrary("./dll_simples.dll")
    funcao = mydll.simuser
    funcao.argtypes = c_double,c_double,POINTER(c_double),POINTER(c_double)



    a = 0
    b = 0
    input_1 = (c_double * 1)()
    input_1[0] = 5
    output_1  = (c_double * 1)()
    funcao(a,b,input_1,output_1)

and my DLL

__declspec(dllexport) void simuser(double t, double delt, double* in, double* out)
{



out[0] = 2 * in[0];



}

after executing this code, i have the error

funcao(a,b,input_1,output_1)

OSError: exception: access violation reading 0x0000000000000018
2
  • 1
    Just a wild guess: have you tried funcao(a,b,byref(input_1),byref(output_1)) Commented Jan 24, 2020 at 22:27
  • Is python a requirement for some reason? Why not test from C and save all the DLL hassles. If you needed exceptions and such then use C++ or C#. Commented Jan 25, 2020 at 1:00

1 Answer 1

1

Listing [Python 3.Docs]: ctypes - A foreign function library for Python.

So, you want to pass an array to a function that expects a pointer. For that case, ctypes.cast is required:

So, instead of:

funcao(a, b, input_1, output_1)

use:

funcao(a, b, cast(input_1, POINTER(c_double)), cast(output_1, POINTER(c_double)))

Looking at the existing C code, it only uses one value for the 2 pointers, case in which you won't be needing arrays at all (but I doubt that's the intent because then the input value shouldn't be a pointer):

# ...

input_1 = c_double(5)
output_1 = c_double(0)

funcao(a, b, byref(input_1), byref(output_1))

A working example: [SO]: Pointer from Python (ctypes) to C to save function output (@CristiFati's answer).

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

9 Comments

i tried this approach in the simplified code with only 1 argument
I really don;t understand what you said. What exactly did you try? Did it work?
i'm sorry. The comment was incomplete unintentionally. I'd like to say that i've tried your approach but it didn't work. So i've simplified the code in order to have only 1 pointer array using your approach ('cast' function). But i'm still struggling to make this piece of code work well. I'll post here the solution when i find it.
i must use array because in the future it'll be an array that will be sent to a C dll which will calculate some stuff and save in another array (output_1).
i'm sorry, your answer is correct. The error is inside my C code. When i have Py_Initialize(); inside the DLL nothing works. This DLL must initialize a Python interpreter that works fine when i call DLL from C code, but when is Python code calling the DLL it cracks with the error "OSError: exception: access violation reading 0x0000000000000025".
|

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.