首页 > 代码库 > .net 调用 Matlab生成dll出现的问题(The type initializer for 'MathWorks?.MATLAB.NE?T.Utility.?MWMCR' threw an exception.)

.net 调用 Matlab生成dll出现的问题(The type initializer for 'MathWorks?.MATLAB.NE?T.Utility.?MWMCR' threw an exception.)

https://cn.mathworks.com/matlabcentral/answers/278399-i-get-an-error-saying-the-type-initializer-for-mathworks-matlab-net-utility-mwmcr-threw-an-except

这篇文章帮忙解决了问题

记录一下备忘:

 

Hi Nug,

Ensure that you have the MCR version corresponding to MATLAB 2012b installed. You can check that from the following link.

MATLAB Runtime

After ensuring that you have the correct version of MCR, it will be useful to check the system path. If you have either MATLAB or any other version of MCR installed on your machine, please make sure that the one you are running the deployable against appears higher in the system path than any other MATLAB/MCR runtime.

To view or change environment variables:

  1. Right-click My Computer, and then click Properties.
  2. Click the Advanced tab.
  3. Click Environment variables.
  4. In the ‘System Variables‘ search for the variable named ‘Path‘ and ensure that the MCR installation for R2012b appears higher than in the path than any other MATLAB/MCR runtime

Also, are you using the Web installed MCR or the packaged MCR?

There might be two possible causes for the error to appear:

A) This error can appear if you copied the MATLAB Runtime DLL mclmcrrt8_0.dll to the folder that contains your .NET application or to some other Windows system folder. If you copy this MATLAB DLL to another folder, when it tries to find its further dependencies it will fail.

To work around this issue, remove any unnecessary copies of the file and leave the file in the installation folder belonging to MATLAB or the MCR.

B) The issue might be due to the fact that the packaged MCR installation is not adding the MWArray properly to the machine Global Assembly Cache (GAC). So in such a case, the deployed component picks up the older MWArray already installed in the GAC and fails. The GAC preempts other references so even if you specifically reference the R2015a MWArray by browsing to it and referencing it in Visual Studio, it may not work.

In order to workaround this issue you would need to either:

  1. Uninstall the other MWArrays from the GAC (so you make sure it picks up the referenced MWArray)
  2. Add the R2012b MWArray into the GAC using Microsoft‘s gacutil.exe. More information about this can be seen from the link below :

http://msdn.microsoft.com/en-us/library/dkkx7f79(v=vs.110).aspx

I hope either of the above workarounds helps you to resolve this issue.

-Rohit

 

.net 调用 Matlab生成dll出现的问题(The type initializer for 'MathWorks?.MATLAB.NE?T.Utility.?MWMCR' threw an exception.)