8

I am using the following code to reference a shell dll

            Type t = Type.GetTypeFromProgID("Shell.Application");

            Shell s = (Shell)Activator.CreateInstance(t);


            Console.WriteLine("success");
            Console.ReadLine();

It works fine on my windows 7 development machine.But when I try running the exe on Win 2003 server I get this exception

Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell3
2.Shell'. This operation failed because the QueryInterface call on the COM compo
nent for the interface with IID '{866738B9-6CF2-4DE8-8767-F794EBE74F4E}' failed
due to the following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).

I took some help from C#: Referencing a windows shell interface but no luck.

I am referencing shell using Microsoft Shell Controls and Automation reference which is Interop.Shell32 dll

If someone can guide it will really helpful.

2
  • 1
    It's old, I don't think the guids have changed but who knows. Run regedit.exe on that machine and navigate to HKCR\Shell.Application. Verify that the CLSID key value is {13709620-C279-11CE-A49E-444553540000} Commented Aug 22, 2012 at 16:16
  • There is a simpler solution, see stackoverflow.com/a/24967301/625349 Commented Mar 5, 2017 at 14:32

4 Answers 4

20

Ok,this is how I got through the problem incase it helps someone

This is how my new code looks like

Type t = Type.GetTypeFromProgID("Shell.Application");

dynamic shell = Activator.CreateInstance(t);

//This is browse through all the items in the folder
var objFolder = shell.NameSpace(@"\\fileshares\Files\test");

foreach (var item in objFolder.Items())
{
    //This is to get the file's comments for each files in the folderitem

    string file_version = objFolder.GetDetailsOf(item, 14).ToString();

     Console.WriteLine(file_version);

}

This script is by combining help from http://nerdynotes.blogspot.com/2008/06/vbnet-shell32-code-compiled-on-vista.html

and

http://foro.h-sec.org/net/problemas-en-net/

The second link is in spanish,I used google translate to make it up in English

Thanks to all who replied to this question

Sign up to request clarification or add additional context in comments.

1 Comment

FYI there is another similar solution for this same problem here that might be helpful: stackoverflow.com/questions/26646068/…
1

Have a look at this: http://nerdynotes.blogspot.com/2008/06/vbnet-shell32-code-compiled-on-vista.html I think it's the same issue.

1 Comment

I had tried that option before posting the question in stackoverflow.I can create an object using object shell = Activator.CreateInstance(t); But then I am not sure how can I use the namespace property.I tried to use it as suggested in one og the comments of the link you posted but had no luck with that as well.
1

Instead of

Type t = Type.GetTypeFromProgID("Shell.Application");

dynamic shell = Activator.CreateInstance(t);

I used

var shell = (IShellDispatch4) new Shell();

shell.Namespace then works as expected.

Turns out that reference for a shell object defaults to IShellDispatch5, which can't be used in XP or 2003.

1 Comment

I could not get the option IShellDispatch4 in my C# code (4.0). Do I need to add any reference?
0

You can reference directly the COM component %SystemRoot%\system32\shell32.dll (ProgID Shell.Application):

  • Right click project file --> Add --> Com Reference --> Browse --> Select %SystemRoot%\system32\shell32.dll. You should be seeing in Dependencies/COM node in Visual Studio Interop.Shell32. Click on it and change "Embed Interop Types" from Yes to No (if you use Net Core).

    Interop.Shell32

  • You can then use this simple code

var shell = new ShellClass();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.