0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

hmm if I add the mentioned line I get the following compiler error: Error CS1730 Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations. And it's all setted to 64bit.
I just did now. No compiler error anymore. But the issue still exists. :/
Hmm. You might have to try different compiler options on Matlab.. I am not sure what else to recommend other than what I have already suggested..
thx anyway. I managed to run it now. I compiled the code a couple of times again AND changed the .net framework to 4.0 insted of 4.6.2 (as it was default for me). As far as I understood the matlab docs it should work on 4.0 and above ... it seams not. So I'm not 100% sure if its because of the re compiling or just magic :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.