Borrar filtros
Borrar filtros

System.Bad​ImageForma​tException​: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B), Source=Mat​hWorks.MAT​LAB.Types

31 visualizaciones (últimos 30 días)
Reproduce:
Within a Visual Studio Windows Console app, add references to:
C:\Program Files\MATLAB\R2024b\extern\dotnet\netstandard2.0\MathWorks.MATLAB.Engine.dll
C:\Program Files\MATLAB\R2024b\extern\dotnet\netstandard2.0\MathWorks.MATLAB.Runtime.dll
C:\Program Files\MATLAB\R2024b\extern\dotnet\netstandard2.0\MathWorks.MATLAB.Types.dll
within your static void Main(string[] args) function, add:
MATLABEngine engine = MATLABEngine.StartMATLAB();
This will produce:
System.BadImageFormatException
HResult=0x8007000B
Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
Source=MathWorks.MATLAB.Types
StackTrace:
at MathWorks.MATLAB.Types.SafeHandles.ArrayFactoryHandle.array_factory_create_v2(ArrayFactoryHandle& factory)
at MathWorks.MATLAB.Types.SafeHandles.ArrayFactoryHandle.ArrayFactoryCreate()
at MathWorks.MATLAB.Types.Impl.MATLABArrayFactoryImpl..ctor()
at MathWorks.MATLAB.Types.MATLABArrayFactory.get_Default()
at MathWorks.MATLAB.Engine.MATLABEngineFactory.get_Default()
at MathWorks.MATLAB.Engine.MATLABEngine.StartMATLAB()
Any ideas what's the problem?

Respuesta aceptada

Animesh
Animesh el 6 de Oct. de 2024
Hey @Andy,
NET assemblies from MATLAB Builder NE are compiled into architecture-independent MSIL but have platform-dependent dependencies like DLLs or MEX-files. Thus, assemblies from 32-bit MATLAB can only be referenced in x86 projects. On a 64-bit server, a 32-bit web application is typically launched by a 64-bit process, but requires a 32-bit process due to its dependencies. Here are a few things you can try to resolve the exception:
  1. Compile the .NET assembly with 64-bit MATLAB Builder NE for 64-bit dependencies.
  2. Configure the server to run both 64-bit and 32-bit processes, ensuring 32-bit applications use a 32-bit process.
You can refer to the following thread; it discusses a similar issue.
  2 comentarios
Andy
Andy el 8 de Oct. de 2024 a las 13:05
indeed, problem got fixed by disabling prefer 32-bit setting under Build prferences:
Andy
Andy el 9 de Oct. de 2024 a las 18:39
Editada: Andy el 9 de Oct. de 2024 a las 18:53
Hi @Animesh, do you happen to know why multiple subsequent calls into:
dynamic matlab = MATLABEngine.ConnectMATLAB("netinstance");
would fail, the first call would succeed, but then if you call again (for a new variable assignment) perhaps from a different thread (or you lost access to var matlab first initialized), any subsequent calls would trigger:
MathWorks.MATLAB.Exceptions.MATLABNotAvailableException: An unexpected error occurred.
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ThrowOnErrFlag(bool) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 46
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ConnectMATLAB(string) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 118
+ MathWorks.MATLAB.Engine.MATLABEngine.ConnectMATLAB(string) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/MATLABEngine.cs : 118
Equally, should you execute:
MATLABEngine.TerminateEngineClient();
then you would not even be able to run:
string[] mSessions = MATLABEngine.FindMATLAB();
MathWorks.MATLAB.Exceptions.MATLABNotAvailableException: The MATLAB session has been terminated.
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ThrowIfTerminated() at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 52
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.FindMATLAB() at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 80
+ MathWorks.MATLAB.Engine.MATLABEngine.FindMATLAB() at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/MATLABEngine.cs : 83
anymore, how do you recover from this within the execution flow of a program?
It makes no sense ... and it's impossible to work with this in Visual Studio 2022 - for any new Build/Compile/Run, you just need to restart Visual Studio ... as you lose access to the dynamic variable matlab ... and re-running again StartMATLAB / ConnectMATLAB will error.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre COM Component Integration en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by