| 7 | #include "win32.hpp" |
| 8 | |
| 9 | std::optional<std::filesystem::path> Window::TryGetNtImageName(DWORD pid) |
| 10 | { |
| 11 | static const auto NtQuerySystemInformation = []() noexcept -> PFN_NT_QUERY_SYSTEM_INFORMATION |
| 12 | { |
| 13 | const auto ntdll = GetModuleHandle(L"ntdll.dll"); |
| 14 | return ntdll ? reinterpret_cast<PFN_NT_QUERY_SYSTEM_INFORMATION>(GetProcAddress(ntdll, "NtQuerySystemInformation")) : nullptr; |
| 15 | }(); |
| 16 | |
| 17 | if (NtQuerySystemInformation) |
| 18 | { |
| 19 | SYSTEM_PROCESS_ID_INFORMATION pidInfo = { |
| 20 | #pragma warning(suppress: 4312) // intentional, the structure uses a pointer to store PIDs |
| 21 | .ProcessId = reinterpret_cast<PVOID>(pid) |
| 22 | }; |
| 23 | |
| 24 | if (NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(SystemProcessIdInformation), &pidInfo, sizeof(pidInfo), nullptr) == STATUS_INFO_LENGTH_MISMATCH_UNDOC) |
| 25 | { |
| 26 | std::wstring buf; |
| 27 | buf.resize_and_overwrite(pidInfo.ImageName.MaximumLength / 2, [&pidInfo](wchar_t* data, std::size_t count) |
| 28 | { |
| 29 | pidInfo.ImageName.Buffer = data; |
| 30 | pidInfo.ImageName.MaximumLength = static_cast<USHORT>(count + 1) * 2; |
| 31 | |
| 32 | // hopefully the process didn't die midway through this lol |
| 33 | if (NT_SUCCESS(NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(SystemProcessIdInformation), &pidInfo, sizeof(pidInfo), nullptr))) |
| 34 | { |
| 35 | return pidInfo.ImageName.Length / 2; |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | return 0; |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | if (!buf.empty()) |
| 44 | { |
| 45 | return std::move(buf); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return std::nullopt; |
| 51 | } |
nothing calls this directly
no outgoing calls
no test coverage detected