nt_status.cc 1.1 KB

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