简体   繁体   中英

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?

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#

I'm working through the same problem and, in my case, it appears to be something in a .csproj file, strange as it sounds.

In general I suggest the following be tried: Create a new Windows Forms app and add the enclosed code. Don't forget to add the reference to MWArray.dll. Run the code and if an exception is thrown then you have an installation problem with the Matlab runtime or the x86 / x64 versions of the runtime and built application don't match. (You indicate you already did this but this is a general answer designed to help others also)

Add the enclosed code to program.cs of your application and see if it throws an exception. Continue progressing towards the DLL that throws the exception until an exception is thrown.

When an exception is finally thrown, verify that the x86 / x64 of the project matches that of the Matlab runtime.

If the x86 / x64 versions match, create a new DLL project with a different class name whose constructor has the same signature as the failing DLL. Add the enclosed code to the new DLL, unwire the failing DLL and wire in this new DLL, and run the application. If the code doesn't throw an exception then you probably have a problem in the .csproj file or another DLL. Copy and paste chunks of code from the failing DLL into the new one and retest for an exception. Continue until all code has been copied or until the exception returns. If the exception returns, continue troubleshooting to find the offending line of code. If all code has been copied then you have a functioning DLL. You should be able to take it from there to get rid of the failing DLL and replace it with the new functioning DLL.

I hope this helps! Zack

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


try
{
    MWNumericArray oMW = new MWNumericArray();
}
catch (Exception ex)
{

}

So this magically started happening on a Windows server in which nothing was changed on the server for weeks.

The solution for me, after ripping my eyes out, was to set the "Enable 32-Bit Applications" setting in the web sites application pool to FALSE . Once I did that, everything worked and I placed my eye balls back into their sockets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM