Convert a C++ program to a Windows service?

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.

Leave a Comment