如何避免 System.Runtime.InteropServices.COMException?

Posted

技术标签:

【中文标题】如何避免 System.Runtime.InteropServices.COMException?【英文标题】:How to avoid a System.Runtime.InteropServices.COMException? 【发布时间】:2011-05-15 22:47:55 【问题描述】:

在我的 Microsoft Surface 项目中,我总是遇到很多以下异常:

'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\MuReSe\MuReSe\bin\Debug\MuReSe.vshost.exe'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Data.DataSetExtensions\3.5.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\WindowsBase\3.0.0.0__31bf3856ad364e35\WindowsBase.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_32\PresentationCore\3.0.0.0__31bf3856ad364e35\PresentationCore.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\PresentationFramework\3.0.0.0__31bf3856ad364e35\PresentationFramework.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.Surface\1.0.0.0__31bf3856ad364e35\Microsoft.Surface.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.Surface.Presentation\1.0.0.0__31bf3856ad364e35\Microsoft.Surface.Presentation.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.Surface.Presentation.Generic\1.0.0.0__31bf3856ad364e35\Microsoft.Surface.Presentation.Generic.dll'
The thread 'vshost.NotifyLoad' (0x1504) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x131c) has exited with code 0 (0x0).
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\MuReSe\MuReSe\bin\Debug\MuReSe.exe', Symbols loaded.
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'
Step into: Stepping over method without symbols 'MuReSe.App.App'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.Surface.Shell.ShellApi\1.0.0.0__31bf3856ad364e35\Microsoft.Surface.Shell.ShellApi.dll'
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.Surface.Common\1.0.0.0__31bf3856ad364e35\Microsoft.Surface.Common.dll'
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Shell.ShellApi.dll
A first chance exception of type 'Microsoft.Surface.Common.ProcessCommunicationException' occurred in Microsoft.Surface.Shell.ShellApi.dll
A first chance exception of type 'System.Threading.WaitHandleCannotBeOpenedException' occurred in mscorlib.dll
'MuReSe.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.Surface.Core\1.0.0.0__31bf3856ad364e35\Microsoft.Surface.Core.dll'
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Surface.Core.dll

最后一个异常一直被抛出,直到我停止程序。我已经注释掉了我的所有代码,并且我运行应用程序时只使用了已经内置的代码,但同样的情况发生了。

我该如何纠正?我只找到了这个参考:

http://www.mobydisk.com/softdev/techinfo/dotnetexceptions.html

上面写着:

当一个 无法识别的 HRESULT 从 COM 方法调用。

但我不知道这是什么意思....

====编辑====

我按照下面答案中的建议来调试异常。这是详细报告:

System.Runtime.InteropServices.COMException occurred
  Message=Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  Source=Microsoft.Surface.Shell.ShellApi
  ErrorCode=-2147221164
  StackTrace:
       at Microsoft.Surface.Shell.NativeMethods.CoCreateInstance(Guid rclsid, Object pUnkOuter, CLSCTX dwClsContext, Guid riid)
  InnerException: 

调试器在构造函数调用处停止:

 public MainWindow()
        
            InitializeComponent();

            // Add handlers for Application activation events
            AddActivationHandlers();

           // AssemblyParser parser = new AssemblyParser();
            //parser.parse();

           // DirSearch(@"C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\TestClass.dll\");
        

我在构造函数的开头就停了:

public MainWindow()

【问题讨论】:

顺便说一下,确保所有代码都引用相同的架构。 【参考方案1】:

您的代码(或您调用的某些代码)正在调用返回未知值的 COM 方法。如果你能找到它,那么你就成功了一半。

您可以在抛出异常时尝试中断。转到 Debug > Exceptions... 并使用 Find... 选项找到 System.Runtime.InteropServices.COMException。勾选抛出时中断的选项,然后调试您的应用程序。

希望它会在有意义的地方中断,您将能够追溯并找到错误的根源。

【讨论】:

好的,谢谢。这真的很奇怪,因为我通过注释掉我所有的代码来测试它。然而,这正在发生。还有一件事:我认为它必须是我正在使用的第三方库,那么如果它位于那里,我该如何解决问题?只是捕获异常? @Roflcoptr - 如果它在第 3 方代码中,那么你所能做的就是抓住它或离开它,离开它并不是一个真正的选择。如果您可以将其缩小到特定调用,请将其报告给第 3 方开发者。 谢谢!我按照您的建议调试了我的项目。我仍然很好奇,因为它被扔进了构造函数中,如果我没有改变任何东西...... 对于 VS 2015 调试 > Windows > 异常设置 (Ctrl + Alt + E)【参考方案2】:

我在打开项目解决方案时遇到了System.Runtime.InteropServices.COMException。有时用户没有足够的权限来运行一些COM 方法。我以管理员身份运行 Visual Studio,异常消失了。

【讨论】:

我和他一样,是的,诀窍是以管理员身份运行 VS。感谢分享,为我节省了很多时间! @Jean-François 说了什么,但不幸的是。我花了很长时间才决定用谷歌搜索错误!哈哈。谢谢! 例如,当您将项目从 .net v.3.5 更改为 4.5 时,也会发生这种情况。并不是说更改本身是错误的,而是框架版本之间可能存在冲突条件,无论是否以管理员身份启动 VS【参考方案3】:

我在将对象(变量)矩阵数组处理到 Excel 工作表时遇到了这个异常。解决方案是,矩阵数组 Index(i,j) 必须从 (0,0) 开始,而 Excel 工作表应该从 (1,1) 的矩阵数组索引 (i,j) 开始。

我希望你这个概念。

【讨论】:

【参考方案4】:

您可能正尝试访问索引为 0 的 Excel,请注意 Excel 行/列从 1 开始。

【讨论】:

这个答案与 Kanna Reddy 上一个答案有何不同? 没有,因为我添加了一条评论,说可能有人犯了类似的错误。

以上是关于如何避免 System.Runtime.InteropServices.COMException?的主要内容,如果未能解决你的问题,请参考以下文章

什么是 CSRF 攻击,如何避免?

如何避免两次编写 SQL Server 查询以避免重复?

如何避免重复的工作?

DDoS攻击是啥?应该如何避免?

Mongodb 连接失败,怎么避免抛错影响正常流程

Java中,当的数据过大时,如何避免值的改变?