0

I have the following problem: At runtime I load a Matlab-generated DLL of a Simulink model, which contains structs for the outputs and inputs of the model and some methods.

Example of the generated structs and methods (the name of the model is "Test_Scalar"):

typedef struct {
  real_T Input_1;                    /* '<Root>/Input_1' */
  real_T Input_2;                    /* '<Root>/Input_2' */
} ExtU_Test_Scalar_T;

/* External outputs (root outports fed by signals with default storage) */
typedef struct {
  real_T Output;                      /* '<Root>/Output' */
} ExtY_Test_Scalar_T;

/* Real-time Model Data Structure */
struct tag_RTM_Test_Scalar_T {
  const char_T * volatile errorStatus;
};

/* External inputs (root inport signals with default storage) */
extern ExtU_Test_Scalar_T Test_Scalar_*;

/* External outputs (root outports fed by signals with default storage) */
extern ExtY_Test_Scalar_T Test_Scalar_Y;

/* Model entry point functions */
extern void Test_Scalar_initialize(void);
extern void Test_Scalar_step(void);
extern void Test_Scalar_terminate(void);

Within my C++ program I can load and execute the init function and I also get a pointer to the structure. However, I am not aware how it is possible to access the members of the structures. Does anyone here have an idea?

int main() {
    HMODULE hModule = LoadLibraryA("C:\\....\\Test_Scalar_win64.dll");
    assert(hModule);

    FARPROC initialize = GetProcAddress(hModule, "Test_Scalar_initialize");
    assert(initialize);
    initialize();

    FARPROC inputs= GetProcAddress(hModule, "Test_Scalar_U");
    assert(inputs);

    //accessing members of input struct here
}

Best regards
Chris

  • 1
    just cast it to `ExtU_Test_Scalar_T*`? – Alan Birtles Oct 21 '20 at 12:56
  • I think the type "ExtU_Test_Scalar_T" is not known because I do not include a header. The access should be as generic as possible, so I only know how many members the structure has and what type they have. – lechrizzle Oct 21 '20 at 13:22
  • Why dont you include the header? – Alan Birtles Oct 21 '20 at 13:25
  • We want to implement a generic C++ file that serves as a JNI access point from Java for any Simulink model. To this class we want to pass only the number and types of inputs and outputs (and of course the path to the dynamically loaded DLL file and the name of the model on which the naming of the generated methods and structures is based). This C++ file should also be converted into a DLL once. This DLL will be called from Java via JNI. Therefore the access to the structures should be done at runtime, without header include at compile time. – lechrizzle Oct 21 '20 at 14:09
  • then you'll have to write code to calculate the structure offsets and memcpy the individual members, if you want to do it more reliably it'd be better to export accessor methods – Alan Birtles Oct 21 '20 at 14:28

0 Answers0