Search code examples
c++winapiwindows-services

How can we find the process ID of a running Windows Service?


I'm looking for a good way to find the process ID of a particular Windows Service.

In particular, I need to find the pid of the default "WebClient" service that ships with Windows. It's hosted as a "local service" within a svchost.exe process. I see that when I use netstat to see what processes are using what ports it lists [WebClient] under the process name, so I'm hoping that there is some (relatively) simple mechanism to find this information.


Solution

  • QueryServiceStatusEx returns a SERVICE_STATUS_PROCESS, which contains the process identifier for the process under which the service is running.

    You can use OpenService to obtain a handle to a service from its name.