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
Budelon
  • 53
  • 5
  • 1
    Just a wild guess: have you tried `funcao(a,b,byref(input_1),byref(output_1))` – PaulMcG Jan 24 '20 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#. – Frank Merrow Jan 25 '20 at 01:00

1 Answers1

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).

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • i tried this approach in the simplified code with only 1 argument – Budelon Jan 27 '20 at 01:56
  • I really don;t understand what you said. What exactly did you try? Did it work? – CristiFati Jan 27 '20 at 07:49
  • 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. – Budelon Jan 27 '20 at 18:13
  • 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). – Budelon Jan 27 '20 at 18:15
  • 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". – Budelon Jan 27 '20 at 19:09
  • I have edited a question to the problem: https://stackoverflow.com/questions/59937552/c-dll-crack-when-called-from-python. – Budelon Jan 27 '20 at 19:22
  • This is very important information that belongs in the question. Check [\[SO\]: How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) or [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/mcve) for more asking related details. Why do you need both *CTypes* and *Py\_Initialize*? It sounds like a design problem. Also try ***pydll*** instead of *cdll*. Also I thonk this answers the question as it is right now, so you could accept it, then focus on the other question. – CristiFati Jan 27 '20 at 19:25
  • I'm using a comercial software which simulates physical things, i can interact with the simulation to feedback information(Control theory), it can be done in this software with a DLL, that i'm using with python interpreter because my feedback function are in python. I'd like to have a code in order to debug my DLL with python functions, so i thought that call my DLL from a python script would allow me to test the DLL(with python interpreter and my python functions inside it). But i'm open to better ideas. Tkss – Budelon Jan 27 '20 at 19:59
  • Yes, your answer is correct. Thank you!! Obs: I've just accepted your answer, sorry i didn't know that i had this privilege. – Budelon Jan 30 '20 at 19:14