Matlab Engine API for C run-time dynamic linking

1

I'm using the Matlab Engine API in a C application and everything works fine but I now want to change it from load-time dynamic linking to run-time dynamic linking using the LoadLibrary() function.

I can load the library and get the address of the functions i need but when i try to open the engine i get an access violation :

  • "Unhandled exception at 0x002fa001 in myProgram.exe: 0xC0000005: Access violation reading location 0x000000fe.".

Here is what the first part of my function looks like:

void callMatlabFunction(struct matLabIO *mIO, struct auxdata *aux){
    static Engine *ep;
    static int firstMatlabCall = 1;
    typedef Engine *(*engOpen)(const char*);
    static engOpen engOpen_ = NULL;
    typedef int (*engEvalString)(Engine*, const char*);
    static engEvalString engEvalString_ = NULL;
    typedef int (*engPutVariable)(Engine*, const char*, const mxArray*);
    static engPutVariable engPutVariable_ = NULL;
    typedef mxArray* (*engGetVariable)(Engine*, const char*);
    static engGetVariable engGetVariable_ = NULL;
    typedef int (*engClose)(Engine*);
    static engClose engClose_ = NULL;

    if (firstMatlabCall){
    HINSTANCE engLib = LoadLibrary("LIB/libeng.dll");
    if (engLib){
        engOpen_ = (engOpen)GetProcAddress(engLib, "ENGOPEN");
        engEvalString_ = (engEvalString)GetProcAddress(engLib, 
        "ENGEVALSTRING");
        engPutVariable_ = (engPutVariable)GetProcAddress(engLib, 
        "ENGPUTVARIABLE");
        engGetVariable_ = (engGetVariable)GetProcAddress(engLib, 
        "ENGGETVARIABLE");
        engClose_ = (engClose)GetProcAddress(engLib, "ENGCLOSE");
        /* All of these return valid addresses */
    }
    else{
        printf("The MatLab Engine DLL cannot be located. Make sure it 
        is located in your LIB folder");
    }
    if (!(ep = engOpen_(NULL))) {
        printf("Can't start MATLAB engine");
        /* ERROR OCCURS HERE */
    }
    firstMatlabCall = 0;
    }
    /*conversion of variables to mxArrays and call to matlab function*/
}
c
matlab
dll
matlab-engine
asked on Stack Overflow Nov 30, 2018 by Alexander Vandenberghe • edited Nov 30, 2018 by Alexander Vandenberghe

1 Answer

0

I found out i was using the wrong function names. I am not aware what the difference is but there is an ENGOPEN and an engOpen function. apparently only the lower case ones work.


User contributions licensed under CC BY-SA 3.0