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

11 次查看(过去 30 天)
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?

采纳的回答

Animesh
Animesh 2024-10-6
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 个评论
Andy
Andy 2024-10-9
编辑:Andy 2024-10-9
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 COM Component Integration 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by