Replace manual bitmasking of the HRESULT with HRESULT_FACILITY() and HRESULT_SEVERITY() . This makes the Win32 error check clearer and uses standard macros for determining FACILITY_WIN32 and SEVERITY_ERROR while preserving the original behavior.
Replace sprintf_s-based Unicode escaping with a manual construction of the "\u00xx" sequence using lowercase hex digits and an explicit ANSI_NULL terminator, and fix the appended byte length to 6. Adjust string length handling for formatted buffers to use the actual return length (not subtracting the null). Consolidate integer formatting to use PhIntegerToUtf8Buffer and append its result directly, removing older fallback/unused code. Also remove a redundant double-formatting fallback, simplifying the JSON serialization path and avoiding sprintf_s where possible.
Compute explicit start/end addresses for user and buffer ranges and add proper wrap/overflow checks to prevent integer wraparound and access violations. Replace MAXULONG_PTR and (ULONG_PTR)-PAGE_SIZE uses with ULONG_PTR_MAX-based comparison. Use PAGE_ALIGN + PAGE_SIZE to advance page pointers and normalize volatile dereference syntax for readability. These changes tighten bounds checking in PhProbeAddress/PhProbeForRead/PhProbeForWrite to avoid subtle failures on pointer wraparound.
Treat returnLength as a WCHAR count instead of bytes: compute it from time/date buffer sizes, compare BufferLength after converting to bytes, and return the output length in bytes. Replace the previous sizeof(UNICODE_NULL) hack with (returnLength - 1) * sizeof(WCHAR). On failure, set *ReturnLength to 0. These changes correct off-bytes/termination size handling when formatting date/time into the buffer.