windows win32 c++/c services服务编写sample demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c5113620/article/details/84643658

windows的服务程序不是简单的写一个console控制台程序然后加入服务就能运行成功的,需要遵循一定的代码规范,程序需要与系统的 SCM 交互,否则就会刚执行就被系统停止

两个windows 服务程序的sample demo
the-complete-service-sample–Microsoft
使用 C++ 编写的基础 Windows 服务

可以对照以上使用windows api编写,以下简单总结windows服务程序

服务程序总体看就是一个console控制台程序,服务 install (CreateService,含使用GetModuleFileName获取的当前应用程序文件路径,可以加启动参数,路径有空格需要 引号 包起来) 到 SCM (service control manager database)后开始启动服务,每次服务开始启动,都会从main开始(无参数)执行,然后调用 StartServiceCtrlDispatcher 去开始找到服务entry point执行入口开始函数

所以一般都加个 在main中处理类似install参数,去安装或卸载服务。

服务程序是由SCM管理,main 里面调用 StartServiceCtrlDispatcher 之后,SCM 才能知道你对应的事件(Start, Stop, Pause, Continue, and Shutdown)的执行函数

猜你喜欢

转载自blog.csdn.net/c5113620/article/details/84643658