From b80c264cdf6051c11d4d5bdeb6a65bab54041fc1 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Sat, 5 Nov 2022 17:58:23 +0100 Subject: [PATCH] Fix for Tabbed File Explorer in Windows 11 22H2 --- .../QuickLook.Native32/HelperMethods.cpp | 10 +----- .../QuickLook.Native32/HelperMethods.h | 2 +- .../QuickLook.Native32/Shell32.cpp | 35 ++++++++++++++----- QuickLook/NativeMethods/QuickLook.cs | 26 +++++++------- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp b/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp index 53248a87f..c9c01c0df 100644 --- a/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp +++ b/QuickLook.Native/QuickLook.Native32/HelperMethods.cpp @@ -18,16 +18,8 @@ #include "stdafx.h" #include "HelperMethods.h" -void HelperMethods::GetSelectedInternal(CComQIPtr pwba, PWCHAR buffer) +void HelperMethods::GetSelectedInternal(CComPtr psb, PWCHAR buffer) { - CComQIPtr psp; - if (FAILED(pwba->QueryInterface(IID_IServiceProvider, reinterpret_cast(&psp)))) - return; - - CComPtr psb; - if (FAILED(psp->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, reinterpret_cast(&psb)))) - return; - CComPtr psv; if (FAILED(psb->QueryActiveShellView(&psv))) return; diff --git a/QuickLook.Native/QuickLook.Native32/HelperMethods.h b/QuickLook.Native/QuickLook.Native32/HelperMethods.h index fa248d634..94c713ccd 100644 --- a/QuickLook.Native/QuickLook.Native32/HelperMethods.h +++ b/QuickLook.Native/QuickLook.Native32/HelperMethods.h @@ -19,7 +19,7 @@ class HelperMethods { public: - static void GetSelectedInternal(CComQIPtr pWebBrowserApp, PWCHAR buffer); + static void GetSelectedInternal(CComPtr psb, PWCHAR buffer); static void ObtainFirstItem(CComPtr dao, PWCHAR buffer); static bool IsCursorActivated(HWND hwndfg); static bool IsExplorerSearchBoxFocused(); diff --git a/QuickLook.Native/QuickLook.Native32/Shell32.cpp b/QuickLook.Native/QuickLook.Native32/Shell32.cpp index 092e06f72..7ddf50531 100644 --- a/QuickLook.Native/QuickLook.Native32/Shell32.cpp +++ b/QuickLook.Native/QuickLook.Native32/Shell32.cpp @@ -104,7 +104,8 @@ void Shell32::getSelectedFromExplorer(PWCHAR buffer) if (FAILED(psw.CoCreateInstance(CLSID_ShellWindows))) return; - auto hwndfg = GetForegroundWindow(); + auto hwndfgw = GetForegroundWindow(); + auto hwndfgt = FindWindowEx(hwndfgw, nullptr, L"ShellTabWindowClass", nullptr); auto count = 0L; psw->get_Count(&count); @@ -120,18 +121,26 @@ void Shell32::getSelectedFromExplorer(PWCHAR buffer) if (S_OK != psw->Item(vi, &pdisp)) continue; - CComQIPtr pwba; - if (FAILED(pdisp->QueryInterface(IID_IWebBrowserApp, reinterpret_cast(&pwba)))) + CComPtr psp; + if (FAILED(pdisp->QueryInterface(IID_IServiceProvider, reinterpret_cast(&psp)))) + continue; + + CComPtr psb; + if (FAILED(psp->QueryService(IID_IShellBrowser, IID_IShellBrowser, reinterpret_cast(&psb)))) + continue; + + HWND phwnd; + if (FAILED(psb->GetWindow(&phwnd))) continue; - HWND hwndwba; - if (FAILED(pwba->get_HWND(reinterpret_cast(&hwndwba)))) + if (hwndfgw != phwnd && (hwndfgt != nullptr && hwndfgt != phwnd)) continue; - if (hwndwba != hwndfg || HelperMethods::IsCursorActivated(hwndwba)) + if (HelperMethods::IsCursorActivated(0)) continue; - HelperMethods::GetSelectedInternal(pwba, buffer); + HelperMethods::GetSelectedInternal(psb, buffer); + return; } } @@ -140,7 +149,7 @@ void Shell32::getSelectedFromDesktop(PWCHAR buffer) CoInitialize(nullptr); CComPtr psw; - CComQIPtr pwba; + CComPtr pwba; if (FAILED(psw.CoCreateInstance(CLSID_ShellWindows))) return; @@ -155,5 +164,13 @@ void Shell32::getSelectedFromDesktop(PWCHAR buffer) if (HelperMethods::IsCursorActivated(reinterpret_cast(LongToHandle(phwnd)))) return; - HelperMethods::GetSelectedInternal(pwba, buffer); + CComPtr psp; + if (FAILED(pwba->QueryInterface(IID_IServiceProvider, reinterpret_cast(&psp)))) + return; + + CComPtr psb; + if (FAILED(psp->QueryService(IID_IShellBrowser, IID_IShellBrowser, reinterpret_cast(&psb)))) + return; + + HelperMethods::GetSelectedInternal(psb, buffer); } diff --git a/QuickLook/NativeMethods/QuickLook.cs b/QuickLook/NativeMethods/QuickLook.cs index 43a8954a1..9d52c8977 100644 --- a/QuickLook/NativeMethods/QuickLook.cs +++ b/QuickLook/NativeMethods/QuickLook.cs @@ -21,6 +21,7 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace QuickLook.NativeMethods @@ -83,24 +84,25 @@ internal static FocusedWindowType GetFocusedWindowType() internal static string GetCurrentSelection() { - StringBuilder sb = null; - try + StringBuilder sb = new StringBuilder(MaxPath); + // communicate with COM in a separate STA thread + var thread = new Thread(() => { - // communicate with COM in a separate thread - Task.Run(() => + try { - sb = new StringBuilder(MaxPath); if (App.Is64Bit) GetCurrentSelectionNative_64(sb); else GetCurrentSelectionNative_32(sb); - }).Wait(); - } - catch (Exception e) - { - Debug.WriteLine(e); - } - + } + catch (Exception e) + { + Debug.WriteLine(e); + } + }); + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + thread.Join(); return ResolveShortcut(sb?.ToString() ?? string.Empty); }