代码之家  ›  专栏  ›  技术社区  ›  anon

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

  •  29
  • anon  · 技术社区  · 14 年前

    在我的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()
    
    3 回复  |  直到 7 年前
        1
  •  40
  •   ChrisF toni    14 年前

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

    当抛出异常时,您可以尝试中断。去 调试>异常。。。 使用 查找。。。 定位选项 System.Runtime.InteropServices.COMException . 勾选在抛出时中断的选项,然后调试应用程序。

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

        2
  •  24
  •   Anubhav Trivedi    11 年前

    我偶然发现 System.Runtime.InteropServices.COMException 打开项目解决方案时。有时用户没有足够的特权来运行 COM 方法。我以管理员身份运行Visual Studio,但异常已消失。

        3
  •  0
  •   Kanna Reddy    6 年前

    我在将对象(变量)矩阵数组复制到Excel工作表时遇到此异常。解决方法是,矩阵数组索引(i,j)必须从(0,0)开始,而Excel工作表应该从(1,1)开始,矩阵数组索引(i,j)开始。

    我希望你有这个概念。

        4
  •  -1
  •   Rajesh    5 年前

    可能您正在尝试使用索引0访问excel,请注意,excel行/列从1开始。