0

I want to create a windows service from a long running process. I'm able to create the service but I cannot execute it in any way.

I've also tries a very simple timeout.exe process with the same results.

> New-Service `
  -Name "test" `
  -BinaryPathName "C:\windows\system32\timeout.exe 30"

// output

Status   Name               DisplayName
------   ----               -----------
Stopped  test               test

After that, I'm trying to start service with

Start-Service test

And i receive IMMEDIATELY this output:

Start-Service: Service 'test (test)' cannot be started due to the following error: Cannot start service 'test' on computer '.'.

On event viewer are presente 2 event logs that are referencing a 30 seconds timeout that never happens: the system fails immediately.

enter image description here

What I am missing ?

Claudio
  • 3,060
  • 10
  • 17
  • 1
    A service is a special type of application that's been built to interface with the windows service control manager, if you register a regular desktop executable as a service its not going to work. You may like to try using svrany: https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service – Alex K. Jun 24 '22 at 13:29

1 Answers1

1

I'm answering to my own question.

As @alex-k says a regular process cannot be started as service and must be special interface.

In this answer there are additional details https://stackoverflow.com/a/3582179/3120219

srvany.exe is a wrapper that implements the required interface and launch a process but can have some limitations.

Claudio
  • 3,060
  • 10
  • 17
  • Instead of using srvany.exe you should create a new project in your solution and select windows service. Take a look at the generated base code. You can combine this code with your application (either console or UI) and register your app as service with a command line parameter. When the app is started without the parameter run your normal application. When started with the parameter go the service route. That way you'll get a hybrid app. – Oliver Jun 28 '22 at 14:11