78

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?

Nathan
  • 10,593
  • 10
  • 63
  • 87

5 Answers5

117

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

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Josh Yeager
  • 3,763
  • 1
  • 25
  • 29
  • 1
    This looks like exactly what I want -- however I can't get it to work. I just keep getting a "usage" message. – Nathan Nov 10 '09 at 15:42
  • 35
    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". – Nathan Nov 10 '09 at 15:56
  • Ah, I forgot about that. Sorry for giving you a bad example. – Josh Yeager Nov 11 '09 at 19:47
  • 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" – John Gilmer Sep 08 '19 at 15:35
  • When running from `PowerShell` window, make sure to use `sc.exe` instead of `sc`, otherwise `PowerShell` thinks you mean `Set-Content`. – Aage Oct 19 '22 at 09:46
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>"
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • 6
    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 – PUG Dec 15 '14 at 22:50
  • 4
    Does not work. Gives an error that my service already exists. – Jason Kelley Sep 01 '15 at 19:42
  • 3
    Works if you have a project installer and override the install and uninstall like in @Volodymyrs answer http://stackoverflow.com/a/25259719/169714 – JP Hellemons Jul 05 '16 at 10:22
  • does not work on a default installer created from Visual studio – Gelootn May 11 '17 at 12:31
  • If I change parameter "servicename" to "name" it works for me. – dbd Sep 21 '18 at 21:29
  • Received this Error: An exception occurred during the Install phase. System.ComponentModel.Win32Exception: The specified service already exists Any way around? – Ayush Singhania May 03 '19 at 10:45
26
  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

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • 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. – Terry Kernan Jul 08 '16 at 11:20
  • 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. – J3RM Feb 02 '21 at 17:05
7

enter image description here

This exactly worked for me!

I hope someone can use this.

kulNinja
  • 526
  • 6
  • 14
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.

jsw
  • 1,752
  • 1
  • 14
  • 20