c++中的windows服务 [英] windows service in c++

查看:46
本文介绍了c++中的windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 C++ 制作一个定期运行的应用程序.很像windows服务.是否可以在 C++ 中创建 Windows 服务.如果是如何?

I want to make an application in c++ which will run periodically. much like an windows service. Is it possible to create a windows service in C++. If yes how?

有这方面的教程吗?

如何为 Windows 服务创建安装程序?

How can I create an installer for the Windows service?

推荐答案

如另一个答案所示,您需要创建一个普通的 C++ 程序(以 main 开头),并调用 StartServiceCtrlDispatcher Win32 API 告诉 Windows 您希望将此程序作为服务运行.关于此函数的一些信息可以在 MSDN 中找到.

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. Some information on this function can be found in MSDN.

此外,您需要一种机制来安装您的服务,以便它出现在 Windows 服务控制面板中.当然,还应该提供卸载功能.通常,所有三个功能(即安装、卸载和运行)都在单个可执行文件中处理.这三者的区别是通过命令行参数来表示的.

Additionally, you'll need a mechanism to install your service so that it appears in Windows Service Control Panel. And of course, an uninstall feature should also be provided. Typically, all the three functions (i.e., install, uninstall and run) are handled in a single executable. The difference in these three is indicated via command line parameters.

您没有说明您使用的是哪个版本的 VC++.如果您要使用托管 .NET 代码,那么这很容易,因为 Windows 服务模板已预先构建到 Visual Studio 中.

You've not indicated what version of VC++ you are using. If you are going with managed .NET code, then it's quite easy as Windows Service template is prebuilt into Visual Studio.

但是,如果您要使用 VC++ 6(或 100% 非托管代码),那么您需要了解很多底层细节.进一步的选择取决于您对 ATL 或 MFC 的了解(如果您使用的是旧版本的编译器/运行时).

However, if you are going with VC++ 6 (or 100% unmanaged code), then you need to know a lot of low level details. Further choices depend on your know-how of ATL or MFC (if you are going with older versions of the compiler/ runtime).

这可能是一个关于使用 C++ 创建 Windows 服务的很好的教程.

This might be a good tutorial on creating Windows Services using C++.

这篇关于c++中的windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆