Skip to content

Commit b80c264

Browse files
committed
Fix for Tabbed File Explorer in Windows 11 22H2
1 parent bd9b32f commit b80c264

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

QuickLook.Native/QuickLook.Native32/HelperMethods.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,8 @@
1818
#include "stdafx.h"
1919
#include "HelperMethods.h"
2020

21-
void HelperMethods::GetSelectedInternal(CComQIPtr<IWebBrowserApp> pwba, PWCHAR buffer)
21+
void HelperMethods::GetSelectedInternal(CComPtr<IShellBrowser> psb, PWCHAR buffer)
2222
{
23-
CComQIPtr<IServiceProvider> psp;
24-
if (FAILED(pwba->QueryInterface(IID_IServiceProvider, reinterpret_cast<void**>(&psp))))
25-
return;
26-
27-
CComPtr<IShellBrowser> psb;
28-
if (FAILED(psp->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, reinterpret_cast<LPVOID*>(&psb))))
29-
return;
30-
3123
CComPtr<IShellView> psv;
3224
if (FAILED(psb->QueryActiveShellView(&psv)))
3325
return;

QuickLook.Native/QuickLook.Native32/HelperMethods.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class HelperMethods
2020
{
2121
public:
22-
static void GetSelectedInternal(CComQIPtr<IWebBrowserApp> pWebBrowserApp, PWCHAR buffer);
22+
static void GetSelectedInternal(CComPtr<IShellBrowser> psb, PWCHAR buffer);
2323
static void ObtainFirstItem(CComPtr<IDataObject> dao, PWCHAR buffer);
2424
static bool IsCursorActivated(HWND hwndfg);
2525
static bool IsExplorerSearchBoxFocused();

QuickLook.Native/QuickLook.Native32/Shell32.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ void Shell32::getSelectedFromExplorer(PWCHAR buffer)
104104
if (FAILED(psw.CoCreateInstance(CLSID_ShellWindows)))
105105
return;
106106

107-
auto hwndfg = GetForegroundWindow();
107+
auto hwndfgw = GetForegroundWindow();
108+
auto hwndfgt = FindWindowEx(hwndfgw, nullptr, L"ShellTabWindowClass", nullptr);
108109

109110
auto count = 0L;
110111
psw->get_Count(&count);
@@ -120,18 +121,26 @@ void Shell32::getSelectedFromExplorer(PWCHAR buffer)
120121
if (S_OK != psw->Item(vi, &pdisp))
121122
continue;
122123

123-
CComQIPtr<IWebBrowserApp> pwba;
124-
if (FAILED(pdisp->QueryInterface(IID_IWebBrowserApp, reinterpret_cast<void**>(&pwba))))
124+
CComPtr<IServiceProvider> psp;
125+
if (FAILED(pdisp->QueryInterface(IID_IServiceProvider, reinterpret_cast<void**>(&psp))))
126+
continue;
127+
128+
CComPtr<IShellBrowser> psb;
129+
if (FAILED(psp->QueryService(IID_IShellBrowser, IID_IShellBrowser, reinterpret_cast<LPVOID*>(&psb))))
130+
continue;
131+
132+
HWND phwnd;
133+
if (FAILED(psb->GetWindow(&phwnd)))
125134
continue;
126135

127-
HWND hwndwba;
128-
if (FAILED(pwba->get_HWND(reinterpret_cast<LONG_PTR*>(&hwndwba))))
136+
if (hwndfgw != phwnd && (hwndfgt != nullptr && hwndfgt != phwnd))
129137
continue;
130138

131-
if (hwndwba != hwndfg || HelperMethods::IsCursorActivated(hwndwba))
139+
if (HelperMethods::IsCursorActivated(0))
132140
continue;
133141

134-
HelperMethods::GetSelectedInternal(pwba, buffer);
142+
HelperMethods::GetSelectedInternal(psb, buffer);
143+
return;
135144
}
136145
}
137146

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

142151
CComPtr<IShellWindows> psw;
143-
CComQIPtr<IWebBrowserApp> pwba;
152+
CComPtr<IWebBrowserApp> pwba;
144153

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

158-
HelperMethods::GetSelectedInternal(pwba, buffer);
167+
CComPtr<IServiceProvider> psp;
168+
if (FAILED(pwba->QueryInterface(IID_IServiceProvider, reinterpret_cast<void**>(&psp))))
169+
return;
170+
171+
CComPtr<IShellBrowser> psb;
172+
if (FAILED(psp->QueryService(IID_IShellBrowser, IID_IShellBrowser, reinterpret_cast<LPVOID*>(&psb))))
173+
return;
174+
175+
HelperMethods::GetSelectedInternal(psb, buffer);
159176
}

QuickLook/NativeMethods/QuickLook.cs

+14-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Runtime.InteropServices;
2222
using System.Runtime.InteropServices.ComTypes;
2323
using System.Text;
24+
using System.Threading;
2425
using System.Threading.Tasks;
2526

2627
namespace QuickLook.NativeMethods
@@ -83,24 +84,25 @@ internal static FocusedWindowType GetFocusedWindowType()
8384

8485
internal static string GetCurrentSelection()
8586
{
86-
StringBuilder sb = null;
87-
try
87+
StringBuilder sb = new StringBuilder(MaxPath);
88+
// communicate with COM in a separate STA thread
89+
var thread = new Thread(() =>
8890
{
89-
// communicate with COM in a separate thread
90-
Task.Run(() =>
91+
try
9192
{
92-
sb = new StringBuilder(MaxPath);
9393
if (App.Is64Bit)
9494
GetCurrentSelectionNative_64(sb);
9595
else
9696
GetCurrentSelectionNative_32(sb);
97-
}).Wait();
98-
}
99-
catch (Exception e)
100-
{
101-
Debug.WriteLine(e);
102-
}
103-
97+
}
98+
catch (Exception e)
99+
{
100+
Debug.WriteLine(e);
101+
}
102+
});
103+
thread.SetApartmentState(ApartmentState.STA);
104+
thread.Start();
105+
thread.Join();
104106
return ResolveShortcut(sb?.ToString() ?? string.Empty);
105107
}
106108

0 commit comments

Comments
 (0)