Skip to content

Commit 1d641ba

Browse files
authored
Fix: Catch COMException wherever it occurs in shell extension loading (#11411)
* Ensure to catch COMException * Delete unused GetContextMenuFolder
1 parent 8854bb6 commit 1d641ba

File tree

1 file changed

+15
-38
lines changed

1 file changed

+15
-38
lines changed

src/Files.App/Shell/ContextMenu.cs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -137,47 +137,24 @@ public async Task InvokeItem(int itemID)
137137
if (!shellItems.Any())
138138
return null;
139139

140-
// HP: The items are all in the same folder
141-
using var sf = shellItems[0].Parent;
142-
143-
Shell32.IContextMenu menu = sf.GetChildrenUIObjects<Shell32.IContextMenu>(default, shellItems);
144-
var hMenu = User32.CreatePopupMenu();
145-
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags);
146-
var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName), owningThread);
147-
EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter);
148-
149-
return contextMenu;
150-
}
151-
152-
public async static Task<ContextMenu?> GetContextMenuForFolder(string folderPath, Shell32.CMF flags, Func<string, bool>? itemFilter = null)
153-
{
154-
var owningThread = new ThreadWithMessageQueue();
155-
return await owningThread.PostMethod<ContextMenu>(() =>
140+
try
156141
{
157-
ShellFolder? shellFolder = null;
158-
try
159-
{
160-
shellFolder = new ShellFolder(folderPath);
161-
var sv = shellFolder.GetViewObject<Shell32.IShellView>(default);
162-
Shell32.IContextMenu menu = sv.GetItemObject<Shell32.IContextMenu>(Shell32.SVGIO.SVGIO_BACKGROUND);
142+
// HP: The items are all in the same folder
143+
using var sf = shellItems[0].Parent;
163144

164-
var hMenu = User32.CreatePopupMenu();
165-
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags);
166-
var contextMenu = new ContextMenu(menu, hMenu, new[] { shellFolder.ParsingName }, owningThread);
167-
EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter);
145+
Shell32.IContextMenu menu = sf.GetChildrenUIObjects<Shell32.IContextMenu>(default, shellItems);
146+
var hMenu = User32.CreatePopupMenu();
147+
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags);
148+
var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName), owningThread);
149+
EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter);
168150

169-
return contextMenu;
170-
}
171-
catch (Exception ex) when (ex is ArgumentException or FileNotFoundException)
172-
{
173-
// Return empty context menu
174-
return null;
175-
}
176-
finally
177-
{
178-
shellFolder?.Dispose();
179-
}
180-
});
151+
return contextMenu;
152+
}
153+
catch (COMException)
154+
{
155+
// Return empty context menu
156+
return null;
157+
}
181158
}
182159

183160
#endregion FactoryMethods

0 commit comments

Comments
 (0)