4

Is there a way to get the service name from C# that the current service was registered with when it was installed.

For instance, I register the same service twice:

sc.exe create ServiceName1 binPath= D:\myservice.exe
sc.exe create ServiceName2 binPath= D:\myservice.exe

In my service, I actually want to know if I am ServiceName1 or ServiceName2. But there seem to be no way to do that.

I tried calling ServiceBase.ServiceName prior to setting it, but it is just empty.

I tried ServiceController.GetServices().Where(svc => svc.ServiceHandle.DangerousGetHandle() == myservice.ServiceHandle) but it seems to get another handle, so they are not compareable.

There is a special program called SRVANY.exe and that seems to be able to do it. The question is just how it does so.

Cine
  • 4,255
  • 26
  • 46
  • I can see it is a duplicate of http://stackoverflow.com/questions/1841790/how-can-a-windows-service-determine-its-servicename – Cine Apr 22 '10 at 10:38

1 Answers1

0

I think you'll find you need to specify the servicename property yourself ...

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.servicename(VS.80).aspx

... if you haven't set the servicename then it will be null, and I think sc.exe goes by that first then generates one based on the exe name.

Also another thing to note is that sc.exe identifies service through lots of "name" properties / methods ...

http://support.microsoft.com/kb/251192

... Maybe because you are specifying the serviename in code it's generating one somehow.

Hope this helps clarify a bit of your confusion even it don't completely resolve the issue.

War
  • 8,539
  • 4
  • 46
  • 98