tags:

views:

631

answers:

4

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 ?

Cheers!

+2  A: 

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

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

Worth a look.

Rob
+1  A: 

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
Rather than cut and paste load your working code (- the UI) into the service as a library
Patrick
+1  A: 

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
+2  A: 

There's a good example on how to set up a minimal service on MSDN: http://msdn.microsoft.com/en-us/library/ms685967%28VS.85%29.aspx . 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.

Mand.

the_mandrill
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
do I add the -foreground to the command line?
Maciek
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