util/oslib-win32: Implement qemu_get_pid_name()
PATCH 1/1 util/oslib-win32: Implement qemu_get_pid_name()
Replace the abort() stub with a proper implementation using QueryFullProcessImageNameW() and g_utf16_to_utf8() to retrieve the process executable name on Windows. Returns NULL on error to match POSIX behavior.
--- util/oslib-win32.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
util/oslib-win32.c +17 -2
@@ -287,8 +287,23 @@ bool qemu_finish_async_prealloc_mem(Error **errp)char *qemu_get_pid_name (pid_t pid) { -/* XXX Implement me */ -abort (); +HANDLE hProcess; +WCHAR path[4096 ]; +DWORD size =G_N_ELEMENTS (path); +char *name =NULL ; + + hProcess =OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION , FALSE, (DWORD )pid); +if (!hProcess) { +return NULL ; + } + +if (QueryFullProcessImageNameW (hProcess,0 , path, &size)) { +const WCHAR *base =wcsrchr (path,L'\\' ); + name =g_utf16_to_utf8 (base ? base +1 : path,-1 ,NULL ,NULL ,NULL ); + } + +CloseHandle (hProcess); +return name; }
Signature · 1 line
2.43.0
ping
https://patchew.org/QEMU/[email protected]/
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg05258.html
Quoted 46 lines · Evgeny Kolmakov
Replace the
abort()stub with a proper implementation usingQueryFullProcessImageNameW()andg_utf16_to_utf8()to retrieve the process executable name on Windows. Returns NULL on error to match POSIX behavior.Signed-off-by Evgeny Kolmakov <[email protected]>--- util/oslib-win32.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)util/oslib-win32.c +17
-3@@ -287,8 +287,23 @@ bool qemu_finish_async_prealloc_mem(Error **errp)char *qemu_get_pid_name (pid_t pid) { -/* XXX Implement me */ -abort (); +HANDLE hProcess; +WCHAR path[4096 ]; +DWORD size =G_N_ELEMENTS (path); +char *name =NULL ; + + hProcess =OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION , FALSE, (DWORD )pid); +if (!hProcess) { +return NULL ; + } + +if (QueryFullProcessImageNameW (hProcess,0 , path, &size)) { +const WCHAR *base =wcsrchr (path,L'\\' ); + name =g_utf16_to_utf8 (base ? base +1 : path,-1 ,NULL ,NULL ,NULL ); + } + +CloseHandle (hProcess); +return name; } --2.43.0
Am 26.07.26 um 12:18 schrieb Evgeny:
Quoted 21 lines
ping
https://patchew.org/QEMU/[email protected]/
https://lists.gnu.org/archive/html/qemu-devel/2026-06/msg05258.htmlOn Sun, Jun 21, 2026 at 4:08 PM Evgeny Kolmakov <[email protected]> wrote:
Replace the abort() stub with a proper implementation using QueryFullProcessImageNameW() and g_utf16_to_utf8() to retrieve the process executable name on Windows. Returns NULL on error to match POSIX behavior. Signed-off-by: Evgeny Kolmakov <[email protected]> --- util/oslib-win32.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 5f3e8f4d98..2b083b5d7a 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c
Quoted 9 lines
@@ -287,8 +287,23 @@ bool qemu_finish_async_prealloc_mem(Error **errp)
char *qemu_get_pid_name(pid_t pid)
{
- /* XXX Implement me */
- abort();
+ HANDLE hProcess;
+ WCHAR path[4096];Would it be better to dynamically allocate the maximum path length (32768)?
Quoted 5 lines
+ DWORD size = G_N_ELEMENTS(path);
+ char *name = NULL;
+
+ hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, (DWORD)pid);Quoted 9 lines
+ if (!hProcess) {
+ return NULL;
+ }
+
+ if (QueryFullProcessImageNameW(hProcess, 0, path, &size)) {
+ const WCHAR *base = wcsrchr(path, L'\\');
+ name = g_utf16_to_utf8(base ? base + 1 : path, -1, NULL,
NULL, NULL);I think that base will never be NULL, so the code here could be simplified.
Quoted 10 lines
+ }
+
+ CloseHandle(hProcess);
+ return name;
}
--
2.43.0Do you have a special use case which requires this implementation because it triggers the assertion otherwise?
Regards
Stefan Weil