dll Matlab实例在c#中运行release时抛出异常

Posted

技术标签:

【中文标题】dll Matlab实例在c#中运行release时抛出异常【英文标题】:dll Matlab instance throw an exception while running release in c# 【发布时间】:2013-09-10 07:01:03 【问题描述】:

我的 c# 程序使用 Matlab dll,只要我在调试模式下运行它就可以找到它。 但是当我尝试在发布模式下运行它时,它会在创建实例时立即崩溃。

它引发 TypeInitializationException。

感谢您的帮助

堆栈跟踪:

"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()"

它说:

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.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

【问题讨论】:

请发布异常的完整堆栈跟踪和消息 - 无论如何,此异常通常表明您的应用程序无法加载 matlab dll。很可能是因为它不在搜索路径中,或者可能是因为您还切换了平台 添加了 Stacktrace,希望它停止,谢谢!我只是从调试切换到发布:( Exception 中应该不止这些 - 还要查看 InnerExceptions 等。这是一个类似问题的示例,以及我们需要找出根本原因的异常信息类型: mathworks.com/matlabcentral/answers/46423 好的,我在我的问题中添加了 stacktrace2。希望这就是你的意思。 不抱歉.. 堆栈跟踪很清楚,它是包含有趣位的异常消息。最简单的方法是在捕获异常的地方设置一个断点,然后在监视窗口中查看它并发布 exception.Message、exception.InnerException.Message、exception.InnerException.InnerException.Message 等(如果有) 【参考方案1】:

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

这是关键,这意味着您正在混合平台。 32 位进程无法加载 64 位 dll,反之亦然。如果一个进程无论如何都试图这样做,它会抛出上述异常。 你说你只是从 Debug->Release 改变的,所以它很可能与平台改变有关。比较配置管理器中所有项目的设置。它们都应该是 x86 或 x64(不是 AnyCPU)。哪一个取决于您的 matlab dll 的平台(在构建时设置)。

【讨论】:

对不起,我找不到配置管理器。我是在 Properties-Build 选项卡上完成的。不工作:(

以上是关于dll Matlab实例在c#中运行release时抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

C#&MatLab:我是不是需要 MCR 才能在任何计算机上运行 MWArray.dll?

如何干净地停止运行 matlab dll 函数的 .NET 线程

c#中怎样运行调用的dll文件

在C#中Debug和Release两种调试方法出来结果不同,求问为啥?

C#自动引用Debug | Release版本的dll

C#自动引用Debug | Release版本的dll