Exception when initialize dll in c# generated by M

2020-02-13 06:12发布

I used the MATLAB Compiler to generate a .NET Assembly with a very little MATLAB code:

function output_arg = extest( input_arg1,input_arg2 )
    output_arg = input_arg1+input_arg2;
end

I generated the dll with the wizard.

Within my Visual Studio project I added the reference to the generated dll (extest.dll) and to the MATLAB Runtime dll (C:\Program Files\MATLAB\MATLAB Runtime\v92\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll) as mentioned in the "Assembly Description".

This is my c# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MathWorks.MATLAB.NET.Utility;
using extest;

namespace DllTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            ExClass e1 = new ExClass();
        }
    }
}

It builds without errors an intelisense is working (so all references should be good from my understanding)

But when I launch it, the following exception gets thrown (on new ExClass()):

An unhandled exception of type 'System.TypeInitializationException' occurred in DllTesting.exe

Additional information: The type initializer for 'extest.ExClass' threw an exception.

Any suggestions what is wrong with this code or whats missing?

1条回答
别忘想泡老子
2楼-- · 2020-02-13 07:14

Try adding this before the class definition

[assembly: MathWorks.MATLAB.NET.Utility.MWMCROption("-nojit")]

Also make sure that the .NET version you use for assembly is the same or lower than the one used for your Visual Studio project.

Another solution might be adding the path of the MATLAB runtime (e.g. C:\Program Files\MATLAB\MATLAB Runtime\v92\runtime\win64) to the PATH Environment Variable.

If none of these helps, have a look here and here, you might have a 64/32 bit mismatch.

查看更多
登录 后发表回答