1 // Copyright 2021 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/win/nt_status.h" 6 7 #include <windows.h> 8 #include <winternl.h> 9 10 #include "base/check.h" 11 12 using GetLastNtStatusFn = NTSTATUS NTAPI (*)(); 13 14 constexpr const wchar_t kNtDllName[] = L"ntdll.dll"; 15 constexpr const char kLastStatusFnName[] = "RtlGetLastNtStatus"; 16 17 namespace base { 18 namespace win { 19 GetLastNtStatus()20NTSTATUS GetLastNtStatus() { 21 // This is equivalent to calling NtCurrentTeb() and extracting 22 // LastStatusValue from the returned _TEB structure, except that the public 23 // _TEB struct definition does not actually specify the location of the 24 // LastStatusValue field. We avoid depending on such a definition by 25 // internally using RtGetLastNtStatus() from ntdll.dll instead. 26 static auto* get_last_nt_status = reinterpret_cast<GetLastNtStatusFn>( 27 ::GetProcAddress(::GetModuleHandle(kNtDllName), kLastStatusFnName)); 28 return get_last_nt_status(); 29 } 30 31 } // namespace win 32 } // namespace base 33