简体   繁体   中英

Convert a C++ program to a Windows service?

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?

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.

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.

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

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

Worth a look.

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.

您可以配置一个应用作为服务通过使用SRVANY工具,这是一部分运行在Windows Server 2003 Resource Kit工具。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM