Files
FileBow/native/ExplorerSelection.cs
T

115 lines
4.7 KiB
C#

using System.Runtime.InteropServices;
internal static class ExplorerSelection
{
[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr parent, IntPtr after, string className, string? title);
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr window);
static readonly Guid TopLevelBrowser = new("4C96BE40-915C-11CF-99D3-00AA004AE837");
static readonly Guid ShellBrowser = new("000214E2-0000-0000-C000-000000000046");
[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IServiceProvider
{
[PreserveSig]
int QueryService(in Guid service, in Guid iid, [MarshalAs(UnmanagedType.Interface)] out object? result);
}
// IShellBrowser inherits IOleWindow. Only its first vtable method is needed.
[ComImport, Guid("000214E2-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellBrowser
{
[PreserveSig] int GetWindow(out IntPtr window);
}
static IntPtr ActiveTab(IntPtr frame)
{
// Explorer places the active ShellTabWindowClass first in child z-order.
// Skip hidden children during a tab switch; do not use the tab caption or path.
IntPtr tab = IntPtr.Zero;
while ((tab = FindWindowEx(frame, tab, "ShellTabWindowClass", null)) != IntPtr.Zero)
if (IsWindowVisible(tab)) return tab;
return IntPtr.Zero;
}
static IntPtr TabOf(object window)
{
object? browser = null;
try {
if (window is not IServiceProvider provider) return IntPtr.Zero;
if (provider.QueryService(TopLevelBrowser, ShellBrowser, out browser) < 0 || browser is not IShellBrowser shellBrowser)
return IntPtr.Zero;
return shellBrowser.GetWindow(out var tab) >= 0 ? tab : IntPtr.Zero;
} finally { Release(browser); }
}
static void Release(object? value)
{
if (value != null && Marshal.IsComObject(value)) Marshal.ReleaseComObject(value);
}
internal static string[] Read()
{
IntPtr frame = GetForegroundWindow();
if (frame == IntPtr.Zero) return [];
IntPtr activeTab = ActiveTab(frame);
object? shell = null, windows = null, selectedWindow = null;
try {
shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")!);
windows = ((dynamic)shell!).Windows();
int count = ((dynamic)windows).Count;
for (int index = 0; index < count; index++) {
object? candidate = ((dynamic)windows).Item(index);
try {
if (candidate == null || new IntPtr((long)((dynamic)candidate).HWND) != frame) continue;
// All tabs share IWebBrowser2.HWND. Match the tab's own shell
// browser HWND before reading its selection, including empty selections.
if (activeTab != IntPtr.Zero && TabOf(candidate) != activeTab) continue;
// On systems without tab windows, only accept an unambiguous frame.
if (selectedWindow != null) return [];
selectedWindow = candidate;
candidate = null;
} catch (COMException) { }
finally { Release(candidate); }
}
if (selectedWindow == null) return [];
var files = ReadItems(selectedWindow);
// A switch or close during COM calls must never use a stale tab's files.
if (GetForegroundWindow() != frame || ActiveTab(frame) != activeTab) return [];
return files;
} catch { return []; }
finally {
Release(selectedWindow);
Release(windows);
Release(shell);
}
}
static string[] ReadItems(object window)
{
var files = new List<string>();
object? document = null, items = null;
try {
document = ((dynamic)window).Document;
items = ((dynamic)document).SelectedItems();
int count = ((dynamic)items).Count;
for (int index = 0; index < count; index++) {
object? item = ((dynamic)items).Item(index);
try {
string file = ((dynamic)item!).Path;
if (File.Exists(file)) files.Add(file);
} finally { Release(item); }
}
return files.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
} finally {
Release(items);
Release(document);
}
}
}