17

I've written a console program that "does stuff" - mainly using boost. How do I convert it to a Windows Service? What should I know about Windows Services beforehand?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Maciek
  • 19,435
  • 18
  • 63
  • 87

6 Answers6

20

There's a good example on how to set up a minimal service on MSDN. See the parts about writing the main function, entry point and also the example code.

Once you've got a windows service built and running, you'll discover the next major gotcha: it's a pain to debug. There's no terminal (and hence no stdout/stderr) and as soon as you try to run the executable it actually launches the service then returns to you.

One trick I've found very useful is to add a -foreground option to your app so that if you run with that flag then it bypasses the service starter code and instead runs like a regular console app, which makes it vastly easier to debug. In VS.Net set up the debugging options to invoke with that flag.

isanae
  • 3,253
  • 1
  • 22
  • 47
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • 1
    There are some useful tips for debugging here (especially the use of gflags to automatically launch the debugger): http://support.microsoft.com/kb/824344 – the_mandrill Oct 12 '09 at 12:50
  • Yes, if you run without a flag then the service gets installed and run (this is the way that services need to work). If you run with the -foreground option then it bypasses the service installation step. – the_mandrill Oct 13 '09 at 08:47
  • For logging, instead of writing to standard output, you can use an logging library like http://log4cplus.sourceforge.net/ or http://log4cpp.sourceforge.net/. – Coyote21 Feb 16 '12 at 16:26
4

There's a really good example on msdn here

It's a boiler plate C++ service project that has self install/uninstall functionality and logs service start and stop events to the windows event log. It can be stopped and started through the services app (snapin) like other services. You may want to initially give it LocalSystem rights to see it working , as on xp at least it doesn't have enough rights to start with the project provided rights of LocalService. The Visual Studio 2008 project otherwise runs out of the box despite the downloaded instructions implying otherwise.

A bit late but I hope this helps someone else.

Rich
  • 4,572
  • 3
  • 25
  • 31
2

You might be able to 'wrap it' using this tool from CodeProject:

http://www.codeproject.com/KB/system/xyntservice.aspx

Worth a look.

Rob
  • 76,700
  • 56
  • 158
  • 197
2

The simplest solution might be to create a new Windows Service project in Visual Studio and copy across your code to the new project.

If you refactor your code so that you've split the UI (in this case the console) from the logic you could create a library that does the work and then call that from both the Console project and the Service Project.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
1

You can configure an application to run as a service by using the Srvany tool, which is a part of the Windows Server 2003 Resource Kit Tools.

Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
0

Srvany allows only one service at same time. So I write my srvany (sFany) to make nginx and php-cgi run as windows service together. Here is the source https://github.com/stxh/sFany

StXh
  • 1,856
  • 1
  • 13
  • 16