I have a service application and I want to retrieve it's display name during run-time. I tried using base.ServiceName, but I'm not getting the expected name. I think I should be referring to display name instead, but the servicebase class doesn't have a property for displayname.
Thanks.
Loading
Jan MontanoPosted Jan 28, 2008, 1:46 AM
It was my fault why the base.ServiceName is not returning the expected result. As for the registry initialization for the service parameters, I did an override of Installer.Install(IDictionary stateSaver) and performed the registry initialization within the method.
Thanks again.
AlanPosted Dec 21, 2007, 5:23 PM
Sorry for the delay in getting back to you on this, Jan, but I still don't have a solution.
I think you'll only be able to access the ServiceController class from the ServiceBase class at runtime if the two have been compiled into a single assembly.
As I understand it, the service console only shows the service display name, whereas the service is listed in the registry (under HKLM\System\CurrentControlSet\Services) using its key name though the display name is also shown under this.
Frankly, I don't understand why the base.ServiceName property is only showing part of the service key name but, if it is, then this makes life difficult in obtaining the display name from the registry programatically. Perhaps it will work if you just use a single word for the key name?
For what it's worth I did come up with a way of getting the display name via API calls though again you have to have the correct key name to start with:
using System;
using System.Text;
using System.Runtime.InteropServices;
class Program
{
[DllImport("advapi32.dll")]
static extern bool CloseServiceHandle(IntPtr hSCObject);
[DllImport("advapi32.dll", EntryPoint="OpenSCManagerW", ExactSpelling=true, CharSet=CharSet.Unicode)]
static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);
[DllImport("advapi32.dll", EntryPoint="GetServiceDisplayNameW", ExactSpelling=true, CharSet=CharSet.Unicode)]
static extern bool GetServiceDisplayName(IntPtr hSCManager, string lpServiceName, StringBuilder lpDisplayName, ref uint lpcchBuffer);
const uint SC_MANAGER_CONNECT = 1;
static void Main()
{
uint lpcchBuffer = 255;
IntPtr hSCManager = OpenSCManager(null, null, SC_MANAGER_CONNECT);
StringBuilder lpDisplayName = new StringBuilder(256);
string lpServiceName = "W32Time";
GetServiceDisplayName(hSCManager, lpServiceName, lpDisplayName, ref lpcchBuffer);
string displayName = lpDisplayName.ToString();
CloseServiceHandle(hSCManager);
Console.WriteLine(displayName); // Windows Time
Console.ReadLine();
}
}
As you'll see, I just tested this on the Windows system service W32Time as I don't have a Service template on my machine to create my own test service.
I wondered if there might be a way to get the service key name from the base.ServiceHandle property but I couldn't see any obvious way of doing this :(
Jan MontanoPosted Dec 17, 2007, 8:40 PM
Accessing the registry value for the display name seems to be the best approach to take. But I was wondering if I could actually access the serviceinstaller class from a ServiceBase derived class during run-time?
My app, has self installation functionality wherein I used a serviceinstaller to install itself. Now on the OnStart() method, is it possible to access the display name from serviceinstaller? Could we load the current running service into a serviceinstaller? then retrieve it's display name? I may be wrong but this is what comes to my mind if the display name is only accessible from the serviceinstaller.
Please do enlighten me. hehehe. =)
*edit
But wait!!! From the service console, my service's name and display name is "AppName - ServerName DBName" However when I call, base.ServiceName, it only gives me "AppName" why? And if this base.ServiceName only gives me an "AppName" I won't be able to access the registry key properly. I might be doing something wrong.
AlanPosted Dec 17, 2007, 12:32 PM
Hi Jan,
The DisplayName property is actually in the ServiceInstaller rather than the ServiceBase class:
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.displayname.aspx