MCR and .NET incompatibility
Asked Answered
B

1

8

I am trying to compile a Matlab (R2010b) application that uses a .NET module, but I am facing a problem with incompatibility between the MCR and the .NET module:

  • The .NET module is compiled with Visual Studio 2010.
  • MCR is configured to use Visual Studio 2010.
  • The application also contain a few Mex-files that are built with Visual Studio 2010 and they work just fine both in Matlab and with MCR.

If I load the assembly from the Matlab cli everything work just fine, but once I compile the app and run it from cmd.exe an error is thrown stating that the assembly is built with a runtime that is newer than the one currently loaded. I think that Matlab R2010b is built with Visual Studio 2008 and believe that this is the problem, but I wonder if anyone has a solution to the problem?

Blimp answered 13/12, 2010 at 12:49 Comment(3)
From mathworks.com/help/techdoc/matlab_external/brpb5k6-1.html: "The MATLAB interface supports the features of the .NET Framework Version 2.0, and works with Version 2.0 and its compatible versions (Versions 3.0 and 3.5). MATLAB supports loading Framework Version 4.0 assemblies if you have Version 4.0 installed on your system. However, Version 4.0-specific features have not been tested."Earnest
It is not about VS version but for which target framework the assemblies are compiled. With both VS2008 and VS2010 you can compile for all .NET versions, inclusive older one. If you are not using specific .NET 4 features then you could try compile to .NET 3.5Earnest
But the assembly work just fine if I load it from the cli, so I don't think the problem is incompatible features in .NET 4.0.Blimp
L
7

The solution is to provide an application config file(foo.exe.config for application called foo.exe) next to the compiled exe with the following entry:

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
 </configuration>

MATLAB .NET interface is built with .NET framework 2.0, which means that in the absence of an app config file the 2.0 CLR is loaded. When running in MATLAB the <supportedRuntime> entries from the config file tells MATLAB to load 4.0 CLR if available.

Ludovico answered 1/7, 2011 at 17:2 Comment(2)
Nice to know! Could you perhaps provide a link to the original source of this documentation? I assume it is part of the .NET framework?Blimp
@Blimp Here is the link from msdn about supportedRuntime link. useLegacyV2RuntimeActivationPolicy has been discussed in link which is important when using a mixed mode assembly(C++/CLI) in .NET 4.0. I also forgot to mention that MATLAB already ships a configuration file, MATLAB.exe.config in $MATLABROOT\bin\$ARCH which is why things work in MATLAB.Ludovico

© 2022 - 2024 — McMap. All rights reserved.