在 visual studio c# 2013 中导入 matlab .dll 导致问题

importing matlab .dll in visual studio c# 2013 causing issue

我正在尝试使用 Matlab R2013a (8.1.0.604) 导出一个 .dll (.Net Assembly) 并使用一个简单的添加函数添加两个数字并尝试在 visual studio 2013 中使用这个 .dll 文件对于 C#。我已经为 Matlabtest.dll 和 MWArray.dll 添加了对 .dll 文件的引用。现在,当我试图使 Class "Adding" 的对象(来自 Matlabtest.dll 的 class 的名称)visual studio 停止工作时没有任何错误并且无法制作 class 的对象。请给我一些参考代码等任何解决方案。我缺少的步骤需要建议和帮助。

这是我的 matlab 函数

function output = adding( a,b )
output = a + b;
end

这是我在单击按钮时的 C# 代码

using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;


using MatlabTest;

namespace TestingMatlab
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MatlabTest.Adding obj;

        public MainWindow()
        {
            InitializeComponent();

        }

        private void btn_sum_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MWArray a;
                MWArray b;

                a = (MWArray)txt_num1.Text;
                b = (MWArray)txt_num2.Text;

                Adding obj = new Adding();

                MWArray output = obj.adding((MWArray)a, (MWArray)b);

                Console.WriteLine(output.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                     
        }
    }
}

所以,在挣扎之后我回来了一个解决方案,以便其他人可以获得帮助。问题实际上出在 MCR Initializer 和 "The MCR instance could not be initialized" 上,我按照以下步骤解决它:

  1. 在 Visual Studio

  2. 中将目标框架更改为 .NET Framework 3.5
  3. WhileExporting .dll using deploytool in Matlab goto settings 和 取消选中将 CTF 存档嵌入应用程序。

  4. 现在,在导出 the.dll 时,您会有一个 projectname.CTF 文件,将此文件复制到您的 Visual Studio 调试目录,例如C:\Program\VS\Debug\projectname.CTF
  5. 检查您是否导出了使用 x86 或 x64 架构的 .dll 以及 然后 select Visual Studio 中的相应平台目标。

按照上述步骤解决了我的问题...:)