I get an error saying 'The type initializer for 'MathWorks​.MATLAB.NE​T.Utility.​MWMCR' threw an exception.' when trying to use matlab dll in visual studio

32 views (last 30 days)
Using Matlab 2012b deploytool I converted my matlab code into .NET assembly and then added the dll as a reference in my .NET project.
When I create a new instance of the custom class that represents the matlab dll I get an error saying 'The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.'
I have a 64 bit machine and I am running Visual Studio 2015. Any suggestions?

Accepted Answer

Rohit Kudva
Rohit Kudva on 14 Apr 2016
Hi Nug,
Ensure that you have the MCR version corresponding to MATLAB 2012b installed. You can check that from the following link.
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 :
I hope either of the above workarounds helps you to resolve this issue.
-Rohit
  2 Comments
rob
rob on 24 Jun 2016
I had this error when i switched from 2010b to 2014a. Installshield was now packaging mclmcrrt8_3.dll:
Adding file 'mclmcrrt8_3.dll' that is a dependency of component 'LTS.exe'
Having this file in the install directory was causing this error despite the fact that the directory the dll was taken from is on my path environment variable.
To fix this in Installshield LE:
Files tab,
right click on the exe,
select "Dependencies from scan at build...",
uncheck mclmcrrt8_3.dll.
raviteja kilaru
raviteja kilaru on 13 Mar 2023
Even after following your steps still facing the same error.
Kindly suggest any changes so that I can get rid of this error.
Visual studio 2019,MCR 9.6

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB Compiler SDK in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!