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

2021-12-18 00:00:00 winapi c++

我写了一个控制台程序来做事"――主要是使用boost.如何将其转换为 Windows 服务?我应该事先了解有关 Windows 服务的哪些信息?

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?

推荐答案

有一个关于如何在 MSDN 上设置最小服务的好例子.请参阅有关编写主函数、入口点和示例代码的部分.

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.

一旦您构建并运行了 Windows 服务,您就会发现下一个主要问题:调试很痛苦.没有终端(因此没有 stdout/stderr),一旦您尝试运行可执行文件,它就会实际启动服务,然后返回给您.

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.

我发现一个非常有用的技巧是向您的应用程序添加一个 -foreground 选项,这样如果您使用该标志运行,它就会绕过服务启动器代码,而是像常规控制台一样运行应用程序,这使得调试变得非常容易.在 VS.Net 中设置调试选项以使用该标志调用.

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.

相关文章