Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/platform/windows/window_manager_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,14 +1281,34 @@ std::shared_ptr<Window> WindowManager::Get(WindowId id) {
return WindowRegistry::GetInstance().Get(id);
}

// Queries a same-process window's title length without blocking indefinitely
// when the window belongs to another thread that is not pumping messages
// (e.g. a WinRT MediaPlayer SMTC helper window owned by a blocked worker
// thread). Calling GetWindowTextLengthW across threads in the same process
// dispatches WM_GETTEXTLENGTH via synchronous SendMessageW and can deadlock.
static int TryGetWindowTextLength(HWND hwnd) {
if (!hwnd || !IsWindow(hwnd)) {
return 0;
}
if (GetWindowThreadProcessId(hwnd, nullptr) == GetCurrentThreadId()) {
return GetWindowTextLengthW(hwnd);
}
DWORD_PTR length = 0;
if (!SendMessageTimeoutW(hwnd, WM_GETTEXTLENGTH, 0, 0,
SMTO_ABORTIFHUNG | SMTO_NORMAL, 50, &length)) {
return 0;
}
return static_cast<int>(length);
}

// Callback for EnumWindows to collect all top-level windows
static BOOL CALLBACK EnumWindowsCallback(HWND hwnd, LPARAM lParam) {
auto* windows = reinterpret_cast<std::vector<HWND>*>(lParam);

// Only include this process's visible windows that have a title (filters out
// helper/background windows and every other application's windows).
if (IsOwnProcessWindow(hwnd) && IsWindowVisible(hwnd)) {
int length = GetWindowTextLengthW(hwnd);
int length = TryGetWindowTextLength(hwnd);
if (length > 0) {
// Check if it's a normal window (not tool window, etc.)
LONG exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
Expand Down Expand Up @@ -1348,11 +1368,15 @@ BOOL CALLBACK FindCurrentWindowCandidates(HWND hwnd, LPARAM data) {
if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)) {
return TRUE;
}
if (!GetPropW(hwnd, kWindowIdProperty) && GetWindowTextLengthW(hwnd) <= 0) {
auto* candidates = reinterpret_cast<CurrentWindowCandidates*>(data);
const bool visible = IsWindowVisible(hwnd) != FALSE;
if (!visible && (candidates->first_visible || candidates->first_hidden)) {
return TRUE;
}
auto* candidates = reinterpret_cast<CurrentWindowCandidates*>(data);
if (IsWindowVisible(hwnd)) {
if (!GetPropW(hwnd, kWindowIdProperty) && TryGetWindowTextLength(hwnd) <= 0) {
return TRUE;
}
if (visible) {
if (!candidates->first_visible) {
candidates->first_visible = hwnd;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ if(WIN32)
target_link_libraries(window_mouse_policy_windows_test PRIVATE nativeapi)
add_test(NAME window_mouse_policy_windows_test COMMAND window_mouse_policy_windows_test)
set_tests_properties(window_mouse_policy_windows_test PROPERTIES TIMEOUT 10)
add_executable(window_manager_get_current_windows_test window_manager_get_current_windows_test.cpp)
target_link_libraries(window_manager_get_current_windows_test PRIVATE nativeapi)
add_test(NAME window_manager_get_current_windows_test COMMAND window_manager_get_current_windows_test)
set_tests_properties(window_manager_get_current_windows_test PROPERTIES TIMEOUT 10)
add_executable(window_system_menu_windows_test window_system_menu_windows_test.cpp)
target_link_libraries(window_system_menu_windows_test PRIVATE nativeapi)
# Explicit desktop regression; captures only a pixel inside its own windows.
Expand Down
94 changes: 94 additions & 0 deletions tests/window_manager_get_current_windows_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Regression test for libnativeapi/nativeapi#76:
// WindowManager::GetCurrent() and WindowManager::GetAll() must not deadlock
// when another thread in the same process owns a top-level HWND (such as
// WinRT's MediaPlayer SMTC helper window) and is blocked without pumping
// Win32 messages.
#include <windows.h>

#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>

#include "nativeapi.h"

namespace {
int g_failures = 0;

void Check(bool ok, const char* label) {
std::cout << (ok ? "PASS " : "FAIL ") << label << std::endl;
g_failures += !ok;
}
} // namespace

int main() {
HINSTANCE hinst = GetModuleHandleW(nullptr);

WNDCLASSW cls = {};
cls.lpfnWndProc = DefWindowProcW;
cls.hInstance = hinst;
cls.lpszClassName = L"NativeApiGetCurrentCrossThreadTestWindow";
RegisterClassW(&cls);

HANDLE worker_ready = CreateEventW(nullptr, TRUE, FALSE, nullptr);
HANDLE worker_release = CreateEventW(nullptr, TRUE, FALSE, nullptr);
std::atomic<HWND> blocked_hwnd{nullptr};

// Worker thread creates a hidden top-level HWND (with no WS_EX_TOOLWINDOW,
// matching WinRT's MediaPlayer SMTC window) and then blocks without pumping
// messages. A synchronous cross-thread GetWindowTextLengthW would deadlock.
std::thread worker([&] {
HWND hwnd = CreateWindowExW(
0, cls.lpszClassName, L"Blocked Worker Window", WS_OVERLAPPEDWINDOW,
0, 0, 200, 100, nullptr, nullptr, hinst, nullptr);
blocked_hwnd.store(hwnd);
SetEvent(worker_ready);
WaitForSingleObject(worker_release, INFINITE);
if (hwnd) {
DestroyWindow(hwnd);
}
});

WaitForSingleObject(worker_ready, 5000);
Check(blocked_hwnd.load() != nullptr, "created blocked worker-thread HWND");

// Create the app's main window on the calling thread, shown without
// activation so GetActiveWindow() is null and GetCurrent() must take the
// EnumWindows fallback path.
HWND main_hwnd = CreateWindowExW(
0, cls.lpszClassName, L"Main App Window", WS_OVERLAPPEDWINDOW,
100, 100, 400, 300, nullptr, nullptr, hinst, nullptr);
Check(main_hwnd != nullptr, "created main-thread app HWND");
ShowWindow(main_hwnd, SW_SHOWNOACTIVATE);

const auto start = std::chrono::steady_clock::now();
auto current = nativeapi::WindowManager::GetInstance().GetCurrent();
auto all = nativeapi::WindowManager::GetInstance().GetAll();
const auto elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();

Check(elapsed_ms < 2000,
"GetCurrent() and GetAll() return promptly despite blocked worker HWND");
Check(current != nullptr && current->GetNativeObject() == main_hwnd,
"GetCurrent() resolves the main app window");

bool found_main = false;
for (const auto& w : all) {
if (w && w->GetNativeObject() == main_hwnd) {
found_main = true;
break;
}
}
Check(found_main, "GetAll() includes the visible main app window");

SetEvent(worker_release);
worker.join();
CloseHandle(worker_ready);
CloseHandle(worker_release);
if (main_hwnd) {
DestroyWindow(main_hwnd);
}
return g_failures ? 1 : 0;
}
Loading