From 356db99bcaf71eec225c0279ba7475d7e79c7708 Mon Sep 17 00:00:00 2001 From: gokcekantarci <115616017+gokcekantarci@users.noreply.github.com> Date: Wed, 7 Feb 2024 17:52:11 +0300 Subject: [PATCH] [Peek]Fix crashes on File Explorer alternatives (#31211) * [Peek] Add check to ensure window class name matches File Explorer * [Peek] Safe casting for IShellFolderViewDual2 compatibility. --- .../Peek.UI/Helpers/FileExplorerHelper.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs b/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs index b2bb1234cd..2c3931c218 100644 --- a/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs +++ b/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs @@ -63,21 +63,23 @@ namespace Peek.UI.Helpers var shell = new Shell32.Shell(); ShellWindows shellWindows = shell.Windows(); - foreach (IWebBrowserApp webBrowserApp in shell.Windows()) + foreach (IWebBrowserApp webBrowserApp in shellWindows) { - var shellFolderView = (Shell32.IShellFolderViewDual2)webBrowserApp.Document; - var folderTitle = shellFolderView.Folder.Title; - - if (webBrowserApp.HWND == foregroundWindowHandle) + if (webBrowserApp.Document is Shell32.IShellFolderViewDual2 shellFolderView) { - var serviceProvider = (IServiceProvider)webBrowserApp; - var shellBrowser = (IShellBrowser)serviceProvider.QueryService(PInvoke.SID_STopLevelBrowser, typeof(IShellBrowser).GUID); - shellBrowser.GetWindow(out IntPtr shellBrowserHandle); + var folderTitle = shellFolderView.Folder.Title; - if (activeTab == shellBrowserHandle) + if (webBrowserApp.HWND == foregroundWindowHandle) { - shellItemArray = GetShellItemArray(shellBrowser, onlySelectedFiles); - return shellItemArray; + var serviceProvider = (IServiceProvider)webBrowserApp; + var shellBrowser = (IShellBrowser)serviceProvider.QueryService(PInvoke.SID_STopLevelBrowser, typeof(IShellBrowser).GUID); + shellBrowser.GetWindow(out IntPtr shellBrowserHandle); + + if (activeTab == shellBrowserHandle) + { + shellItemArray = GetShellItemArray(shellBrowser, onlySelectedFiles); + return shellItemArray; + } } } }