Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MATLAB MWArray.dll in C# Class Library

I'm trying to use .dll, built in MATLAB with Matlab .net Complier, in C# Class Library, but program throws an exception every time object from MWArray.dll is initialized, for example:

MWNumericArray m = new MWNumericArra(10,10);

Exception:

System.TypeInitializationException was caught
  HResult=-2146233036
  Message=The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' threw an exception.
  Source=MWArray
  TypeName=MathWorks.MATLAB.NET.Arrays.MWNumericArray
  StackTrace:
       at MathWorks.MATLAB.NET.Arrays.MWNumericArray.op_Implicit(Double[] values)
       at VolCalc.Vol.CalculateVolatility(Double[] data)
  InnerException: System.TypeInitializationException
       HResult=-2146233036
       Message=The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWIndexArray' threw an exception.
       Source=MWArray
       TypeName=MathWorks.MATLAB.NET.Arrays.MWIndexArray
       StackTrace:
            at MathWorks.MATLAB.NET.Arrays.MWIndexArray..ctor()
            at MathWorks.MATLAB.NET.Arrays.MWNumericArray..ctor(Double scalar)
            at MathWorks.MATLAB.NET.Arrays.MWNumericArray.get__Inf()
            at MathWorks.MATLAB.NET.Arrays.MWNumericArray..cctor()
       InnerException: System.Security.SecurityException
            HResult=-2146233078
            Message=Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
            Source=MWArray
            StackTrace:
                 at MathWorks.MATLAB.NET.Utility.MWSafeHandle..ctor()
                 at MathWorks.MATLAB.NET.Arrays.MWArray..ctor()
                 at MathWorks.MATLAB.NET.Arrays.MWIndexArray..ctor()
                 at MathWorks.MATLAB.NET.Arrays.MWNumericArray..ctor(MWArrayComponent arrayComponent, Int32 rows, Int32 columns)
                 at MathWorks.MATLAB.NET.Arrays.MWIndexArray..cctor()
            InnerException: 

I tried to do this in Console App, and works fine, but I need .dll that uses .dll built in matlab.

Any ideas?

like image 286
user3588594 Avatar asked Mar 19 '23 17:03

user3588594


1 Answers

What helped me was to make sure the C# application and the matlab modules are under the same architecture - Meaning 32 or 64 bit.

If your Matlab is 64 bits - Try changing the C# project to 64 bit

See this post: Creating a shared library (.NET Assembly) in Matlab and using it in C#

like image 128
Nitay Avatar answered Mar 22 '23 05:03

Nitay