static bool GetThreadContext()

in include/wil/result.h [979:1021]


            static bool GetThreadContext(_Inout_ FailureInfo *pFailure, _In_opt_ ThreadFailureCallbackHolder *pCallback, _Out_writes_(callContextStringLength) _Post_z_ PSTR callContextString, _Pre_satisfies_(callContextStringLength > 0) size_t callContextStringLength)
            {
                *callContextString = '\0';
                bool foundContext = false;
                if (pCallback != nullptr)
                {
                    foundContext = GetThreadContext(pFailure, pCallback->m_pNext, callContextString, callContextStringLength);

                    if (pCallback->m_pCallContext != nullptr)
                    {
                        auto &context = *pCallback->m_pCallContext;

                        // We generate the next telemetry ID only when we've found an error (avoid always incrementing)
                        if (context.contextId == 0)
                        {
                            context.contextId = ::InterlockedIncrementNoFence(&s_telemetryId);
                        }

                        if (pFailure->callContextOriginating.contextId == 0)
                        {
                            pFailure->callContextOriginating = context;
                        }

                        pFailure->callContextCurrent = context;

                        auto callContextStringEnd = callContextString + callContextStringLength;
                        callContextString += strlen(callContextString);

                        if ((callContextStringEnd - callContextString) > 2)     // room for at least the slash + null
                        {
                            *callContextString++ = '\\';
                            auto nameSizeBytes = strlen(context.contextName) + 1;
                            size_t remainingBytes = static_cast<size_t>(callContextStringEnd - callContextString);
                            auto copyBytes = (nameSizeBytes < remainingBytes) ? nameSizeBytes : remainingBytes;
                            memcpy_s(callContextString, remainingBytes, context.contextName, copyBytes);
                            *(callContextString + (copyBytes - 1)) = '\0';
                        }

                        return true;
                    }
                }
                return foundContext;
            }