From 1d641ba5192dac3b1d7918707ae9ae7dfdf1ae8f Mon Sep 17 00:00:00 2001 From: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Date: Thu, 23 Feb 2023 01:33:15 +0900 Subject: [PATCH] Fix: Catch COMException wherever it occurs in shell extension loading (#11411) * Ensure to catch COMException * Delete unused GetContextMenuFolder --- src/Files.App/Shell/ContextMenu.cs | 53 +++++++++--------------------- 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/src/Files.App/Shell/ContextMenu.cs b/src/Files.App/Shell/ContextMenu.cs index 49b0b4f5ca99..15a06dcbd521 100644 --- a/src/Files.App/Shell/ContextMenu.cs +++ b/src/Files.App/Shell/ContextMenu.cs @@ -137,47 +137,24 @@ public async Task InvokeItem(int itemID) if (!shellItems.Any()) return null; - // HP: The items are all in the same folder - using var sf = shellItems[0].Parent; - - Shell32.IContextMenu menu = sf.GetChildrenUIObjects(default, shellItems); - var hMenu = User32.CreatePopupMenu(); - menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags); - var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName), owningThread); - EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter); - - return contextMenu; - } - - public async static Task GetContextMenuForFolder(string folderPath, Shell32.CMF flags, Func? itemFilter = null) - { - var owningThread = new ThreadWithMessageQueue(); - return await owningThread.PostMethod(() => + try { - ShellFolder? shellFolder = null; - try - { - shellFolder = new ShellFolder(folderPath); - var sv = shellFolder.GetViewObject(default); - Shell32.IContextMenu menu = sv.GetItemObject(Shell32.SVGIO.SVGIO_BACKGROUND); + // HP: The items are all in the same folder + using var sf = shellItems[0].Parent; - var hMenu = User32.CreatePopupMenu(); - menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags); - var contextMenu = new ContextMenu(menu, hMenu, new[] { shellFolder.ParsingName }, owningThread); - EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter); + Shell32.IContextMenu menu = sf.GetChildrenUIObjects(default, shellItems); + var hMenu = User32.CreatePopupMenu(); + menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags); + var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName), owningThread); + EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter); - return contextMenu; - } - catch (Exception ex) when (ex is ArgumentException or FileNotFoundException) - { - // Return empty context menu - return null; - } - finally - { - shellFolder?.Dispose(); - } - }); + return contextMenu; + } + catch (COMException) + { + // Return empty context menu + return null; + } } #endregion FactoryMethods