Create a Web Service Method to Get NT Service Information | CodeGuru

Create a Web Service Method to Get NT Service Information

Recently, I created a SQL Server Management tool for mobile devices, Siccolo, that allows me to manage SQL Servers by using web services hosted on public domain. (See more information on the Siccolo web site about how to develop a mobile management tool: developing Siccolo—a mobile management tool.) As a part of the management tool, […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 7, 2006
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Recently, I created a SQL Server Management tool for mobile devices, Siccolo, that allows me to manage SQL Servers by using web services hosted on public domain. (See more information on the Siccolo web site about how to develop a mobile management tool: developing Siccolo—a mobile management tool.)

As a part of the management tool, I needed to show some information about selected NT Service, such as the path to the service executable. For example, services.msc shows it like this:

In my mobile management tool, I needed to display in in a similar manner:

The code presented retrieves information about Path to Executable for selected NT Service.

Background (Optional, but Needed)

My “managing” web service is hosted under SSL with “Integrated Windows” authentication set. Therefore, a mobile application is required to pass network credentials. This is needed for remote access to get information from the Registry on the remote machine.

Using the Code

Components used:


  • serviceprocessor.asmx.cs: Web service interface

  • NTServiceInfo.cs: A small “wrapper” to retrieve NT Service information

.NET provides just the tool for this task, the System.ServiceProcess.ServiceController class. To create an instance of System.ServiceProcess.ServiceController, do the following:

// C# //
…
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!=””)
   {Service = new ServiceController(this.m_ServiceName,
                                    this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
…

The fact that authentication (Integrated Windows authentication or Basic) is in place on IIS actually helps here. To be able to access service(s) on the different machine than the web service host, the web service needs to “assume” the identity of an authenticated user. Normally, the web service is running under an ASP.NET user with minimum privileges; I needed to impersonate an authenticated user with the web service.

On the server side, to retrieve authenticated user, you need to use System.Web.Services.WebService.User and then impersonate: The code does just that—”Impersonates the user represented by the WindowsIdentity object.”

// C# //
…
System.Security.Principal.WindowsImpersonationContext
   impersonationContext;
   impersonationContext =
      ((System.Security.Principal.WindowsIdentity)User.Identity).
      Impersonate();
…

To retrieve the path to the executable, you can look under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services, find the selected service, and get “ImagePath”:

To read the value of a Registry key (on the remote machine, or on the local machine):

private string ReadRegestryKey(string RegistryKey,
                               out string ErrorInfo)
{
   try
   {
      string Value=””;
      ErrorInfo =””;
      RegistryKey Key;
      RegistryKey KeyHKLM = Registry.LocalMachine;
      try
      {
         if (this.m_MachineName !=”” )    //open on remote machine
            Key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(
               RegistryHive.LocalMachine, this.m_MachineName
                  ).OpenSubKey(RegistryKey);
         else
            Key = KeyHKLM.OpenSubKey(RegistryKey);
         Value = Key.GetValue(“ImagePath”).ToString();
         Key.Close();
      }
      catch (Exception ex_open_key)
      {
         ErrorInfo = “Error Accessing Registry
            [” + ex_open_key.ToString() + “]”;
         return “”;
      }
      return Value;
   }
   catch (Exception ex_read_registry)
   {
      ErrorInfo =   ex_read_registry.Message;
      return “”;
   }
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.