問題描述
我寫了一個控制臺程序來做事"——主要是使用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.
這篇關于將 C++ 程序轉換為 Windows 服務?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!