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

14 ビュー (過去 30 日間)
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);
}
I need to use into an application that cannot use MWArray or other Matlab types 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 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?
I managed correctly input output parameters?
I would like to use the wrapper assembly also in a 32 bit application.. there's some reccomendation?
  1 件のコメント
Michele
Michele 2025 年 2 月 17 日 15:04
I also tried to specify the .net framework 4.8 in mcc line.. nothing changes

サインインしてコメントする。

採用された回答

Michele
Michele 2025 年 2 月 17 日 15:27
I found the problem:
The highlighted option force executable to 32bit even if AnyCpu or 64bit configuration is selected.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDeploy to .NET Applications Using MWArray API についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by