Can the Shell32.dll be used in the IIS environment?

Yitong Phou 21 Reputation points
2021-10-20T20:23:08.02+00:00

I have a console exe program that used shell32.dll (compiled in dotnetCore 5). It works. However, when I used it in IIS environment (called it from CONTROLLERS) and it doesn't work.

I wonder that is a restriction especially for the shell32.dll?
If there is a restriction, what would be the other DLLs that have features like shell32.dll I can use?

Here is the message I got.
System.InvalidCastException
HResult=0x80004002
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 (0x80004002 (E_NOINTERFACE)).
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.CompilerServices.CastHelpers.ChkCastAny(Void* toTypeHnd, Object obj)

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,077 questions
{count} votes

Accepted answer
  1. Michael Taylor 45,986 Reputation points
    2021-10-20T20:46:30.7+00:00

    Yes it can work but the issue isn't with shell32 which is where the bulk of the Shell APIs reside. For example ShellExecute resides there and that is used when you call Process.Start with UseShellExecute set. Problems occur when you try to use APIs that are designed for desktop users in a web app that doesn't have such a thing. Where the actual problem is depends upon exactly what you're trying to call and since you didn't tell us that it is hard to say.

    Out of the box your app pool is probably not loading the user profile of the user account under which it runs. Hence there is no user profile loaded and none of the APIs relying on that works. Setting the app pool to load the user profile has a negative impact on performance but can solve issues in some cases.

    The web app itself is running in a non-interactive environment and therefore most APIs related to the UI will also not work. This appears to be what you might be doing here. You're trying to get the Shell object which may or may not be available. Posting the code you wrote that you are trying to get working would be helpful for us.

    You mentioned a console app so I suspect you might be trying to start the process from a web app and it is then failing. That could just be related to you not passing the right startup options (or you might need to ensure the user profile for the app pool is loaded).


2 additional answers

Sort by: Most helpful
  1. Yitong Phou 21 Reputation points
    2021-10-23T20:28:01.237+00:00
    using System;
    using System.Collections.Generic;
    using Shell32;
    using System.Threading;
    
    namespace ShellTestAPI.Helpers
    {
        class MediaMetaData
        {
            //[STAThread]
            public static List<FileMetaData> SingleDir(string folder)
            {
                List<FileMetaData> results = null;
    
                var thread = new System.Threading.Thread(() => {
                    results = GetFilesSingleDir(folder);
                });
    
    #pragma warning disable CA1416 // Validate platform compatibility
                thread.SetApartmentState(ApartmentState.STA);
    #pragma warning restore CA1416 // Validate platform compatibility
                thread.Start();
                thread.Join();
    
                return results;
            }
    
    0 comments No comments

  2. Yitong Phou 21 Reputation points
    2022-05-19T22:13:08.4+00:00

    Please view https://github.com/d052057/ShellTestAPI for the answer. It works with VS2019 - dotnet core 5.

    0 comments No comments