4

According to How to Create a User-Defined Service you can create a Windows service using Windows NT Resource Kit. For example as shown in above link:

C:\Program Files\Resource Kit\Instsrv.exe Notepad C:\Program Files\Resource Kit\Srvany.exe

But this does not work in Windows 7 or 10 and 64bit platform for that matter because Windows NT Resource Kit is not available for newer Windows versions. I was able to create the Windows service using this batch file:

sc create "MyService" binPath= "C:\Program Files (x86)\MyProg\myprog.exe" start= auto DisplayName= "My Service" obj= LocalSystem

But it does not start the Windows service. When I try to start I get this message:

error message dialog

Windows could not start the OrchestrateIT Import Service service on Local Computer.

Error 1053: The service did not respond to the start or control request in a timely fashion.

The information in Install a Windows service using a Windows command prompt? and Create Windows service from executable does not work.

How to start the Windows service successfully?

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Codename K
  • 890
  • 4
  • 23
  • 52
  • 1
    Paths which include spaces should be enclosed with doublequotes. – Compo Sep 05 '18 at 07:34
  • It does have doublequotes and it works if you have Windows NT Resource Kit. But this is not available in Windows 7 and Windows 10. – Codename K Sep 05 '18 at 07:58
  • 1
    Codename K, unless my device has an issue, the command you have shown as not working definitely doesn't have any doublequotes! – Compo Sep 05 '18 at 10:07
  • 1
    The command that I have shown, the "Instsrv.exe" is from the Windows NT Resource Kit. This is how the Windows service is installed previously. But Windows NT Resource Kit ("Instsrv.exe") is not available in Windows 7 and Windows 10. The question is how to create services in Windows 7 or Windows 10? Are there any alternate methods to this? – Codename K Sep 05 '18 at 10:27
  • 1
    Your question is therefore not about a specific issue with your code, but about software and off topic for this particular site. You should instead have asked your question on [Super User](https://superuser.com/questions/ask). – Compo Sep 05 '18 at 12:17

1 Answers1

5

Srvany.exe is an ancient program and it will work, to my best knowledge, till Windows 2003. You can just forget it existed for Windows 7/10.

To start/stop a service on Windows 7/10 (I'm using Acronis service) with the service name which is usually quite different from the display name:

Stopping running service:

sc stop syncagentsrv

SERVICE_NAME: syncagentsrv
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Starting running service:

sc start syncagentsrv

SERVICE_NAME: syncagentsrv
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 2240
        FLAGS              :

Check the statek of the service:

sc query syncagentsrv

SERVICE_NAME: syncagentsrv
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

To create the service you want to:

sc create "MyService" binPath="C:\Program Files (x86)\MyProg\myprog.exe" start=auto DisplayName="My Service"

This only creates the service and adds record into the Registry/serviceDB. You need to check the exe file itself.

You don't need obj=LocalSystem as this is default option. Don't create spaces between the equal sign = and the values!

Did you check event viewer? What does it say when you start the service? Only the timeout? Does the myprog.exe work alone without being a service? Please answer these questions and I'll edit the answer.

Edit 1 & 2 based on comments and should you have any difficulties with the above approach

There is a small utility called SrvStart (originally from Nick Rozanski), but it has been now adapted for VS 2017 (get srvstart.v120.zip which needs to be build). If you are happy with the older version and already provided exe files download the srvstart_run.v110.zip and copy both *.exe files and *.dll into the directory as indicated below.

  1. Copy all the files into C:\Windows (yes, your Windows directory). The reason being that the C:\Windows should be always in your PATH thus reachable by the SrvStart executable.

  2. Now create a MyProg.ini file:

    [MyService]
    startup="C:\Program Files (x86)\MyProg\myprog.exe"
    shutdown_method=winmessage
    

    winmessage forces to close any opened windows when you are shutting down the service.

  3. Then place the *.ini file directly into your root c:\.

  4. Then use a command:

    sc create <servicename> Displayname= "<servicename>" binpath= "srvstart.exe <servicename> -c <path to srvstart config file>" start= <starttype>
    

    In your case it would be:

    sc create "MyService" DisplayName="My Service" binPath="srvstart.exe MyService -c C:myprog.ini" start=auto 
    

    Note: There is no backslash (\) between C: and myprog.ini which is correct.

  5. Now check your services and you should see the "My Service" name there and it should behave like a service.

Edit 3 based on comments from @CodenameK

Apparently you need to compile the SrvStart 120 version with that is compilable with VS2017 to make it run correctly @Win10. The only version available is the old 1.1 version, which appears not to work correctly at Win10.

The solution that worked for CodenameK was to use NSSM - the Non-Sucking Service Manager. For future reference if anyone needs it.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
tukan
  • 17,050
  • 1
  • 20
  • 48
  • I checked the event viewer and it shows this error, _The My Service service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion._ – Codename K Sep 18 '18 at 16:45
  • 1
    @CodenameK yes that is natural, any other? Did you try to run the exe file manually? Does it run correctly? – tukan Sep 20 '18 at 08:13
  • 1
    @CodenameK do you have any log from the application confirming it? If it works when executed under that user then it is probably an issue of user rights. Try to create the service with credentials of that user where it works and not as a `LocalSystem` (did you try to run it under `LocalSystem`?) – tukan Sep 22 '18 at 09:31
  • It did not work under `LocalSystem`. I added the username and password and it still showed the same issue. My assumption is that it need to run as 'NT AUTHORITY\Network Service'. – Codename K Sep 22 '18 at 09:35
  • 1
    @CodenameK That is more question for the application programmer, how it should be run as service. It is very hard to guess this when I don't even know that application. For example, we are running one of our legacy applications with a "technical" administration account, which allows the application to have all necessary rights it needs. – tukan Sep 22 '18 at 09:38
  • In order to test, I set Notepad to run and got the same error message. – Codename K Sep 22 '18 at 09:41
  • 1
    @CodenameK Ok, I'll edit the answer and show you one more way to do it. – tukan Sep 22 '18 at 09:51
  • 1
    @CodenameK you can try the approach written in edit. – tukan Sep 22 '18 at 10:10
  • The `srvstart.v120.zip` does not contain the `srvstart.exe`. It seems to have only the source code. Does this mean you need VS 2017 to build it? – Codename K Sep 22 '18 at 16:21
  • 1
    @CodenameK Ah, you are right. It has been long time I've used it. Please use the `srvstart_run.v110.zip` which contains the exe files and dll files. This is the 1.1 original version and should work for win7/10. If you want to use the VC 2017 version you need to build it from the `srvstart_v120.zip` (according to the author's statement they should be functionally equivalent). – tukan Sep 22 '18 at 18:12
  • 1
    I tried what you mentioned. But it produced the same error message. However, while searching for the `srvstart.exe`, I found another program called `nssm.exe` which solved the issue. Thanks for the help. I wouldn't have thought to search for `srvstart.exe` and found the `nssm.exe` if not for your answer. – Codename K Sep 23 '18 at 08:57
  • 1
    @CodenameK I'm happy that I could help at least in some way. – tukan Sep 23 '18 at 09:23
  • I added the +50 to you. – Codename K Sep 24 '18 at 11:19
  • 1
    @CodenameK yes, thank you. I have edited the post for future reference. – tukan Sep 24 '18 at 11:37