Skip to content

Commit

Permalink
Fix: Catch COMException wherever it occurs in shell extension loading (
Browse files Browse the repository at this point in the history
…#11411)

* Ensure to catch COMException

* Delete unused GetContextMenuFolder
  • Loading branch information
hishitetsu authored Feb 22, 2023
1 parent 8854bb6 commit 1d641ba
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions src/Files.App/Shell/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Shell32.IContextMenu>(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<ContextMenu?> GetContextMenuForFolder(string folderPath, Shell32.CMF flags, Func<string, bool>? itemFilter = null)
{
var owningThread = new ThreadWithMessageQueue();
return await owningThread.PostMethod<ContextMenu>(() =>
try
{
ShellFolder? shellFolder = null;
try
{
shellFolder = new ShellFolder(folderPath);
var sv = shellFolder.GetViewObject<Shell32.IShellView>(default);
Shell32.IContextMenu menu = sv.GetItemObject<Shell32.IContextMenu>(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<Shell32.IContextMenu>(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
Expand Down

0 comments on commit 1d641ba

Please sign in to comment.