Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Catch COMException wherever it occurs in shell extension loading #11411

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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