无法在 C# 中实例化 COM 组件 - 错误 80070002

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1008040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:23:30  来源:igfitidea点击:

Can't instantiate COM component in C# - error 80070002

c#cominteropcom-interop

提问by Judah Gabriel Himango

I'm attempting to instantiate a Windows Media Player COM object on my machine:

我试图在我的机器上实例化一个 Windows Media Player COM 对象:

Guid mediaPlayerClassId = new Guid("47ac3c2f-7033-4d47-ae81-9c94e566c4cc");
Type mediaPlayerType = Type.GetTypeFromCLSID(mediaPlayerClassId);
Activator.CreateInstance(mediaPlayerType); // <-- this line throws

When executing that last line, I get the following error:

执行最后一行时,出现以下错误:

System.IO.FileNotFoundException was caught
  Message="Retrieving the COM class factory for component with CLSID {47AC3C2F-7033-4D47-AE81-9C94E566C4CC} failed due to the following error: 80070002."
  Source="mscorlib"
  StackTrace:
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
       at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at MyStuff.PreviewFile(String filePath) in F:\Trunk\PreviewHandlerHosting\PreviewHandlerHost.cs:line 60
  InnerException: 

This same code works on other developer machines and end user machines. For some reason, it only fails on my machine. What could be the cause?

相同的代码适用于其他开发人员机器和最终用户机器。出于某种原因,它只在我的机器上失败。可能是什么原因?

采纳答案by Reed Copsey

80070002 is a File Not Found error.

80070002 是文件未找到错误。

My guess is your machine is missing a dependency. Try running the com component through depends.exe to see if you have all of the required libraries installed.

我的猜测是您的机器缺少依赖项。尝试通过depends.exe 运行com 组件以查看是否安装了所有必需的库。

回答by Ana Betts

Well, 0x80070002 means File not found, so I'd check to see whether the DLL pointed to in the COM registration actually exists on your machine

好吧,0x80070002 表示找不到文件,所以我要检查一下 COM 注册中指向的 DLL 是否确实存在于您的计算机上

回答by saknet

If application pool crashes this error can happen. You can check your code if there is any function or object initialization repeating more eventually. This can cause memory leakage and at last application pool crash

如果应用程序池崩溃,则可能发生此错误。如果有任何函数或对象初始化最终重复更多,您可以检查您的代码。这可能会导致内存泄漏并最终导致应用程序池崩溃

回答by Mr AH

Further possibly helpful info. We saw this issue on a classic asp web app which loads .net components. one app was fine, another was not. Same machine? So what gives? We weren't getting proper errors around failing to load a com component, just the error number 0x80070002.

进一步可能有用的信息。我们在加载 .net 组件的经典 asp web 应用程序上看到了这个问题。一个应用程序很好,另一个不是。同一台机器?那么什么给呢?我们没有收到有关无法加载 com 组件的正确错误,只是错误编号 0x80070002。

Eventually this was fixed by just setting the app pool of the broken app to the same as the working app. Something about the app pool meant that the component couldn't be loaded properly, same identity etc (iis 6).

最终,通过将损坏的应用程序的应用程序池设置为与工作应用程序相同的应用程序池来解决此问题。关于应用程序池的一些事情意味着无法正确加载组件,相同的身份等(iis 6)。

There were a bunch of old version numbers in the registry for that component, but regasm always does that, it's terrible at cleaning up, we've even had to write a little tool to delete them all when moving between version numbers on dev machines. But in this instance, those version numbers weren't the issue.

该组件的注册表中有一堆旧版本号,但 regasm 总是这样做,清理起来很糟糕,我们甚至不得不编写一个小工具在开发机器上的版本号之间移动时将它们全部删除。但在这种情况下,这些版本号不是问题。

Conclusion: sounds like a permissions problem, but what do i know..

结论:听起来像是权限问题,但我知道什么..