title: Sending data structures as inputs to a C DLL function using ctypes and modify their attributes internally
body: <p>I am working with an old C dll and I want to use an internal function that takes two data structures as input arguments. The DLL function returns no outputs per say: the results are assigned internally to a data structure that is passed as an input argument, such as <em>MyFunction(INPUT,OUTPUT)</em> where: </p>
  
  <ol>
  <li>INPUT: Structure containing all the input parameters required by the DLL</li>
  <li>OUTPUT: Structure containing a series of initialized outputs, that are to be edited once the dll is executed.</li>
  </ol>
  
  <p>What I want to do is use Python ctypes to pass data structures to this DLL, and hopefully read output values from the OUTPUT data structure once it has been edited following the DLL execution. Unfortunately, I don't have the source code for the DLL: all I have is a working example in Visual Basic showing me the inputs and outputs of the internal function and how the data is defined. </p>
  
  <p>In VB, the data types and function are defined as follows: </p>
  
  <pre><code>Type DATAIN
    VAR1 As Long
    VAR2 As Double
    Var3(1 To 2) As Double
  End Type
  
  Type DATAOUT
    ABCD As Double
    EFGH As Double
    MYOUTPUT As Double
  End Type
  
  Public INPUTS as DATAIN
  Public OUTPUTS as DATAOUT
  Declare MyFunction Lib "c:\mydir\mydll.dll" (INPUTS As DATAIN, OUTPUTS As DATAOUT)
  INPUTS.VAR1=1
  INPUTS.VAR2=1.01
  INPUTS.VAR3(1)=0.01
  INPUTS.VAR3(2)=0.02
  
  Call MyFunction(INPUTS,OUTPUTS)
  X=OUTPUTS.myoutput
  </code></pre>
  
  <p>And the function correctly returns a result, say X=5.0. Now for the Python part, I have tried the following: </p>
  
  <pre><code>import ctypes as c
  
  class DATAIN(c.Structure):
      _fields_=[("VAR1", c.c_int),
                ("VAR2", c.c_double),
                ("VAR3", c.c_double*2)]
  class DATAOUT(c.Structure):
      _fields_=[("ABCD", c.c_double),
                ("EFGH", c.c_double),
                ("MYOUTPUT", c.c_double)]
  
  # Assign values to input structure and initialize output
  var3=[0.01,0.02]
  INPUTS=DATAIN(1,1.01,(c.c_double*len(var3))(*var3))    
  OUTPUTS=DATAOUT()
  
  path="c:\\mydir\\mydll.dll"
  dll=c.WinDLL(path)
  dll.MyFunction(c.byref(INPUTS),c.byref(OUTPUTS))
  # Now read the output value
  result=OUTPUTS.MYOUTPUT
  </code></pre>
  
  <p>But the value of my output remains 0.0, which is the way it was initialized. It seems like I can't get the dll to edit the data structure properly. Am I missing something fundamental here? Is it even possible to edit a ctypes Structure attributes within a DLL? </p>
  
  <p>I have also found <a href="https://stackoverflow.com/questions/4351721/python-ctypes-passing-a-struct-to-a-function-as-a-pointer-to-get-back-data">this thread</a> which does similar things but I can't seem to make my problem work. Any help would be greatly appreciated! Thanks</p>