Skip to content

Commit

Permalink
Fix for Tabbed File Explorer in Windows 11 22H2
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed Nov 5, 2022
1 parent bd9b32f commit b80c264
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
10 changes: 1 addition & 9 deletions QuickLook.Native/QuickLook.Native32/HelperMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@
#include "stdafx.h"
#include "HelperMethods.h"

void HelperMethods::GetSelectedInternal(CComQIPtr<IWebBrowserApp> pwba, PWCHAR buffer)
void HelperMethods::GetSelectedInternal(CComPtr<IShellBrowser> psb, PWCHAR buffer)
{
CComQIPtr<IServiceProvider> psp;
if (FAILED(pwba->QueryInterface(IID_IServiceProvider, reinterpret_cast<void**>(&psp))))
return;

CComPtr<IShellBrowser> psb;
if (FAILED(psp->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, reinterpret_cast<LPVOID*>(&psb))))
return;

CComPtr<IShellView> psv;
if (FAILED(psb->QueryActiveShellView(&psv)))
return;
Expand Down
2 changes: 1 addition & 1 deletion QuickLook.Native/QuickLook.Native32/HelperMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class HelperMethods
{
public:
static void GetSelectedInternal(CComQIPtr<IWebBrowserApp> pWebBrowserApp, PWCHAR buffer);
static void GetSelectedInternal(CComPtr<IShellBrowser> psb, PWCHAR buffer);
static void ObtainFirstItem(CComPtr<IDataObject> dao, PWCHAR buffer);
static bool IsCursorActivated(HWND hwndfg);
static bool IsExplorerSearchBoxFocused();
Expand Down
35 changes: 26 additions & 9 deletions QuickLook.Native/QuickLook.Native32/Shell32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -120,18 +121,26 @@ void Shell32::getSelectedFromExplorer(PWCHAR buffer)
if (S_OK != psw->Item(vi, &pdisp))
continue;

CComQIPtr<IWebBrowserApp> pwba;
if (FAILED(pdisp->QueryInterface(IID_IWebBrowserApp, reinterpret_cast<void**>(&pwba))))
CComPtr<IServiceProvider> psp;
if (FAILED(pdisp->QueryInterface(IID_IServiceProvider, reinterpret_cast<void**>(&psp))))
continue;

CComPtr<IShellBrowser> psb;
if (FAILED(psp->QueryService(IID_IShellBrowser, IID_IShellBrowser, reinterpret_cast<LPVOID*>(&psb))))
continue;

HWND phwnd;
if (FAILED(psb->GetWindow(&phwnd)))
continue;

HWND hwndwba;
if (FAILED(pwba->get_HWND(reinterpret_cast<LONG_PTR*>(&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;
}
}

Expand All @@ -140,7 +149,7 @@ void Shell32::getSelectedFromDesktop(PWCHAR buffer)
CoInitialize(nullptr);

CComPtr<IShellWindows> psw;
CComQIPtr<IWebBrowserApp> pwba;
CComPtr<IWebBrowserApp> pwba;

if (FAILED(psw.CoCreateInstance(CLSID_ShellWindows)))
return;
Expand All @@ -155,5 +164,13 @@ void Shell32::getSelectedFromDesktop(PWCHAR buffer)
if (HelperMethods::IsCursorActivated(reinterpret_cast<HWND>(LongToHandle(phwnd))))
return;

HelperMethods::GetSelectedInternal(pwba, buffer);
CComPtr<IServiceProvider> psp;
if (FAILED(pwba->QueryInterface(IID_IServiceProvider, reinterpret_cast<void**>(&psp))))
return;

CComPtr<IShellBrowser> psb;
if (FAILED(psp->QueryService(IID_IShellBrowser, IID_IShellBrowser, reinterpret_cast<LPVOID*>(&psb))))
return;

HelperMethods::GetSelectedInternal(psb, buffer);
}
26 changes: 14 additions & 12 deletions QuickLook/NativeMethods/QuickLook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit b80c264

Please sign in to comment.