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 MyFunction(INPUT,OUTPUT) where:
- INPUT: Structure containing all the input parameters required by the DLL
- OUTPUT: Structure containing a series of initialized outputs, that are to be edited once the dll is executed.
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.
In VB, the data types and function are defined as follows:
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
And the function correctly returns a result, say X=5.0. Now for the Python part, I have tried the following:
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
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?
I have also found this thread which does similar things but I can't seem to make my problem work. Any help would be greatly appreciated! Thanks