wpf HRESULT 异常:0x80040111 (CLASS_E_CLASSNOTAVAILABLE)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29929862/
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-09-13 13:06:37  来源:igfitidea点击:

Exception from HRESULT: 0x80040111 (CLASS_E_CLASSNOTAVAILABLE)

c#.netwpfdllcom

提问by Saagar Elias Hymany

Using .Net 4.0 / WPF Application / C#

使用 .Net 4.0 / WPF 应用程序 / C#

I have the following piece of code in my application, which opens a FileDialog when the Selectbutton is clicked.

我的应用程序中有以下代码段,Select单击按钮时会打开一个 FileDialog 。

OpenFileDialog fdgSelectFile;
bool? dialogResult;

try
{
    fdgSelectFile = new OpenFileDialog {DefaultExt = FileDialogDefaultExt, Filter = FileDialogFilter};
    dialogResult = fdgSelectFile.ShowDialog();
    if (dialogResult.HasValue && dialogResult.Value)
    {
        SelectedFilePath = fdgSelectFile.FileName;
        // do your stuff
    }
}

This piece of code works in other machines, but not in my machine. It just throws an exception - as below - when the Selectbutton is clicked upon.

这段代码在其他机器上有效,但在我的机器上无效。当Select单击按钮时,它只会引发异常 - 如下所示。

2015-04-28 14:33:47,453 [1] ERROR XXXX.XXXX.XXXX.ViewModels.UploadViewModel - SelectFile - System.Runtime.InteropServices.COMException (0x80040111): Creating an instance of the COM component with CLSID {DC1C5A9C-E88A-4DDE-A5A1-60F82A20AEF7} from the IClassFactory failed due to the following error: 80040111 ClassFactory cannot supply requested class (Exception from HRESULT: 0x80040111 (CLASS_E_CLASSNOTAVAILABLE)).
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.Win32.OpenFileDialog.CreateVistaDialog()
   at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
   at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
   at Microsoft.Win32.CommonDialog.ShowDialog()
   at XXXX.XXXX.XXXX.ViewModels.UploadViewModel.SelectFile(Object param) in c:\XXXX\XXXX\Client\XXXX.XXXX.XXXX\ViewModels\UploadViewModel .cs:line 176


Finding out the error is caused by comdlg32.dllfrom Microsoft.Win32namespace, inside PresentationFramework.dllassembly, I queried the Registry for this CLS ID

找出错误是造成comdlg32.dllMicrosoft.Win32命名空间内PresentationFramework.dll组装,我查询注册表此CLS ID

reg query HKCR\CLSID | find /i "{DC1C5A9C-E88A-4DDE-A5A1-60F82A20AEF7}"

and here is what it says

这就是它所说的

HKEY_CLASSES_ROOT\CLSID{DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7}

HKEY_CLASSES_ROOT\CLSID{DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7}



I have also tried the following

我也试过以下

  1. As per thisSO Post, I tried to register the dll, but it came back saying

    [Window Title] RegSvr32

    [Content] The module "comdlg32.dll" was loaded but the entry-point DllRegisterServer was not found.

    Make sure that "comdlg32.dll" is a valid DLL or OCX file and then try again.

    [OK]

  2. As per thisSO Post, I tried changing the Permissions, but no luck

  1. 根据这个SO Post,我试图注册dll,但它回来说

    [窗口标题] RegSvr32

    [内容] 模块“comdlg32.dll”已加载,但未找到入口点 DllRegisterServer。

    确保“comdlg32.dll”是有效的 DLL 或 OCX 文件,然后重试。

    [好的]

  2. 根据这个SO Post,我尝试更改Permissions,但没有运气



Is there any way this can be resolved apart from re-imaging the machine or re-installing Windows?

除了重新映像机器或重新安装 Windows 之外,有什么方法可以解决这个问题吗?

If this helps : I have .Net FrameWork v3.5/ v4.0 / v4.5.1 & v4.5.2 installed in my machine and the PresentationFramework.dllis available in all locations inside the folders

如果这有帮助:我的机器上安装了 .Net FrameWork v3.5/v4.0/v4.5.1 & v4.5.2,并且PresentationFramework.dll可以在文件夹内的所有位置使用

v3.5   : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client
v4.0   : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
v4.5   : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5
v4.5.1 : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1
v4.5.2 : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2

回答by Mike Dinescu

Discarding the possibility that the Windows installation is broken, this is actually a rather insidious problem that is caused by "Visual Themes" being turned off in Windows 7 and above.

排除 Windows 安装损坏的可能性,这实际上是一个相当隐蔽的问题,由 Windows 7 及更高版本中关闭“Visual Themes”引起。

To reproduce it you can take a working WPF application and modify it's compatibility settings (righ-click on the .exe in Windows Explorer, then select Preferences and from the Compatibiltiy tab, check "Disable Visual Themes"). Then, try running the application and you'll notice it starts crashing when you try to show an OpenFileDialog or a SaveFileDialog.

要重现它,您可以使用一个有效的 WPF 应用程序并修改它的兼容性设置(在 Windows 资源管理器中右键单击 .exe,然后选择首选项并从兼容性选项卡中,选中“禁用视觉主题”)。然后,尝试运行该应用程序,当您尝试显示 OpenFileDialog 或 SaveFileDialog 时,您会注意到它开始崩溃。

Visual Themes may be turned off at the OS level as well (when using a high contrast theme for instance) and they are usually turned off in Terminal Services sessions, or when desktop sharing via WebEx or some other desktop sharing applications.

视觉主题也可以在操作系统级别关闭(例如,当使用高对比度主题时),它们通常在终端服务会话中关闭,或者在通过 WebEx 或其他一些桌面共享应用程序共享桌面时关闭。

Unfortunately I don't have a solution yet but based on reading through MSDN it looks like Microsoft is saying you should "provide an alternative code path" when desktop composition and visual themes are off - whatever that means.

不幸的是,我还没有解决方案,但根据阅读 MSDN,微软似乎在说,当桌面组合和视觉主题关闭时,您应该“提供替代代码路径”——不管这意味着什么。

Internally, the OpenFileDialog's implementation has a method that attempts to initialize an instance of the open file dialog COM control which fails when Visual Themes are off

在内部,OpenFileDialog 的实现有一个方法,该方法尝试初始化打开文件对话框 COM 控件的实例,该方法在 Visual Themes 关闭时失败

[SecurityCritical, SecurityTreatAsSafe]
internal override IFileDialog CreateVistaDialog()
{
    new SecurityPermission(PermissionState.Unrestricted).Assert();
    return (IFileDialog) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")));
}

回答by Cla3rk

A note on the answer to use System.Windows.Forms.SaveFileDialog; be sure to set the property AutoUpgradeEnabled = False.

关于使用答案的注释System.Windows.Forms.SaveFileDialog;一定要设置属性AutoUpgradeEnabled = False

There is a high-contrast mode in Windows 10, if the user has it on and you don't set this option, you will get this error.

Windows 10 有高对比度模式,如果用户打开了而你没有设置这个选项,你会得到这个错误。

Caveat: The control will look like something from Windows XP, but at least it will still work.

警告:控件看起来像 Windows XP 中的东西,但至少它仍然可以工作。

回答by user2239903

I had the same issue when using SaveFileDialog.

我在使用时遇到了同样的问题SaveFileDialog

Make sure to use System.Windows.Forms.SaveFileDialog, and notMicrosoft.Win32.SaveFileDialog

确保使用System.Windows.Forms.SaveFileDialog, 而不是Microsoft.Win32.SaveFileDialog