Using MATLAB MWArray.dll in C# Class Library

孤者浪人 提交于 2019-12-10 10:03:53

问题


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?


回答1:


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#




回答2:


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)
{

}



回答3:


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.



来源:https://stackoverflow.com/questions/23384629/using-matlab-mwarray-dll-in-c-sharp-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!