79

I have a windows service executable that I know is written in .NET which I need to install under a different service name to avoid a conflict. The install doesn't provide anyway to specify a service name. If I only have access to the binary, is there anyway to override the service name when I install it with installutil?

5 Answers 5

118

Do you have to use InstallUtil? Here are the commands to do what you want using sc:

sc create MyService binPath= "MyService.exe" DisplayName= "MyService"  
sc description MyService "My description"

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

Sign up to request clarification or add additional context in comments.

5 Comments

This looks like exactly what I want -- however I can't get it to work. I just keep getting a "usage" message.
My problem was that there apparently must be a space between the equal sign and the binPath value, e.g. sc create ahSchedulerService binPath= "MyService.exe", not sc create ahSchedulerService binPath="MyService.exe".
Ah, I forgot about that. Sorry for giving you a bad example.
When I used the SC command to create a service instance, I found that I had to put the entire path before the EXE name. Before running SC, I had changed my command prompt directory to be the same as the EXE, thinking that would be enough, but it wasn’t. When I tried to start the service, I got an error saying “system cannot find the file specified”. So the SC command has to have a parameter like: binPath= "C:\whatever\servieName.exe"
When running from PowerShell window, make sure to use sc.exe instead of sc, otherwise PowerShell thinks you mean Set-Content.
30

It is not true that InstallUtil doesn't allow you to configure the service name. I do it all the time like this

InstallUtil.exe /servicename="<service name>" "<path to service exe>"

6 Comments

if you already have a service with same name as exe, it will give error System.ComponentModel.Win32Exception: The specified service already exists. I was trying to install 2 instances of same service and naming them differently. Use sc create methods given in below answers
Does not work. Gives an error that my service already exists.
Works if you have a project installer and override the install and uninstall like in @Volodymyrs answer stackoverflow.com/a/25259719/169714
does not work on a default installer created from Visual studio
If I change parameter "servicename" to "name" it works for me.
|
28
  1. Add project installer to your service
  2. Add method to get CustomService name

    private void RetrieveServiceName() 
    {
        var serviceName = Context.Parameters["servicename"];
        if (!string.IsNullOrEmpty(serviceName))
        {
            this.SomeService.ServiceName = serviceName;
            this.SomeService.DisplayName = serviceName;
        }
    }
    
  3. call on install and uninstall

    public override void Install(System.Collections.IDictionary stateSaver)
    {
       RetrieveServiceName();
      base.Install(stateSaver);
    }
    
    
    public override void Uninstall(System.Collections.IDictionary savedState)
    
    {
       RetrieveServiceName();
       base.Uninstall(savedState);
    }
    
  4. installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

Source

2 Comments

This was very useful, I did have to recompile my service executable to get it work once I added this code, that wasn't an issue for me.
taken from the source link but incase... You need to change the “someService” to the ServiceInstaller name. It’s probably ServiceInstaller1 as that is the default.
7

enter image description here

This exactly worked for me!

I hope someone can use this.

2 Comments

image is broken
dedicated to copy paste: sc create FooService_v666 binPath= "c:\path\foo\bar.exe" DisplayName= "foo bar service" start= auto
2

Try installing your service with sc.exe. A quick search will yield lots documentation. With that tool it's easy to modify existing services and/or add new ones -- including names.

Edit: I install my .NET services with this tool.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.