Matlab Engine API,用于C运行时动态链接

时间:2018-11-30 11:09:44

标签: c matlab dll matlab-engine

我在C应用程序中使用Matlab Engine API,一切正常,但现在我想使用LoadLibrary()函数将其从加载时动态链接更改为运行时动态链接。

我可以加载库并获取所需功能的地址,但是当我尝试打开引擎时,出现访问冲突:

  • “ myProgram.exe中0x002fa001处未处理的异常:0xC0000005:访问冲突读取位置0x000000fe。”

这是我函数的第一部分:

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*/
}

1 个答案:

答案 0 :(得分:0)

我发现我使用了错误的函数名。我不知道有什么区别,但是有一个ENGOPEN和engOpen函数。显然只有小写字母起作用。