开发者

How to call functions using ctypes with no input parameters?

I am trying to call the C++ function add_two_U from Python using ctypes. The function add_two_U is defined in the C++ header file as:

extern ExternalInputs_add_two add_two_U;

The structure ExternalInputs_add_two is defined in the header file as:

typedef struct {
  int32_T Input;                       /* '<Root>/Input' */
  int32_T Input1;                      /* '<Root>/Input1' */
} ExternalInputs_add_two;

The function add_two_initialize that I call in my Python code below is defined in the header file as:

extern void add_two_initialize(boolean_T firstTime);

My Python code:

import sys
from ctypes import *

class ModelInput(Structure):
    _fields_ = [("Input", c_int),
                ("Input1", c_int)]

#define the functions    
initiateModel = cdll.add_two_win32.add_two_initialize
U_Model = cdll.add_two_win32.add_two_U


# define the pointers to the functions
initiateModel.restype = c_void_p
U_Model.restype = c_void_p

#initialize the model with value of 1
print "\n\nInitialize"
errMsg = initiateModel(1)
print "initateModel reports:", errMsg

#Initialize the structure and get the pointer.
test_input = ModelInput(1,2)
input_ptr =  pointer(test_input)

I am trying to call function add_two_U in the python code thru the variable U_Model. Notice in the header file that this function doesn't have any input parameters and uses the structure in the header file to get the input data.

I have the following 2 questions:

  1. How do I set the structure ExternalInputs_add_two structure in my Python code to pass the data to the add_two_U function?

  2. How do I call the dll function with no parameters add_two_U that I reference thru U_Model in the Python code? If I use the Python statement to call the function:

    result = U_Model()
    

    I get the followin开发者_C百科g error:

    WindowsError: exception: access violation reading 0xFFFFFFFF
    

I've searched on-line for an answer and couldn't find an example of initializing a structure in a header file and calling a function with no parameters.

Notice in my Python code that I am able to call function add_two_initialize thru initiateModel with no errors since this function has input parameters.


add_two_U is not a function, it's an exported value. You need to use in_dll.

See Accessing values exported from dlls.


David,

Thank you for answering my first question. I changed my Python code to this:

#Comment out this line.  add_two_U is not a function
#U_Model = cdll.add_two_win32.add_two_U

#Get the output pointer from add_two_U
output = POINTER(ModelInput)
results = output.in_dll(cdll.add_two_win32,"add_two_U")

print "got results", results.contents()

This code works. I still can't figure out the answer to my first question: How to set initialize the structure ExternalInputs_add_two in the header file from Python code.

I've searched the ctypes documentation and can't find a function or examples on how to do this. I realize it's probably in the documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜