none
How to get running windows service process id ?

    Question

  • Hello All,

    I'm trying to find out information on currently running windows service processes. I looked at following API, I could get the display name ,but not the process id of each running service. I need both service name (first one on General tab on service properties ) and process id.

    I looked at ManagementObject's GetProperty, I can get  Name, caption etc but not process id.
    I looked at ServiceController,I can get Display Name, status etc,but not the process id.

    I looked at Process class, where I can get id (property Id), but not the service name.

    Appreciate your help

    Kishore

    Wednesday, August 04, 2010 8:13 PM

Answers

  • First add reference of System.Management, System.ServiceProcess in your project.

    and try with below functions:

     private uint GetProcessIDByServiceName(string serviceName)
        {
          uint processId = 0;
          string qry = "SELECT PROCESSID FROM WIN32_SERVICE WHERE NAME = '" + serviceName + "'";
          System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(qry);
          foreach (System.Management.ManagementObject mngntObj in searcher.Get())
          {
            processId = (uint)mngntObj["PROCESSID"];
          }
          return processId;
        }
    
        private uint GetProcessIDByServiceDisplayName(string serviceDisplayName)
        {
          uint processId = 0;
          string qry = "SELECT PROCESSID FROM WIN32_SERVICE WHERE DISPLAYNAME = '" + serviceDisplayName + "'";
          System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(qry);
          foreach (System.Management.ManagementObject mngntObj in searcher.Get())
          {
            processId = (uint)mngntObj["PROCESSID"];
          }
          return processId;
        }
    
        private List<System.ServiceProcess.ServiceController> GetRunningServices()
        {
          System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();      
          List<System.ServiceProcess.ServiceController> running = new List<System.ServiceProcess.ServiceController>();
    
          foreach (System.ServiceProcess.ServiceController item in services)
          {
            if (item.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            {
              running.Add(item);
            }
          }
          return running;
        }
    
    

    Kiran Saravade
    • Marked as answer by Bhava Thursday, August 05, 2010 6:51 PM
    Thursday, August 05, 2010 7:59 AM

All replies

  • First add reference of System.Management, System.ServiceProcess in your project.

    and try with below functions:

     private uint GetProcessIDByServiceName(string serviceName)
        {
          uint processId = 0;
          string qry = "SELECT PROCESSID FROM WIN32_SERVICE WHERE NAME = '" + serviceName + "'";
          System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(qry);
          foreach (System.Management.ManagementObject mngntObj in searcher.Get())
          {
            processId = (uint)mngntObj["PROCESSID"];
          }
          return processId;
        }
    
        private uint GetProcessIDByServiceDisplayName(string serviceDisplayName)
        {
          uint processId = 0;
          string qry = "SELECT PROCESSID FROM WIN32_SERVICE WHERE DISPLAYNAME = '" + serviceDisplayName + "'";
          System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(qry);
          foreach (System.Management.ManagementObject mngntObj in searcher.Get())
          {
            processId = (uint)mngntObj["PROCESSID"];
          }
          return processId;
        }
    
        private List<System.ServiceProcess.ServiceController> GetRunningServices()
        {
          System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();      
          List<System.ServiceProcess.ServiceController> running = new List<System.ServiceProcess.ServiceController>();
    
          foreach (System.ServiceProcess.ServiceController item in services)
          {
            if (item.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            {
              running.Add(item);
            }
          }
          return running;
        }
    
    

    Kiran Saravade
    • Marked as answer by Bhava Thursday, August 05, 2010 6:51 PM
    Thursday, August 05, 2010 7:59 AM
  • Thank you Kiran
    Thursday, August 05, 2010 6:51 PM
  • Yes but what about 64bit machine?
    ?
    Wednesday, December 21, 2011 2:36 PM
  •     <Runtime.InteropServices.DllImport("kernel32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto, SetLastError:=True)> _
        Public Function GetCurrentProcessId() As UInt32
        End Function
    

    (: fcm :)

    Monday, June 25, 2012 6:39 AM