How to retrieve service name at runtime

  • Thread starter Thread starter QSIDeveloper
  • Start date Start date
Q

QSIDeveloper

I have a Windows service that we need to have multiple instances running. We
achieve this by using InstallUtil Service.exe /name="SomeServiceName". 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 ServiceInstaller
class. Is there some way to retrieve this information 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:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx .
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello,

By InstallUtil, I assume you mean the Installer Tool (Installutil.exe) -
http://msdn.microsoft.com/en-us/library/50614e95(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/library/aa394418(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.Diagnostics;
using System.Management; // Add System.Management as reference to the
project.

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

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"root\\CIMV2", "SELECT DisplayName FROM Win32_Service WHERE
ProcessId = " + Pid);

foreach (ManagementObject queryObj in searcher.Get())
{
String displayName = queryObj["DisplayName"].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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights
 
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.
 
Back
Top