How to retrieve service name at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UVNJRGV2ZWxvcGVy?=

    #1

    How to retrieve service name at runtime

    I have a Windows service that we need to have multiple instances running. We
    achieve this by using InstallUtil Service.exe /name="SomeServi ceName". I
    need to know the name of the instance of the service that is running at
    runtime, the name displayed in the Services UI. If I examine ServiceName at
    runtime I do not get the name assigned by InstallUtil that is shown in the
    services UI nor do I get the default name assigned by the ServiceInstalle r
    class. Is there some way to retrieve this information at runtime?
  • Feng Chen[MSFT]

    #2
    RE: How to retrieve service name at runtime

    Hi

    This is a quick note to let you know that I am performing research on this
    issue and will get back to you as soon as possible. I appreciate your
    patience.

    Best regards,
    Feng Chen
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.
    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscripti...ult.aspx#notif
    ications .

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscripti...t/default.aspx .
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Feng Chen[MSFT]

      #3
      RE: How to retrieve service name at runtime

      Hello,

      By InstallUtil, I assume you mean the Installer Tool (Installutil.ex e) -
      http://msdn.microsoft.com/en-us/libr...95(VS.90).aspx. However, this
      tool does not have the '/name' argument. If I have misunderstood you,
      please let me know.

      For a windows service with multi-instance running at the same time, we can
      use WMI Win32_Service
      class(http://msdn.microsoft.com/en-us/libr...18(VS.85).aspx) to
      query the DisplayName of a service by specifying the Process ID of the
      running service. And I write a code snippet:

      using System;
      using System.Diagnost ics;
      using System.Manageme nt; // Add System.Manageme nt as reference to the
      project.

      {
      // Get process ID of current running service instance
      int Pid = Process.GetCurr entProcess().Id ;

      ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(
      "root\\CIMV 2", "SELECT DisplayName FROM Win32_Service WHERE
      ProcessId = " + Pid);

      foreach (ManagementObje ct queryObj in searcher.Get())
      {
      String displayName = queryObj["DisplayNam e"].ToString();
      break;
      }

      Debug.WriteLine ( "Display name of the current service: " + displayName
      );
      }

      Please try the code above and let me know if it does not help you you need
      any other help.

      Best regards,
      Feng Chen
      Microsoft Online Community Support
      =============== =============== ===========
      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can improve the support we provide to you. Please
      feel free to let my manager know what you think of the level of service
      provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.

      This posting is provided "AS IS" with no warranties, and confers no rights

      Comment

      • =?Utf-8?B?UVNJRGV2ZWxvcGVy?=

        #4
        RE: How to retrieve service name at runtime

        Sorry I had missed your reply.

        I tried the code and it does work as you said.

        BTW we added the parameter ‘name’ to change the display name of the service .

        Thank you for your help.


        "Feng Chen[MSFT]" wrote:
        Hello,
        >
        By InstallUtil, I assume you mean the Installer Tool (Installutil.ex e) -
        http://msdn.microsoft.com/en-us/libr...95(VS.90).aspx. However, this
        tool does not have the '/name' argument. If I have misunderstood you,
        please let me know.
        >
        For a windows service with multi-instance running at the same time, we can
        use WMI Win32_Service
        class(http://msdn.microsoft.com/en-us/libr...18(VS.85).aspx) to
        query the DisplayName of a service by specifying the Process ID of the
        running service. And I write a code snippet:
        >
        using System;
        using System.Diagnost ics;
        using System.Manageme nt; // Add System.Manageme nt as reference to the
        project.
        >
        {
        // Get process ID of current running service instance
        int Pid = Process.GetCurr entProcess().Id ;
        >
        ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(
        "root\\CIMV 2", "SELECT DisplayName FROM Win32_Service WHERE
        ProcessId = " + Pid);
        >
        foreach (ManagementObje ct queryObj in searcher.Get())
        {
        String displayName = queryObj["DisplayNam e"].ToString();
        break;
        }
        >
        Debug.WriteLine ( "Display name of the current service: " + displayName
        );
        }
        >
        Please try the code above and let me know if it does not help you you need
        any other help.
        >
        Best regards,
        Feng Chen
        Microsoft Online Community Support
        =============== =============== ===========
        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.
        >
        This posting is provided "AS IS" with no warranties, and confers no rights
        >
        >

        Comment

        Working...