我正在C应用程序中使用Matlab引擎API,一切正常,但现在我想使用LoadLibrary()函数将其从加载时动态链接更改为运行时动态链接。
我可以加载库并获取所需函数的地址,但当我尝试打开引擎时,会遇到访问冲突:
“myProgram.exe中0x002fa001处的未处理异常:0xc000005:访问冲突读取位置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*/
}

最佳答案

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

10-06 15:13