none
Instantiate Shell32.Shell object in Windows 8 RRS feed

  • Question

  • Hi,

    I want to instantiate the Shell32.Shell object in Windows 8. I have created a small console project in VS 2012, added reference to Microsoft Shell Controls and Automation. When running the following code

    namespace Shell32Test
    {
      class Program
      {
        static void Main(string[] args)
        {
          object shell = new Shell32.Shell();
        }
      }
    }
    

    The invalid cast exception occurs.

    Details of the exception:

    System.InvalidCastException was unhandled
      HResult=-2147467262
      Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
      Source=Shell32Test
      StackTrace:
           at Shell32Test.Program.Main(String[] args) in c:\Users\christ.ADLIB\Documents\Visual Studio 11\Projects\Shell32Test\Shell32Test\Program.cs:line 8
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:

    Any ideas how to solve this issue?

    Thanks,

    C.

    Friday, September 7, 2012 10:16 AM

Answers

  • The solution that worked for me is as following:

    public Shell32.Folder GetShell32NameSpaceFolder(Object folder)
    {
    Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
    
    Object shell = Activator.CreateInstance(shellAppType);
    return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
    System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folder });
    }
    I'm developing in Windows 8 and the program is run in Windows 7 and Windows Server 2008.

    Monday, July 8, 2013 9:50 AM
  • I got same issue in win8 with VS 2012.

    I also try to build the same code in win7 with VS 2012, it work fine under win7..... and, the execute file generated under win7 also work in win8 (this is my temporay solution... ).

    Thursday, September 13, 2012 4:46 AM

All replies

  • Shell32.Shell shell = new Shell32.Shell();

    Friday, September 7, 2012 8:22 PM
  • Hi JohnWein,

    Thanks, but your solution has been tried and is throwing the same exception. Note that the same code works on Windows 7 but fails on Windows 8.

    Regards,

    C.

    Tuesday, September 11, 2012 8:33 AM
  • Try the same code in VB.  To ensure that you're referencing the correct shell32.dll, in Add Reference, browse to System32/shell32.dll or SysWOW32/shell32.dll and double-click on it.
    Tuesday, September 11, 2012 9:10 AM
  • I got same issue in win8 with VS 2012.

    I also try to build the same code in win7 with VS 2012, it work fine under win7..... and, the execute file generated under win7 also work in win8 (this is my temporay solution... ).

    Thursday, September 13, 2012 4:46 AM
  • Thanks twu2,

    That does work. The suggested solution of JohnWein to try to reference System32/shell32.dll or SysWOW64/shell32.dll didn't work. For now we will use this solution until a real fix becomes available.

    Regards,

    C.

    Thursday, September 13, 2012 10:16 AM
  • After some test, I think I got the solution for this.

    1. the code work in Windows Form application.
    2. for console application, it will get above exception.

    so I compare the code for Windows Form and Console application, then add [STAThread] line before Main(), then the console application also work fine. like this:

            [STAThread]
            static void Main(string[] args)

    Tuesday, September 18, 2012 4:09 PM
  • The solution that worked for me is as following:

    public Shell32.Folder GetShell32NameSpaceFolder(Object folder)
    {
    Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
    
    Object shell = Activator.CreateInstance(shellAppType);
    return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
    System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folder });
    }
    I'm developing in Windows 8 and the program is run in Windows 7 and Windows Server 2008.

    Monday, July 8, 2013 9:50 AM
  • very good... thanks
    Thursday, January 2, 2014 9:47 AM
  • It Works!!!!

    THX man.

    Monday, January 19, 2015 10:11 PM
  • Can you explain what this does? I'm getting the same invalid cast exception developing on Windows 10 and trying to use my app on a Windows 7 computer, but every time I cast a shell object an exception throws.
    Wednesday, January 18, 2017 6:41 AM
  • Using " [STAThread]" should also be marked as an answer and is probably the answer.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, January 27, 2018 11:04 PM