// SPDX-License-Identifier: LGPL-2.1-or-later #include #include #include "../edit_hook.h" static HWND worker_window; static HANDLE ready; static void pump(DWORD milliseconds) { DWORD end = GetTickCount() + milliseconds; do { MSG message; while (PeekMessageW(&message, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&message); DispatchMessageW(&message); } Sleep(10); } while ((LONG)(end - GetTickCount()) > 0); } static BOOL attached_on_focus(HWND parent) { HWND edit = CreateWindowA("Edit", "ANSI background probe", WS_CHILD, 0, 0, 100, 30, parent, NULL, GetModuleHandleW(NULL), NULL); if (!edit) ExitProcess(2); // A background application can send focus messages of its own. It must // not acquire new accessibility subclasses through a desktop-wide hook. SendMessageW(edit, WM_SETFOCUS, 0, 0); BOOL attached = GetPropW(edit, EDIT_STATE_PROPERTY) != NULL; DestroyWindow(edit); return attached; } static LRESULT CALLBACK window_proc(HWND window, UINT message, WPARAM wp, LPARAM lp) { if (message == WM_APP) return attached_on_focus(window); if (message == WM_CLOSE) { DestroyWindow(window); PostQuitMessage(0); return 0; } return DefWindowProcW(window, message, wp, lp); } static DWORD WINAPI worker(void *unused) { (void)unused; worker_window = CreateWindowW(L"WineAccessHookScope", L"Other UI thread", WS_OVERLAPPEDWINDOW, 300, 0, 200, 120, NULL, NULL, GetModuleHandleW(NULL), NULL); SetEvent(ready); MSG message; while (GetMessageW(&message, NULL, 0, 0) > 0) DispatchMessageW(&message); return 0; } int main(void) { WNDCLASSW cls = {0}; cls.lpfnWndProc = window_proc; cls.hInstance = GetModuleHandleW(NULL); cls.lpszClassName = L"WineAccessHookScope"; if (!RegisterClassW(&cls)) return 2; HWND window = CreateWindowW(cls.lpszClassName, L"Hook scope fixture", WS_OVERLAPPEDWINDOW, 0, 0, 200, 120, NULL, NULL, cls.hInstance, NULL); ready = CreateEventW(NULL, TRUE, FALSE, NULL); HANDLE thread = CreateThread(NULL, 0, worker, NULL, 0, NULL); if (!window || !ready || !thread || WaitForSingleObject(ready, 3000) != WAIT_OBJECT_0 || !worker_window) return 3; ShowWindow(window, SW_SHOW); SetForegroundWindow(window); pump(600); BOOL first = GetForegroundWindow() == window && attached_on_focus(window); BOOL background = SendMessageW(worker_window, WM_APP, 0, 0); ShowWindow(worker_window, SW_SHOW); SetForegroundWindow(worker_window); pump(600); BOOL switched = GetForegroundWindow() == worker_window && SendMessageW(worker_window, WM_APP, 0, 0); BOOL old_thread = attached_on_focus(window); SetForegroundWindow(window); pump(600); BOOL restored = GetForegroundWindow() == window && attached_on_focus(window); BOOL detached = !SendMessageW(worker_window, WM_APP, 0, 0); BOOL excluded = GetEnvironmentVariableA("WINE_ACCESS_EXPECT_NO_HOOKS", NULL, 0) != 0; BOOL loaded = GetModuleHandleW(L"wine-access-menu.dll") != NULL || GetModuleHandleW(L"wine-access-menu32.dll") != NULL; printf("hook scope: initial=%d background=%d switched=%d old-thread=%d restored=%d detached=%d\n", first, background, switched, old_thread, restored, detached); PostMessageW(worker_window, WM_CLOSE, 0, 0); WaitForSingleObject(thread, 3000); CloseHandle(thread); CloseHandle(ready); DestroyWindow(window); if (excluded) { printf("excluded process: hook DLL loaded=%d\n", loaded); return !first && !background && !switched && !old_thread && !restored && detached && !loaded ? 0 : 5; } return first && !background && switched && !old_thread && restored && detached ? 0 : 4; }