简体   繁体   English

C ++中的Windows服务

[英]windows service in c++

I want to make an application in c++ which will run periodically. 我想制作一个将定期运行的c ++应用程序。 much like an windows service. 很像Windows服务。 Is it possible to create a windows service in C++. 是否可以在C ++中创建Windows服务。 If yes how? 如果是,怎么办?

Any tutorial for this? 任何教程吗?

EDIT: How can I create an installer for the Windows service? 编辑:如何为Windows服务创建安装程序?

C++ is compatible enough with C that the traditional approaches work. C ++与C足够兼容,因此传统方法可以工作。 You build the application as a normal C++ program (starting with main ); 您将应用程序构建为普通的C ++程序(从main开头); in the startup you call StartServiceCtrlDispatcher to inform the OS that your program can run as a service. 在启动过程中,您调用StartServiceCtrlDispatcher来通知OS您的程序可以作为服务运行。 Windows doesn't care that you use classes internally. Windows不在乎您在内部使用类。

As indicated in another answer, you need to create a normal C++ program (starting with main ), and call StartServiceCtrlDispatcher Win32 API to tell Windows that you want to run this program as a service. 如另一个答案所示,您需要创建一个普通的C ++程序(以main开头),并调用StartServiceCtrlDispatcher Win32 API来告知Windows您希望将该程序作为服务运行。 Some information on this function can be found in MSDN . 在MSDN中可以找到有关此功能的一些信息。

Additionally, you'll need a mechanism to install your service so that it appears in Windows Service Control Panel. 此外,您将需要一种机制来安装服务,以使其显示在Windows服务控制面板中。 And of course, an uninstall feature should also be provided. 当然,还应该提供卸载功能。 Typically, all the three functions (ie, install, uninstall and run) are handled in a single executable. 通常,所有三个功能(即安装,卸载和运行)都在单个可执行文件中处理。 The difference in these three is indicated via command line parameters. 这三个方面的差异通过命令行参数指示。

You've not indicated what version of VC++ you are using. 您尚未指出要使用的VC ++版本。 If you are going with managed .NET code, then it's quite easy as Windows Service template is prebuilt into Visual Studio. 如果使用托管的.NET代码,则很容易,因为Windows Service模板已预先内置到Visual Studio中。

However, if you are going with VC++ 6 (or 100% unmanaged code), then you need to know a lot of low level details. 但是,如果要使用VC ++ 6(或100%非托管代码),则需要了解许多底层细节。 Further choices depend on your know-how of ATL or MFC (if you are going with older versions of the compiler/ runtime). 其他选择取决于您对ATL或MFC的了解(如果要使用较旧版本的编译器/运行时)。

This might be a good tutorial on creating Windows Services using C++ . 这可能是有关使用C ++创建Windows服务的很好的教程。

Check out POCO C++ libraries that allow you to write daemons/services with little effort. 查看POCO C ++库,该库使您可以轻松编写守护程序/服务。

http://pocoproject.org/docs/00100-GuidedTour.html http://pocoproject.org/docs/00100-GuidedTour.html

http://pocoproject.org/slides/190-Applications.pdf http://pocoproject.org/slides/190-Applications.pdf

maybe I am late, but I write C++ class for windows services creating and want share code you need inherit your class from my base class cWinService http://sprogram.com.ua/en/articles/how-write-service-for-windows-with-cpp 也许我来晚了,但是我为Windows服务创建C ++类,并且想要共享代码,您需要从我的基类cWinService中继承您的类http://sprogram.com.ua/en/articles/how-write-service-for- Windows与CPP

please contact with me if you have any observation. 如果有任何发现,请与我联系。 I am not C++ wizard I am just study ;) 我不是C ++向导,我只是研究;)

Of course it's possible. 当然可以。 Not only possible, but that's what the native API is designed for (well, C really, but close enough). 不仅可能,而且这就是本机API设计的目的(确实,C,但足够接近)。

See Jeff Richter's book "Programming Server side Applications for Windows 2000" 请参阅Jeff Richter的书“为Windows 2000编程服务器端应用程序”

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

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