简体   繁体   English

将C ++程序转换为Windows服务?

[英]Convert a C++ program to a Windows service?

I've written a console program that "does stuff" - mainly using boost. 我编写了一个控制台程序,可以“完成任务”-主要是使用boost。 How do I convert it to a Windows Service? 如何将其转换为Windows服务? What should I know about Windows Services beforehand? 我应该对Windows服务有什么了解?

There's a good example on how to set up a minimal service on MSDN . 有一个很好的示例,说明了如何在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. 构建并运行Windows服务之后,您将发现下一个主要的难题:调试很麻烦。 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. 没有终端(因此没有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. 我发现非常有用的一个技巧是在您的应用程序中添加-foreground选项,这样,如果您使用该标志运行,那么它将绕过服务启动程序代码,而像常规控制台应用程序一样运行,这使得调试变得非常容易。 In VS.Net set up the debugging options to invoke with that flag. 在VS.Net中,设置调试选项以使用该标志调用。

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. 这是一个具有自我安装/卸载功能的样板C ++服务项目,并将服务的启动和停止事件记录到Windows事件日志中。 It can be stopped and started through the services app (snapin) like other services. 可以像其他服务一样通过服务应用程序(snapin)停止和启动它。 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. 您可能最初希望为其授予LocalSystem权限以使其正常运行,因为至少在xp上,它没有足够的权限才能从项目提供的LocalService权限开始。 The Visual Studio 2008 project otherwise runs out of the box despite the downloaded instructions implying otherwise. 尽管已下载的说明另有说明,但Visual Studio 2008项目在其他情况下仍是开箱即用的。

A bit late but I hope this helps someone else. 有点晚了,但我希望这对其他人有帮助。

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. 值得一看。

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 Service项目,然后将您的代码复制到新项目中。

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(在本例中为控制台)与逻辑分开,则可以创建一个可以完成工作的库,然后从Console项目和Service Project中进行调用。

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

Srvany allows only one service at same time. Srvany一次仅允许一项服务。 So I write my srvany (sFany) to make nginx and php-cgi run as windows service together. 因此,我编写了srvany(sFany),使nginx和php-cgi一起作为Windows服务运行。 Here is the source https://github.com/stxh/sFany 这是源https://github.com/stxh/sFany

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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