C ++和Matlab Engine之间的连接丢失了

时间:2015-10-18 12:39:48

标签: c++ matlab-engine

我的任务是从C ++ poject调用Matlab函数。我知道有几种方法可以做到这一点,我更喜欢通过Matlab Engine使用它。

  1. 我有几个m文件在Matlab环境中完美运行。

    mymat.m

    function myfig()
        figure;
    end
    
  2. 我在C ++中创建了一个dll-wrapper,用m ++连接m文件。

    dllwrap.h

    #pragma once
    
    #include <Engine.h>
    
    #pragma comment  ( lib,"libmx.lib" )
    #pragma comment  ( lib,"libmat.lib" )
    #pragma comment  ( lib,"libeng.lib" )
    #pragma comment  ( lib,"libmex.lib" )
    
    #ifdef DLLWRAP_EXPORTS
    #define DLLWRAP_API __declspec(dllexport)
    #else
    #define DLLWRAP_API __declspec(dllimport)
    #endif
    
    DLLWRAP_API bool TestDll();
    DLLWRAP_API void MyFigure();
    

    dllwrap.cpp

    #include "stdafx.h"
    #include "dllwrap.h"
    
    Engine* pEng = NULL;
    
    void StartVirtualEngineMatlab()
    {
        if ((pEng = engOpen(NULL)) == NULL)
        {
            MessageBoxA(NULL, (LPSTR)"Can't start MATLAB engine!", (LPSTR) "MatLab Engine: ERROR!", MB_OK | MB_ICONSTOP);
            return;
        };
        return;
    }
    
    void StopedVirtualEngineMatlab()
    {
        engClose(pEng);
        return;
    }
    
    DLLWRAP_API bool TestDll()
    {
        if (pEng == NULL) return false;
        return true;
    }
    
    DLLWRAP_API void MyFigure()
    {
        engEvalString(pEng, "myfig();");
    }
    

    dllmain.cpp

    #include "stdafx.h"
    #include "dllwrap.h"
    
    extern Engine* pEng;
    
    extern void StartVirtualEngineMatlab();
    extern void StopedVirtualEngineMatlab();
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
                     )
    {
        switch (ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
            StartVirtualEngineMatlab();
            break;
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            StopedVirtualEngineMatlab();
            break;
        }
        return TRUE;
    }
    
  3. 现在我专注于测试项目(C#控制台应用程序),通过dll-wraper调用m文件。

    test.cs中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;
    
    namespace Test
    {
        class Program
        {
            [DllImport("dllwrap.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern bool TestDll();
            [DllImport("dllwrap.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern void MyFigure();
    
                    static void Main(string[] args)
                    {
                        bool res = TestDll();
                        if (res == false)
                            return;
    
                        MyFigure();
                    }
                }
            }
    
  4. Test项目正在运行并完成工作,但是存在问题。 Matlab引擎在意外时间崩溃。它可能会在开始时或之后崩溃。我甚至试图在engOpen(NULL)函数之后立即停止断点,但崩溃似乎并不依赖于我的休息。

    我使用Visual Studio 2013,Matlab 2015a 32bit。 请帮忙提供建议。 感谢。

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。 我刚刚删除了该行:

StopedVirtualEngineMatlab();

来自dllmain.cpp

case DLL_PROCESS_DETACH:
    StopedVirtualEngineMatlab();
    break;
}

并将其作为外部函数从Test.cs代码的末尾调用它。

现在它完美无缺。

但我仍然不明白为什么调用DLL_PROCESS_DETACH案例......?