将 C++ 程序转换为 Windows 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1554047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a C++ program to a Windows service?
提问by Maciek
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?
我写了一个“做事”的控制台程序——主要是使用boost。如何将其转换为 Windows 服务?我应该事先了解有关 Windows 服务的哪些信息?
回答by the_mandrill
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.
关于如何在 MSDN 上设置最小服务有一个很好的例子。请参阅有关编写主函数、入口点以及示例代码的部分。
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.
一旦您构建并运行了 Windows 服务,您就会发现下一个主要问题:调试很痛苦。没有终端(因此没有 stdout/stderr),一旦您尝试运行可执行文件,它就会实际启动服务,然后返回给您。
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.
我发现一个非常有用的技巧是向-foreground
您的应用程序添加一个选项,这样如果您使用该标志运行,它就会绕过服务启动器代码,而是像常规控制台应用程序一样运行,这使得调试变得更加容易。在 VS.Net 中设置调试选项以使用该标志进行调用。
回答by Rich
There's a really good example on msdn here
有一个很好的例子在MSDN上这里
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.
它是一个样板 C++ 服务项目,具有自安装/卸载功能并将服务启动和停止事件记录到 Windows 事件日志中。它可以像其他服务一样通过服务应用程序 (snapin) 停止和启动。您可能希望最初授予它 LocalSystem 权限以查看它的工作,因为在 xp 上至少它没有足够的权限从项目提供的 LocalService 权限开始。Visual Studio 2008 项目在其他情况下开箱即用,尽管下载的说明暗示其他情况。
A bit late but I hope this helps someone else.
有点晚了,但我希望这对其他人有所帮助。
回答by Rob
You might be able to 'wrap it' using this tool from CodeProject:
您可以使用 CodeProject 中的此工具“包装”它:
http://www.codeproject.com/KB/system/xyntservice.aspx
http://www.codeproject.com/KB/system/xyntservice.aspx
Worth a look.
值得一看。
回答by ChrisF
The simplest solution might be to create a new Windows Service project in Visual Studio and copy across your code to the new project.
最简单的解决方案可能是在 Visual Studio 中创建一个新的 Windows 服务项目,并将您的代码复制到新项目中。
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.
如果您重构代码以便将 UI(在本例中为控制台)与逻辑分开,您可以创建一个库来完成工作,然后从控制台项目和服务项目中调用它。
回答by Jacob Seleznev
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.
您可以使用 Srvany 工具将应用程序配置为作为服务运行,该工具是Windows Server 2003 资源工具包工具的一部分。
回答by StXh
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
Srvany 只允许同时提供一项服务。所以我写了我的srvany(sFany)让nginx和php-cgi一起作为windows服务运行。这里是源https://github.com/stxh/sFany