The name of the current service if the same exe was configured as multiple services

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

For example, 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 really want to know if I am ServiceName1 or ServiceName2. But there seems to be no way to do this.

I tried calling ServiceBase.ServiceName before installing it, but it is just empty.

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

There is a special program called SRVANY.exe and it looks like it can do it. The question is how this is done.

+4
source share
1 answer

I think you will find that you need to specify the servicename property yourself ...

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

... If you did not set the name servicename, then it will be null, and I think sc.exe passes first, and then generates it based on the name exe.

It should also be noted that sc.exe identifies the service through a variety of "name" properties / methods ...

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

... Perhaps because you specify serviename in the code that it generates somehow.

Hope this helps clarify a bit of your confusion, even if it doesn't completely resolve the problem.

0
source

Source: https://habr.com/ru/post/1307655/


All Articles