weirdgyn
weirdgyn

Reputation: 988

TypeInitia​lizationEx​ception, BadImageFormatException using a .net assembly created in MatLab 2019b 64bit

I need to create a .NET assembly from a Matlab 2019b 64bit script and use it in .NET 4.8 application wrote in Visual Studio 2022 64bit . The script is pretty simple:

function [SNR, PhaseNoise] = Measure_SNR_w_SBX(FullName, FigurePath)
close all
%%Do something with data in file FullName, calculate SNR, PhaseNoise and
%%create a file FigurePath img
end

I created the assembly trough this command:

mcc -W 'dotnet:myAssembly,myClass' -T link:lib Measure_SNR_w_SBX.m

resulting in an assemply exposing, among others, this method:

public MWArray[] Measure_SNR_w_SBX(int numArgsOut, MWArray FullName, MWArray FigurePath)
{
    return mcr.EvaluateFunction(numArgsOut, "Measure_SNR_w_SBX", FullName, FigurePath);
}

The final usage will be an application where I cannot use MWArray or other Matlab types but I'm restricted to standard type (double, strings) so I defined a wrapper assemlby (.NET 4.8) that uses standard types parameters:

public void Measure_SNR_w_SBX(in string FullName, in string FigurePath, out double SNR, out double PhaseNoiseMean)
{   
    MWCharArray mwFullName = new MWCharArray(FullName);
    MWCharArray mwFigurePath = new MWCharArray(FigurePath);
    
    MWNumericArray output = obj.Measure_SNR_w_SBX(2, mwFullName, mwFigurePath);
    
    SNR = output[0].ToScalarDouble();
    PhaseNoiseMean = output[1].ToScalarDouble();
}

Inside the wrapper I instantiate the Matlab produced assembly trough this:

using myAssembly;
using MathWorks.MATLAB.NET.Arrays;
[....]
obj = new myAssembly.myClass();

But when myClass is istantiated what I got is:

System.TypeInitializationException: 'The type initializer for 'myAssembly.myClass' threw an exception.'

which in turn collect three internal exceptions:

TypeInitializationException: The type initializer of 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.
TypeInitializationException: The type initializer of 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception.
BadImageFormatException: Attempt to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Of course myAssembly, mwArray and wrapper assemblies are referenced into the application. Matlab runtime is installed and the Array assembly is placed here:

C:\Program Files\MATLAB\MATLAB Runtime\v97\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll

Also excluding the wrapper assembly and instantiating myAssembly directly into the application lead to the same result. And if I change the call order i.e.:

MWCharArray fullName = new MWCharArray(txtDatFile.Text);
MWCharArray figure_path = new MWCharArray(txtFigureFile.Text);
var obj = new myAssembly.myClass();
output output = obj.Measure_SNR_w_SBX(2, fullName, figurePath);
double snr = output[0].ToScalarDouble();
double phase_noise = output[1].ToScalarDouble();

The exception is thrown on the first line:

MWCharArray fullName = new MWCharArray(txtDatFile.Text);

i.e.:

System.TypeInitializationException: 'The type initializer of 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception.'

that's collecting:

BadImageFormatException: Attempt to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
TypeInitializationException: The type initializer of 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.

How can I fix this?

Upvotes: 1

Views: 30

Answers (1)

weirdgyn
weirdgyn

Reputation: 988

I found the problem:

Application properties

the bitness of the application is forced to 32bit even if the AnyCPU model is applied if this checkbox is checked (it translate to Perfer 32bit)

Upvotes: 0

Related Questions