TypeInitialization exception in using MWArray.dll

4 views (last 30 days)
Sir, I have created a dotnetcomponet (PolyFitDotNetComponent.dll) with Matlab NE Builder using Microsoft SDK Compiler. But when i am using it from a sample application, it is raising type initialization exception. The error is something like this:
System.TypeInitializationException was unhandled Message=The type initializer for 'PolyFitDotNetComponent.PolyFitDotNetComponentclass' threw an exception. Source=PolyFitDotNetComponent TypeName=PolyFitDotNetComponent.PolyFitDotNetComponentclass StackTrace: at PolyFitDotNetComponent.PolyFitDotNetComponentclass..ctor() at ConsoleApplication11.Program.Main(String[] args) in C:\Users\I7A\Documents\Visual Studio 2010\Projects\ConsoleApplication11\ConsoleApplication11\Program.cs:line 13 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.TypeInitializationException Message=The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception. Source=PolyFitDotNetComponent TypeName=MathWorks.MATLAB.NET.Arrays.MWArray StackTrace: at PolyFitDotNetComponent.PolyFitDotNetComponentclass..cctor() InnerException: System.BadImageFormatException Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) Source=MWArray StackTrace: at MathWorks.MATLAB.NET.Utility.MWMCR.mclmcrInitialize() at MathWorks.MATLAB.NET.Utility.MWMCR.InitializeApplication(String[] startupOptions) at MathWorks.MATLAB.NET.Arrays.MWArray..cctor() InnerException:

Answers (1)

Madheswaran
Madheswaran on 25 Mar 2025
Hi Mahesh,
This error is very likely related to a 32/64 bit architecutre mismatch between your MATLAB Compiled component and the application trying to use it. The key clue is the 'BadImageFormatException' with HRESULT:0x8007000B, which typically indicates an attempt to load a 64-bit DLL in a 32 bit process (or vice-versa).
Here are some solutions you can try:
Change your application's platform target in Visual Studio:
  • Open your project in Visual Studio
  • Go to Configuration Manager
  • Change the platform to 'x64' (assuming your MATLAB component is 64-bit)
Check MATLAB Runtime installation:
  • Ensure you have the MATLAB Runtime installed and make sure the architecure (32/64 bit) matches with your application
You can see the similar issues are being addressed in the following answers:
  1. https://www.mathworks.com/matlabcentral/answers/373215-why-do-i-receive-unhandled-exception-of-type-system-typeinitializationexception-when-using-net-as
  2. https://www.mathworks.com/matlabcentral/answers/384932-why-do-i-get-type-initializer-throws-an-exception-error-every-time-the-compiled-class-is-instantia
Hope this helps!

Community Treasure Hunt

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

Start Hunting!