【问题标题】:Matlab Engine API for C run-time dynamic linking用于 C 运行时动态链接的 Matlab 引擎 API
【发布时间】:2025-12-11 17:35:02
【问题描述】:

我在 C 应用程序中使用 Matlab 引擎 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*/
}

【问题讨论】:

    标签: c matlab dll matlab-engine


    【解决方案1】:

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

    【讨论】: