简体   繁体   English

在C#类库中使用MATLAB MWArray.dll

[英]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: 我正在尝试在C#类库中使用带有Matlab .net Complier的MATLAB构建的.dll,但是每次初始化MWArray.dll的对象时程序都会抛出异常,例如:

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. 我尝试在Console App中执行此操作,并且工作正常,但我需要.dll使用matlab内置的.dll。

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. 帮助我的是确保C#应用程序和matlab模块在相同的架构下 - 意思是32位或64位。

If your Matlab is 64 bits - Try changing the C# project to 64 bit 如果您的Matlab是64位 - 尝试将C#项目更改为64位

See this post: Creating a shared library (.NET Assembly) in Matlab and using it in C# 请参阅以下文章: 在Matlab中创建共享库(.NET程序集)并在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. 我正在解决同样的问题,在我的情况下,它似乎是.csproj文件中的内容,听起来很奇怪。

In general I suggest the following be tried: Create a new Windows Forms app and add the enclosed code. 一般情况下,我建议尝试以下方法:创建一个新的Windows窗体应用程序并添加附带的代码。 Don't forget to add the reference to MWArray.dll. 不要忘记添加对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. 运行代码,如果抛出异常,那么Matlab运行时的安装问题或运行时的x86 / x64版本与构建的应用程序不匹配。 (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. 将附带的代码添加到应用程序的program.cs中,看看它是否会引发异常。 Continue progressing towards the DLL that throws the exception until an exception is thrown. 继续前进到抛出异常的DLL,直到抛出异常。

When an exception is finally thrown, verify that the x86 / x64 of the project matches that of the Matlab runtime. 当最终抛出异常时,验证项目的x86 / x64是否与Matlab运行时的x86 / x64匹配。

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. 如果x86 / x64版本匹配,请创建一个具有不同类名的新DLL项目,其构造函数与失败的DLL具有相同的签名。 Add the enclosed code to the new DLL, unwire the failing DLL and wire in this new DLL, and run the application. 将附带的代码添加到新的DLL中,将失败的DLL解除连接并连接到这个新的DLL,然后运行该应用程序。 If the code doesn't throw an exception then you probably have a problem in the .csproj file or another DLL. 如果代码没有抛出异常,那么您可能在.csproj文件或其他DLL中遇到问题。 Copy and paste chunks of code from the failing DLL into the new one and retest for an exception. 将失败的DLL中的代码块复制并粘贴到新的DLL中,然后重新测试异常。 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. 如果所有代码都已复制,那么您将拥有一个正常运行的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. 您应该能够从那里取出它以摆脱失败的DLL并将其替换为新的功能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. 所以这神奇地开始发生在Windows服务器上,几周内服务器上没有任何变化。

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 . 我的解决方案就是将网站应用程序池中的“启用32位应用程序”设置设置为FALSE Once I did that, everything worked and I placed my eye balls back into their sockets. 一旦我这样做,一切正常,我把眼球放回插座。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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