From 077dbaffcbd6069630696e587747303797d0309c Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 6 Feb 2024 12:10:17 -0300 Subject: [PATCH] Re-impl get_process_name() for Win32 using QueryFullProcessImageNameW() In this way we avoid creating a snapshot/iterating all processes. --- base/process.cpp | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/base/process.cpp b/base/process.cpp index 0862fac85..e0330aa24 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -11,11 +11,11 @@ #include "base/process.h" +#include "base/fs.h" #include "base/string.h" #if LAF_WINDOWS #include - #include #else #include #include @@ -27,7 +27,6 @@ #endif #if LAF_LINUX - #include "base/fs.h" #include #include #endif @@ -59,20 +58,17 @@ bool is_process_running(pid pid) std::string get_process_name(pid pid) { - bool running = false; - HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); + std::string name; + HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); if (handle) { - PROCESSENTRY32 pe; - pe.dwSize = sizeof(PROCESSENTRY32); - if (Process32First(handle, &pe)) { - do { - if (pe.th32ProcessID == pid) - return base::to_utf8(pe.szExeFile); - } while (Process32Next(handle, &pe)); + WCHAR exeName[MAX_PATH]; + DWORD len = MAX_PATH-1; + if (QueryFullProcessImageNameW(handle, 0, exeName, &len)) { + name = base::get_file_name(base::to_utf8(exeName)); } CloseHandle(handle); } - return std::string(); + return name; } #else