From 022aee7729f1a2713e0776d7186a67d5d4de6a84 Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sun, 6 Feb 2022 18:59:58 +0100 Subject: [PATCH] NOISSUE Implement status code lookup for Win32 --- libraries/systeminfo/CMakeLists.txt | 2 +- libraries/systeminfo/src/sys_win32.cpp | 40 ++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/libraries/systeminfo/CMakeLists.txt b/libraries/systeminfo/CMakeLists.txt index a05a3de0..d7d48faf 100644 --- a/libraries/systeminfo/CMakeLists.txt +++ b/libraries/systeminfo/CMakeLists.txt @@ -39,7 +39,7 @@ if (WIN32) target_compile_definitions(systeminfo-ntstatus-gen PRIVATE NTSTATUS_PREPROCESSOR_OUT=\"${systeminfo_PREPROCESSOR_OUT}\") - set(NTSTATUS_GEN_HEADER "${CMAKE_CURRENT_BINARY_DIR}/NtStatsuGen.h") + set(NTSTATUS_GEN_HEADER "${CMAKE_CURRENT_BINARY_DIR}/NtStatusGen.h") set(NTSTATUS_GEN_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/NtStatusGen.cpp") add_custom_command( diff --git a/libraries/systeminfo/src/sys_win32.cpp b/libraries/systeminfo/src/sys_win32.cpp index 6c3b2642..da9ca97c 100644 --- a/libraries/systeminfo/src/sys_win32.cpp +++ b/libraries/systeminfo/src/sys_win32.cpp @@ -1,8 +1,9 @@ #include "sys.h" #include +#include -#include "NtStatsuGen.h" +#include "NtStatusGen.h" Sys::KernelInfo Sys::getKernelInfo() { @@ -59,6 +60,41 @@ Sys::DistributionInfo Sys::getDistributionInfo() bool Sys::lookupSystemStatusCode(uint64_t code, std::string &name, std::string &description) { + bool hasCodeName = Win32::lookupNtStatusCodeName(code, name); + if(!hasCodeName) + { + name = "unknown status"; + } - return false; + PSTR messageBuffer = nullptr; + HMODULE ntdll = GetModuleHandleA("ntdll.dll"); + if(!ntdll) + { + // ??? + qWarning() << "GetModuleHandleA returned nullptr for ntdll.dll"; + return false; + } + + auto messageSize = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, + ntdll, + code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&messageBuffer), + 0, + nullptr + ); + + bool hasDescription = messageSize > 0; + if(hasDescription) + { + description = std::string(messageBuffer, messageSize); + } + + if(messageBuffer) + { + LocalFree(messageBuffer); + } + + return hasCodeName || hasDescription; }