$ show Chuyu-Team/YY-Thunks
YY-Thunks is a Windows API compatibility shim library that allows applications compiled for modern Windows versions (down to Windows 2000/XP) to run on older systems where those APIs do not natively exist. It achieves this through a sophisticated macro-based thunk framework: each API is declared with `__DEFINE_THUNK` which, at runtime, attempts to resolve the real export via `GetProcAddress` and falls back to a hand-coded emulation if missing. The library covers hundreds of APIs across dozens of system DLLs, including kernel32, bcrypt, user32, shell32, DNS, WinHttp, D3D11, and many more. The fallback implementations often reverse-engineer Windows internals, such as SRWLock/condition variable data structures, HSTRING layout, API-set namespace resolution, and threadpool dispatch. The repository also provides a companion tool (YY.Depends.Analyzer) for analyzing binary dependencies and identifying missing APIs, as well as a PE header patcher (MinimumRequiredVersionHelper) for lowering the reported minimum OS version in executables. The project is delivered as static .obj/.lib files and a NuGet package, integrated into the MSBuild pipeline. It is heavily tested with a custom unit test framework that can force fallback paths via `AwaysNullGuard` guards. While the codebase exhibits high craftsmanship in its emulation patterns, the sheer breadth and reliance on undocumented Windows internals introduce maintenance challenges. Some components (e.g., bcrypt.hpp) are monolithic, and hardcoded offsets or version-specific workarounds require continuous updates as Windows evolves.
The LLM identified 4 files worth highlighting for reuse:
A lock-free single-producer/single-consumer queue implemented using Windows interlocked operations (`InterlockedExchangePointer`, `InterlockedPushEntrySList`). It provides `Push`, `Pop`, and a combined `PushWithInterlockedFlush` that flushes to a system SLIST for cross-CPU visibility. This is a reusable, self-contained header that could be extracted for any Windows project needing a high-performance, non-blocking queue without external dependencies.
A memory allocator for executable thunk code, using `VirtualAlloc` to reserve 64KB blocks and managing a free list of thunk slots (each 64 bytes). Supports allocation (`Alloc`) and deallocation (`Free`) with atomic operations on a `SLIST_HEADER`. The thunks can be used to redirect functions or as trampolines for hooking. This is a standalone utility for generating small code snippets in executable memory, useful for API hooking frameworks or custom runtime patching.
A singleton background thread that creates a hidden window and runs a message loop, enabling synchronous or asynchronous execution of callbacks on a dedicated thread. It provides `Run` to execute a lambda on that thread (posting a message) and `RunAndWait` for synchronous blocking. This pattern is essential for emulating APIs that require a window message pump (e.g., power notifications, clipboard listeners). The implementation is self-contained, using standard Windows thread and window creation, and can be reused in any application needing a lightweight UI-thread-like execution context.
A parser for Windows API-set namespace schema (versions 2, 4, and 6). It reads the `API_SET_NAMESPACE` structure embedded in `apisetschema.dll` and provides functions to resolve an API set name (e.g., `api-ms-win-core-path-l1-1-0`) to the actual host DLL, walking the binary trie/hash tables. This component could be extracted to enable API-set-aware PE analysis in other tools, as it implements the same binary search algorithm that `ntdll` uses internally.