How to initialize a structure variable in a C++ header file using Python ctypes?
I am trying to intialize the structure ExternalInputs_add_two
in .h header file using Python ctypes. The header file is:
typedef struct {
int32_T Input; /* '<Root>/Input' */
int32_T Input1; /* '<Root>/Input1' */
} ExternalInputs_add_two;
I need to initialize this structure since this member declaration uses the structure values for input:
extern ExternalInputs_add_two add_two_U;
What ctypes function intializes the structure? My Python code so far is:
class ModelOutput(Structure):
_fields_ = [("Output", c_int)]
class ModelInput(Structure):
_fields_ = [("Input", c_int),
("Input1", c_int)]
#define the functions
initiateModel = cdll.add_two_win32.add_two_initialize
stepModel = cdll.add_two_win32.add_two_step
terminateModel = cd开发者_开发技巧ll.add_two_win32.add_two_terminate
#define the pointers to the functions
initiateModel.restype = c_void_p
stepModel.restype = c_void_p
terminateModel.restype = c_void_p
#initialize the model with value of 1
print "\n\nInitialize"
errMsg = initiateModel(1)
print "initateModel reports:", errMsg
#Set the input
test_input = ModelInput(1,2)
input_ptr = pointer(test_input)
#This probably doesn't work since ExternalInputs_add_two is a structure, not a function.
cdll.add_two_win32.ExternalInputs_add_two = input_ptr
#Get the output pointer from add_two_U
output = POINTER(ModelOutput)
results = output.in_dll(cdll.add_two_win32,"add_two_U")
print "got results", results.Output
I asked the question yesterday how to get the output from the member add_two_U
. David was nice enough to answer this question yesterday (use the in_dll
function).
I've searched the ctypes documentation and online for an example or function to set a structure using ctype. I haven't found any so far.
Thank you for your help.
Thank you for your help and answer. Unfortunately, setting the input structure this way does not work. I forgot to mention that a Matlab script was written to use the dll and it works. I'm trying to convert to Python. To initialize the structure ExternalInputs_add_two, Matlab uses the following statements:
sm.Input = 1
sm.Input1 = 2
sp = libpointer('ExternalInputs_add_two', sm)
Then function add_two_U is called:
sp = calllib('add_two_win32', 'add_two_U')
sp.value
get(sp, 'Value')
sp.Value.Input = 3
sp.Value.Input1 = 2
If I remove the Matlab statement that initializes the ExternalInputs_add_two structure, I get an error.
What is the Python ctypes equivalent to initialize the structure? I'm sorry if I seem to be a pest.
It seems that you're confusing structure definitions with structure instances. As declared in the header file, ExternalInputs_add_two
is a type and add_two_U
is an instance. DLLs do not export types, only instances of types. Thus the following line in your Python code is nonsensical:
cdll.add_two_win32.ExternalInputs_add_two = input_ptr
What you probably want instead is to modify the instance of ExternalInputs_add_two
called add_two_U
. To do this, you do as David's previous answer suggested and use the in_dll
function:
PModelInput= POINTER(ModelInput)
padd_two_U = PModelInput.in_dll(cdll.add_two_win32, "add_two_U")
# Now modify the contents of the "add_two_U" variable
padd_two_U.Input = 1
padd_two_U.Input2 = 2
My advice to you is to stop exporting the variable from the DLL. Instead you should export two functions from your DLL: one to read the variable, and one to write the variable.
精彩评论