none
Get Service Display Name from windows service OnStart Method RRS feed

  • Question

  • Hi,

    I already Installed a windows service and listed in Service List "RancelabDS_Demo"

    When I start that service. In OnStart() Method of my windows service code, how to get that Service Name / DisplayName ?

    I need to start only "RancelabDS_Demo" and not any other like "RancelabDS_Demo123".

    how to check in windows service code?

    please help as soon as possible.


    Arindam Banerjee

    Wednesday, September 19, 2012 10:38 AM

Answers

  • Normally, a Service has a unique and only one name once it is out of development stage. You will perform any of the administrations on a Service through code using this unique name - like Start, Stop, Pause and Continue. A clean way.

    Of course, it is only the normal case and I have never come across a Windows Service that requires to change its name dynamically. Otherwise, you can understand that managing such an infrastructure becomes unstable.

    I believe you would definitely have a valid reason to have different names at different times. For me, this reason looks more interesting to address your problem. Your problem may be just a symptom and this reason may be the root cause. We will try to attack the root cause.

    Sorry if I have overunderstood your problem though.

    Off-note: A nice walk-through on creating a Windows Service is available here anyway.

    Thursday, September 20, 2012 7:56 AM
  • The Net library has a namesspace System.ServiceProcess 

    See this webpage for sample code

    http://www.codeproject.com/Articles/11810/Service-Controller

    I'm not sure if the code runs with the latest version of Net, so if you have problems try the pinvoke method below.

    See webpage below.  Look at all the function in the advapi32.dll

    http://www.pinvoke.net/default.aspx/advapi32/QueryServiceConfig2.html


    jdweng

    Thursday, September 20, 2012 5:58 AM
  • Of course, it is only the normal case and I have never come across a Windows Service that requires to change its name dynamically.

    So you've never used SQL Server? ;-) The name changes based on the instance name used when it's installed i.e. MSSQL$SQLEXPRESS MSSQL$FRED etc.

    For the OP. The important thing is your service name MUST match the name the service controller expects when the service starts. Inside OnStart you can access this name via this.ServiceName. Once you know that, you can use the service controller to get the display name, but I prefer to just look in the registry... Something like:

    displayName = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\" + this.ServiceName, 
        "DisplayName", 
        this.ServiceName).ToString();


    This signature unintentionally left blank.



    Friday, September 21, 2012 12:55 PM

All replies

  • I think you need to get the window handle and then name that is shown on the window.  That would require getting the process name of the exe and getting the handle from the process..  Is that what you are looking for?

    jdweng

    Wednesday, September 19, 2012 12:15 PM
  • Suppose windows service name ="RancelabDS_Demo"

    when I start it, I want to take that name in OnStart() method

    Please give some code example


    Arindam Banerjee

    Wednesday, September 19, 2012 12:28 PM
  • Whether the following code in your "ServiceBase" inherited class helps?

    public partial class MyTestService : ServiceBase
    {
      protected override void OnStart (string[] args)
      {
        ServiceController sc = new ServiceController (this.ServiceName);
    
        // sc.DisplayName represents the display name of service
        eventLog.WriteEntry ("DisplayName: " + sc.DisplayName);
      }
    }


    Wednesday, September 19, 2012 3:02 PM
  • That's not solve my problem

    I do not know what is my service name. it's random. "RancelabDS_Demo" or "RancelabDS_Demo123" or "RancelabDS_DemoXYZ"



    Arindam Banerjee

    Thursday, September 20, 2012 4:49 AM
  • The Net library has a namesspace System.ServiceProcess 

    See this webpage for sample code

    http://www.codeproject.com/Articles/11810/Service-Controller

    I'm not sure if the code runs with the latest version of Net, so if you have problems try the pinvoke method below.

    See webpage below.  Look at all the function in the advapi32.dll

    http://www.pinvoke.net/default.aspx/advapi32/QueryServiceConfig2.html


    jdweng

    Thursday, September 20, 2012 5:58 AM
  • Normally, a Service has a unique and only one name once it is out of development stage. You will perform any of the administrations on a Service through code using this unique name - like Start, Stop, Pause and Continue. A clean way.

    Of course, it is only the normal case and I have never come across a Windows Service that requires to change its name dynamically. Otherwise, you can understand that managing such an infrastructure becomes unstable.

    I believe you would definitely have a valid reason to have different names at different times. For me, this reason looks more interesting to address your problem. Your problem may be just a symptom and this reason may be the root cause. We will try to attack the root cause.

    Sorry if I have overunderstood your problem though.

    Off-note: A nice walk-through on creating a Windows Service is available here anyway.

    Thursday, September 20, 2012 7:56 AM
  • Go to projectinstaller class and look for the below code like :

    this.ServiceInstaller1.DisplayName

    set this value to a global variable and then use this in your class


    Please mark it as an answer/helpful if you find it as useful. Thanks, Satya Prakash Jugran

    Thursday, September 20, 2012 8:19 AM
  • Hi Satya Jugran,

    It is true that DisplayName is set through ServiceInstaller class (when installing through Windows Installer project).

    This DisplayName can be read from OnStart method only by supplying the ServiceName to ServiceController as I have provided the code above. From .Net code, ServiceController is the simple and elegant way to read all the information about all the windows services in the system.

    PdNet's requirement is to read DisplayName from OnStart method but without supplying ServiceName because it keeps changing. In my opinion, it is not possible because ServiceName is the Key-identifier.

    That's the reason we need to know PdNet's requirements first.

    Hope I have not misunderstood your answer. Please feel free to correct me if I am wrong.

    Thursday, September 20, 2012 8:59 AM
  • Of course, it is only the normal case and I have never come across a Windows Service that requires to change its name dynamically.

    So you've never used SQL Server? ;-) The name changes based on the instance name used when it's installed i.e. MSSQL$SQLEXPRESS MSSQL$FRED etc.

    For the OP. The important thing is your service name MUST match the name the service controller expects when the service starts. Inside OnStart you can access this name via this.ServiceName. Once you know that, you can use the service controller to get the display name, but I prefer to just look in the registry... Something like:

    displayName = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\" + this.ServiceName, 
        "DisplayName", 
        this.ServiceName).ToString();


    This signature unintentionally left blank.



    Friday, September 21, 2012 12:55 PM
  • I hope I am not overlooking your words.

    Yes, I have used SQL Server, SSIS and SSRS. You are just talking about a prefix. Even in SQL Server, does your service name changes dynamically? As a side note, SSRS specific services assume a prefix as "ReportServer", right?

    If you look in depth about how those services are assuming such prefixes, this behavior is in their installers. The SQL Server or SSRS service installers name them like that during the installation (service registration) process. It means that the installers know their "exact" names precisely.

    That's why the key point here is to know the ServiceName (and not whether it is prefixed). I have mentioned clearly that the service does not change its name "dynamically" once it is installed. Of course, you can change it yourself. But even then you know its precise name, right?

    Once you know the ServiceName, you can go with either my .Net way or your Registry way.

    Under the hood, .Net of course hits your Registry entry. But I prefer to work from a higher abstraction like .Net than the lowest nitty-gritty like Registry because it takes care of many deeper details. In your code also, Windows internally takes care of the required Registry virtualization for you, right?

    So, my first question to PdNet stands good still: "Why your ServiceName changes dynamically or frequently?"

    Friday, September 21, 2012 1:25 PM
  • The service manager forces a relationship between service names and registry keys under HKLM\System\CurrentControlSet\Services.  In other words, if the registry key isn't there, it's not a serivce. 

    To oversimplify the process, The service manager enumerates these keys, and starts each service in turn.  If it starts "w3svc" the exe launched MUST register itself exactly as "w3svc" when it comes up or the service manager will freak out and post that ugly "At least one service failed to start" message box on the machine console.

    However an EXE can be a service under many names, and even host more than one service.  SQL Server is a good example because everyone's heard about it.  If you have more than one instance on the same machine, you'll see multiple keys under the registry, but they (can) all point to the same sqlsevr.exe file.

    So for our OP, I'm assuming dynamic in this discussion refers to the name being decided at install time.  I don't see anything else as possible.  He's faced with a problem. 

    He needs a way to identify himself.  This can be difficult.  At the lowest level, there's no predefined way to know the expected name when the process starts. This carries up to the .NET level which is why when you create a new service project you get the line of code: this.ServiceName ="Service1";  SQL server accomplishes this by the -s parameter on the ImagePath.  -sSQLEXPRESS tells it to launch using the service name as MSSQL$SQLEXPRESS instead of the default name MSSQLSERVER.

    Only after the correct ServiceName is known can the DisplayName be discovered.  In which case either of our methods will work.


    This signature unintentionally left blank.


    • Edited by Nick F. _ Monday, September 24, 2012 11:18 AM
    Monday, September 24, 2012 11:16 AM