DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务。Debug、Release版本的注册和卸载,及其原理服务运行、停止流程浅析分别介绍了Windows服务的注册、卸载、运行、停止的简单流程。那么今天就来介绍一下如何使用开源库Boost.Application来快速构建一个Windows服务。

Boost.Application简介

Boost.Application 是一个开源的C++库,主要用于构建跨平台的服务,包括Windows、Unix、Linux、MaxOS等。没错,这个库可以用来做跨平台的服务。Boost.Application 使用开源协议 Boost Software License, Version 1.0

Boost.Application 目前还不是Boost官方的库,想要使用它,需要到Github上下载最新代码,当前版本为0.4.12。

用Boost.Application构建Windows服务

第一步,用VS创建一个控制台程序。

控制台程序

第二步,配置Boost.Application以及Boost的include路径和lib路径。

第三步,将Boost.Application目录下的README.md文件中的示例代码拷贝到 myservice.cpp 中并覆盖原来所有的代码。

第四部,将编译选项设置为多字节

多字节

第五步,编译并运行程序

运行程序

由于这时候还没有注册服务,所以以控制台的方式运行起来就会报错了。

第六步,将程序注册为Windows服务,在命令行中执行如下命令:

sc create myservice binpath= "D:\Code\C++\CommonCode\Win32\Debug\myservice.exe" type= own start= demand displayname= "service test"

sc命令的详细使用方法可以参考玩转Windows服务系列——命令行管理Windows服务

在Windows服务管理器中可以看到刚刚创建的服务:

myservice

第七步,启动服务,然后停止服务,在服务所在目录,可以看到产生了一个log.txt文件,

复制代码
复制代码
Start Log...
-----------------------------
---------- Arg List ---------
-----------------------------
myservice
-----------------------------
0, running...
1, running...
2, running...
Stoping my application...
复制代码
复制代码

这样,一个完整的Windows服务就算完成了。

注册服务程序

在Boost.Application 的 “Boost.Application\example\setup”目录下,有一个用于注册卸载服务的示例代码。

将此示例代码编译为service_setup_ex.exe程序后,就可以使用此程序进行服务的注册和卸载了。

安装服务,支持设置程序路径、服务名字、显示名字、描述、启动模式、依赖服务等,如下:

service_setup_ex.exe -i --name="My Service" --path="c:\myservice\service.exe"
service_setup_ex.exe -i --name="My Service" --path="c:\myservice\service.exe" --display="My Service"
service_setup_ex.exe -i --name="My Service" --path="c:\myservice\service.exe" --display="My Service" --description "Service Description"
service_setup_ex.exe -i --name="My Service" --path="c:\myservice\service.exe" --user=".\Renato Tegon Forti" --pass="x1x1x1"
service_setup_ex.exe -i --name="My Service" --path="c:\myservice\service.exe" --start="manaul" --depends="service1\service2\service3"

检测服务:

service_setup_ex.exe -c --name="My Service"

卸载服务:

service_setup_ex.exe -u --name="My Service" --path="c:\myservice\service.exe"

参考资料

Boost.Application

Build a Server Application using Application Library

系列链接

玩转Windows服务系列——创建Windows服务

玩转Windows服务系列——Debug、Release版本的注册和卸载,及其原理

玩转Windows服务系列——无COM接口Windows服务启动失败原因及解决方案

玩转Windows服务系列——服务运行、停止流程浅析

玩转Windows服务系列——Windows服务小技巧

玩转Windows服务系列——命令行管理Windows服务

玩转Windows服务系列——Windows服务启动超时时间

玩转Windows服务系列——使用Boost.Application快速构建Windows服务

玩转Windows服务系列——给Windows服务添加COM接口

 

from:https://www.cnblogs.com/hbccdf/p/build_windows_service_using_boost_application_library.html

posted on   DoubleLi  阅读(440)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2017-07-11 C++11中的小细节--字符串的原始字面量
2017-07-11 CPPCMS库在Windows下的使用
2017-07-11 How to build CppCMS 1.x.x
2017-07-11 error LNK2019: 无法解析的外部符号 _deflate
2017-07-11 CppCMS1.0.3 Build by VS2012
2013-07-11 MFC DestroyWindow窗口对象和窗口句柄的销毁
2012-07-11 VC++中的各种文件的作用
点击右上角即可分享
微信分享提示