0

My c# program uses Matlab dll and it works find as long as I run it in Debug mode. but as I trying to run it in Release mode it crashes immediately when creating it's instance.

It trows TypeInitializationException.

Thanks for your help

StackTrace:

"at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
 at System.Environment.get_StackTrace()
 at Turbo_Neuron.ANNController..ctor() 
    in C:\\Users\\Eli\\Desktop\\....\\MyProject\\Program.cs:line 17
        at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
        at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
        at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
        at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
        at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
        at System.Activator.CreateInstance(ActivationContext activationContext)
        at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
        at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
        at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
        at System.Threading.ThreadHelper.ThreadStart()"

And it sais:

The type initializer for 'MatlabANNComp.MatlabANN' threw an exception.

StachTrace 2:

   at MatlabANNComp.MatlabANN..ctor()
   at Turbo_Neuron.ANNController..ctor() in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\Matlab_Layer\ANNController.cs:line 49
   at Turbo_Neuron.TNController..ctor(pnl_main form) in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\Logic_Layer\TNController.cs:line 49
   at Turbo_Neuron.pnl_main..ctor() in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\View_Layer\Turbo_Form.cs:line 29
   at Turbo_Neuron.Program.Main() in C:\Users\Eli\Desktop\Work_ANN\ANN\working\Current\Turbo_Neuron_Pro_Filxed_10.09.13_N\Turbo_Neuron\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at System.Activator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Exception messages:

Exception.Message = 
"The type initializer for 'MatlabANNComp.MatlabANN' threw an exception."

InnerException.Message =
{"Could not load file or assembly 'MWArray, Version=2.10.1.0, 
Culture=neutral, PublicKeyToken=e1d84a0da19db86f' 
or one of its dependencies. An attempt was made 
to load a program with an incorrect format."}

InnerException.InnerException = null
Eli
  • 4,576
  • 1
  • 27
  • 39
  • 1
    Please post the full stacktrace and message of the exception - anyway, this exception normally indicates your application can't load the matlab dll. Most likely cause it's not in the search path, or maybe because you also switched platforms – stijn Sep 10 '13 at 07:41
  • 1
    Stacktrace added, hope it halps, thanks! I have only switched from debug to release :( – Eli Sep 10 '13 at 08:46
  • 1
    there should be more than that in the Exception - also look at the InnerExceptions etc. Here's an example of a similar problem, and the kind of exception information we need to figure out what the root cause is: http://www.mathworks.com/matlabcentral/answers/46423 – stijn Sep 10 '13 at 10:11
  • 1
    Ok, I added stacktrace2 to my question. With hope that is what you meant. – Eli Sep 10 '13 at 11:15
  • 1
    no sorry.. the stacktrace is clear enough, it's the exception message that contains the interesting bit. Easiest way to get this is set a breakpoint where you catch the exception, then look at it in the watchwindow and post exception.Message, exception.InnerException.Message, exception.InnerException.InnerException.Message etc if available – stijn Sep 10 '13 at 12:01
  • I added exception messages at the bottom. tell me what need to be delete, if there is some unnecessary information. Thanks – Eli Sep 10 '13 at 12:22

1 Answers1

0

An attempt was made to load a program with an incorrect format

this is the key, and it means you are mixing platforms somwhow. A 32bit process cannot load 64bit dlls and vice-versa. If a process tries to do so anyway, it throws the above exception. You say you only changed from Debug->Release, so it's likely something that has to do with platform changed as well. Compare settings for all projects in the Configuration Manager. They all should be x86 or x64 (not AnyCPU). Which one depends on your matlab dll's platform (set when building it).

stijn
  • 34,664
  • 13
  • 111
  • 163
  • Sorry, I can't find the configuration-manager. I did it at the Properties-Build tab. Doesn't work :( – Eli Sep 11 '13 at 10:24