giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.38k forks source link

[Windows] win_service_get by PID and not by name #2423

Closed arossert closed 3 months ago

arossert commented 3 months ago

Summary

Description

I have a use case where I have a PID and I want to know what service it is, the process name for this PID is svchost.exe but I want to get the service name.

The only option that I have now is to iterate the full list using win_service_iter() and look for the service with this PID, can you add a new function that will retrieve the service by the PID?

giampaolo commented 3 months ago

Mmm I don't think it's worth it. Services are inherently bound to a name. Such a function would basically be a one-liner which you can easily implement in your own code.

Each new API you add makes the project and doc bigger / more complex, thus it has a cost. As such it makes sense to only add utility functions which solve a either complex or very common problem (like process_iter()).

arossert commented 3 months ago

@giampaolo Thanks for the reply, I just wonder if there is a Windows API to get the service by its PID and not the name it is worth exposing it as iterating over all the services just to find the one that I need can be a "heavy" call (I assume).

I'm not a windows expert so this API might not event exists 😃

giampaolo commented 3 months ago

I doubt it exists because the Windows API which returns a service handler accepts the service name (aka a string): https://github.com/giampaolo/psutil/blob/89b6096f2a026ec85f2a188920877371d4515b60/psutil/arch/windows/services.c#L30