[EXPLORER] Check registry key for default shell (#7502)
authorBrady McDermott <freedom7341@hotmail.com>
Sat, 30 Nov 2024 17:17:54 +0000 (10:17 -0700)
committerGitHub <noreply@github.com>
Sat, 30 Nov 2024 17:17:54 +0000 (10:17 -0700)
Improves detection of Explorer being the default shell.

- Check the "Software\Microsoft\Windows NT\CurrentVersion\Winlogon" key to see if Explorer is the default shell.
- Use this check to determine whether to start the desktop and taskbar or only open a file browser window.

JIRA issue CORE-19887

base/shell/explorer/explorer.cpp

index a88cf05..d51a329 100644 (file)
@@ -87,6 +87,47 @@ HideMinimizedWindows(IN BOOL bHide)
 }
 #endif
 
+static BOOL
+IsExplorerSystemShell()
+{
+    BOOL bIsSystemShell = TRUE; // Assume we are the system shell by default.
+    WCHAR szPath[MAX_PATH];
+
+    if (!GetModuleFileNameW(NULL, szPath, _countof(szPath)))
+        return FALSE;
+
+    LPWSTR szExplorer = PathFindFileNameW(szPath);
+
+    HKEY hKeyWinlogon;
+    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
+                      0, KEY_READ, &hKeyWinlogon) != ERROR_SUCCESS)
+    {
+        // No registry access.
+        bIsSystemShell = TRUE;
+    }
+    else
+    {
+        LSTATUS Status;
+        DWORD dwType;
+        WCHAR szShell[MAX_PATH];
+        DWORD cbShell = sizeof(szShell);
+
+        // TODO: Add support for paths longer than MAX_PATH
+        Status = RegQueryValueExW(hKeyWinlogon, L"Shell", 0, &dwType, (LPBYTE)szShell, &cbShell);
+        if (Status == ERROR_SUCCESS)
+        {
+            if ((dwType == REG_SZ || dwType == REG_EXPAND_SZ) && StrStrI(szShell, szExplorer))
+                bIsSystemShell = TRUE;
+            else
+                bIsSystemShell = FALSE;
+        }
+
+        RegCloseKey(hKeyWinlogon);
+    }
+
+    return bIsSystemShell;
+}
+
 #if !WIN7_COMPAT_MODE
 static INT
 StartWithCommandLine(IN HINSTANCE hInstance)
@@ -212,15 +253,14 @@ _tWinMain(IN HINSTANCE hInstance,
     TRACE("Explorer starting... Command line: %S\n", lpCmdLine);
 
 #if !WIN7_COMPAT_MODE
-    if (GetShellWindow() == NULL)
-        bExplorerIsShell = TRUE;
+    bExplorerIsShell = (GetShellWindow() == NULL) && IsExplorerSystemShell();
 
     if (!bExplorerIsShell)
     {
         return StartWithCommandLine(hInstance);
     }
 #else
-    bExplorerIsShell = TRUE;
+    bExplorerIsShell = IsExplorerSystemShell();
 #endif
 
     return StartWithDesktop(hInstance);