Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception from HRESULT: 0x80040111 (CLASS_E_CLASSNOTAVAILABLE)

Tags:

c#

.net

wpf

dll

com

Using .Net 4.0 / WPF Application / C#

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

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 Select button is clicked upon.

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.dll from Microsoft.Win32 namespace, inside PresentationFramework.dll assembly, I queried the Registry for this 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}


I have also tried the following

  1. As per this SO 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 this SO Post, I tried changing the Permissions, but no luck


Is there any way this can be resolved apart from re-imaging the machine or re-installing 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.dll is available in all locations inside the folders

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
like image 755
Saagar Elias Jacky Avatar asked Mar 16 '23 06:03

Saagar Elias Jacky


1 Answers

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.

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.

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.

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.

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

[SecurityCritical, SecurityTreatAsSafe]
internal override IFileDialog CreateVistaDialog()
{
    new SecurityPermission(PermissionState.Unrestricted).Assert();
    return (IFileDialog) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")));
}
like image 170
Mike Dinescu Avatar answered Mar 18 '23 19:03

Mike Dinescu