Accessing members of a structure from a DLL, loaded at runtime in C++

139 views Asked by At

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

0

There are 0 answers