Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a C++ program to a Windows service?

Tags:

c++

winapi

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?

like image 968
Maciek Avatar asked Oct 12 '09 11:10

Maciek


People also ask

Can you create Windows Service using C#?

Let's create a Windows Service in C# using Visual Studio. Open Visual Studio, go to File > New and select Project. Now select a new project from the Dialog box and select “Window Service” and click on the OK button.


1 Answers

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.

like image 146
the_mandrill Avatar answered Oct 04 '22 23:10

the_mandrill