当前位置: 首页 > article >正文

Rust编写Windows服务

文章目录

  • Rust编写Windows服务
    • 一:Windows服务程序大致原理
    • 二:Rust中编写windows服务
    • 三:具体实例

Rust编写Windows服务

编写Windows服务可选语言很多, 其中C#最简单。本着练手Rust语言,尝试用Rust编写一个服务。

一:Windows服务程序大致原理

参考官网C/C++创建服务流程
https://learn.microsoft.com/zh-cn/windows/win32/services/service-program-tasks

  • 编写服务程序的main函数
    1. main函数中,填充数据结构 DispatchTable: [[服务名,ServiceMain函数], [NULL, NULL]]
    2. 调用StartServiceCtrlDispatcher( DispatchTable )
int __cdecl _tmain(int argc, TCHAR *argv[])
{ 
    SERVICE_TABLE_ENTRY DispatchTable[] = 
    { 
        { SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain }, 
        { NULL, NULL } 
    }; 
    if (!StartServiceCtrlDispatcher( DispatchTable )) 
    { 
        SvcReportEvent(TEXT("StartServiceCtrlDispatcher")); 
    } 
} 

  • 编写 ServiceMain 函数
    1. 注册控制处理函数 RegisterServiceCtrlHandler(SvcCtrlHandler )
    2. 设置服务状态,SERVICE_START_PENDING
    3. 做一些预备工作,比如初始化日志/注册事件等
    4. 设置服务状态,SERVICE_START_RUNNING
    5. 开始loop处理我们自己的代码(loop循环中可以接受3中注册的事件,当通知停止时退出循环)
  • 编写控件处理程序函数
    1. 接受事件管理器发送的消息并处理,比如收到SERVICE_CONTROL_STOP时,使用3中注册的事件句柄发送停止事件

二:Rust中编写windows服务

借用第三方库 windows-service = "0.7.0"
https://crates.io/crates/windows-service
参考windows-service中ping-service示例提取了一个模板,只有替换编写两处/* */代码

use std::{
    ffi::OsString,
    sync::mpsc,
    time::Duration,
};
use windows_service::{
    define_windows_service,
    service::{
        ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
        ServiceType,
    },
    service_control_handler::{self, ServiceControlHandlerResult},
    service_dispatcher,
};

static SERVICE_NAME: &str = "Rust Demo Service";

fn main() -> Result<(), windows_service::Error> {
    service_dispatcher::start(SERVICE_NAME, ffi_service_main)?;
    Ok(())
}


define_windows_service!(ffi_service_main, my_service_main);

fn my_service_main(arguments: Vec<OsString>) {
    let _ = arguments;
        
    /*
    	这里服务还没开始, 可以填写初始化日志文件等操作
    */
    
    let (shutdown_tx, shutdown_rx) = mpsc::channel();

    // 对应SvcCtrlHandler
    let _event_handler = move |control_event| -> ServiceControlHandlerResult {
        match control_event {
            ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,

            // 处理停止事件
            ServiceControl::Stop => {
                shutdown_tx.send(()).unwrap();
                ServiceControlHandlerResult::NoError
            }

            // 其他用户事件都当作停止事件处理
            ServiceControl::UserEvent(code) => {
                if code.to_raw() == 130 {
                    shutdown_tx.send(()).unwrap();
                }
                ServiceControlHandlerResult::NoError
            }

            _ => ServiceControlHandlerResult::NotImplemented,
        }
    };

    let status_handle = service_control_handler::register(SERVICE_NAME, _event_handler);
    let status_handle = status_handle.unwrap();
    let _ = status_handle.set_service_status(ServiceStatus {
        service_type: ServiceType::OWN_PROCESS,
        current_state: ServiceState::Running,
        controls_accepted: ServiceControlAccept::STOP,
        exit_code: ServiceExitCode::Win32(0),
        checkpoint: 0,
        wait_hint: Duration::default(),
        process_id: None,
    });

    loop {
    	/*
			这里写自己的代码逻辑,下面时处理一次循环后睡眠5秒,若是接受到停止等消息退出循环
		*/
        match shutdown_rx.recv_timeout(Duration::from_secs(5)) {
            Ok(_) | Err(mpsc::RecvTimeoutError::Disconnected) => break,
            Err(mpsc::RecvTimeoutError::Timeout) => (),
        }
    }

    let _ = status_handle.set_service_status(ServiceStatus {
        service_type: ServiceType::OWN_PROCESS,
        current_state: ServiceState::Stopped,
        controls_accepted: ServiceControlAccept::empty(),
        exit_code: ServiceExitCode::Win32(0),
        checkpoint: 0,
        wait_hint: Duration::default(),
        process_id: None,
    });
}

三:具体实例

笔记本策略经常恢复到合上盖子睡眠功能,写个小服务定时设置合上盖子不做任何操作
逻辑比较简单,定时调用WinAPI函数CallNtPowerInformation获取配置信息,不符合当前设置执行修改
完整如下

use std::{
    ffi::{c_void, OsString},
    sync::mpsc,
    time::Duration,
};
use windows::Win32::{Foundation::STATUS_SUCCESS, System::Power::{CallNtPowerInformation, POWER_INFORMATION_LEVEL, SYSTEM_POWER_POLICY}};
use windows_service::{
    define_windows_service,
    service::{
        ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
        ServiceType,
    },
    service_control_handler::{self, ServiceControlHandlerResult},
    service_dispatcher,
};

static SERVICE_NAME: &str = "Power Lid Service";

fn main() -> Result<(), windows_service::Error> {
    service_dispatcher::start(SERVICE_NAME, ffi_service_main)?;
    Ok(())
}


define_windows_service!(ffi_service_main, my_service_main);

fn my_service_main(arguments: Vec<OsString>) {
    let _ = arguments;
    let (shutdown_tx, shutdown_rx) = mpsc::channel();

    let _event_handler = move |control_event| -> ServiceControlHandlerResult {
        match control_event {
            ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,

            ServiceControl::Stop => {
                shutdown_tx.send(()).unwrap();
                ServiceControlHandlerResult::NoError
            }

            ServiceControl::UserEvent(code) => {
                if code.to_raw() == 130 {
                    shutdown_tx.send(()).unwrap();
                }
                ServiceControlHandlerResult::NoError
            }

            _ => ServiceControlHandlerResult::NotImplemented,
        }
    };

    let status_handle = service_control_handler::register(SERVICE_NAME, _event_handler);
    let status_handle = status_handle.unwrap();
    let _ = status_handle.set_service_status(ServiceStatus {
        service_type: ServiceType::OWN_PROCESS,
        current_state: ServiceState::Running,
        controls_accepted: ServiceControlAccept::STOP,
        exit_code: ServiceExitCode::Win32(0),
        checkpoint: 0,
        wait_hint: Duration::default(),
        process_id: None,
    });

    loop {
        unsafe {
            let mut a: SYSTEM_POWER_POLICY = std::mem::zeroed();
            let status = CallNtPowerInformation(
                POWER_INFORMATION_LEVEL { 0: 8 },
                None,
                0,
                Some(&mut a as *mut SYSTEM_POWER_POLICY as *mut c_void),
                size_of::<SYSTEM_POWER_POLICY>() as u32,
            );
            if status != STATUS_SUCCESS {
                println!("获取电源状态失败: {:x} !", status.0);
                return;
            }
            if a.LidClose.Action.0 == 0 {
                println!("状态已为0, 忽略");
                return;
            } else {
                println!("状态为{:x}", a.LidClose.Action.0);
                a.LidClose.Action.0 = 0;
                let status = CallNtPowerInformation(
                    POWER_INFORMATION_LEVEL { 0: 0 },
                    Some(&mut a as *mut SYSTEM_POWER_POLICY as *mut c_void),
                    size_of::<SYSTEM_POWER_POLICY>() as u32,
                    None,
                    0,
                );
                if status != STATUS_SUCCESS {
                    println!("设置ac电源状态失败: {:x} !", status.0);
                    return;
                } else {
                    println!("设置AC电源状态成功");
                }
                let status = CallNtPowerInformation(
                    POWER_INFORMATION_LEVEL { 0: 1 },
                    Some(&mut a as *mut SYSTEM_POWER_POLICY as *mut c_void),
                    size_of::<SYSTEM_POWER_POLICY>() as u32,
                    None,
                    0,
                );
                if status != STATUS_SUCCESS {
                    println!("设置dc电源状态失败: {:x} !", status.0);
                    return;
                } else {
                    println!("设置DC电源状态成功");
                }
            }
        }
        match shutdown_rx.recv_timeout(Duration::from_secs(5)) {
            Ok(_) | Err(mpsc::RecvTimeoutError::Disconnected) => break,
            Err(mpsc::RecvTimeoutError::Timeout) => (),
        }
    }

    let _ = status_handle.set_service_status(ServiceStatus {
        service_type: ServiceType::OWN_PROCESS,
        current_state: ServiceState::Stopped,
        controls_accepted: ServiceControlAccept::empty(),
        exit_code: ServiceExitCode::Win32(0),
        checkpoint: 0,
        wait_hint: Duration::default(),
        process_id: None,
    });
}



http://www.kler.cn/a/305467.html

相关文章:

  • dapp获取钱包地址,及签名
  • AI生活之我用AI处理Excel表格
  • 给查询业务添加redis缓存和缓存更新策略
  • Node.Js+Knex+MySQL增删改查的简单示例(Typescript)
  • 招聘app开发,人才招聘、求职首要方式
  • 晨控RFID技术助力半导体制造业革新之路
  • 从“天宫课堂”到人工智能:中国少儿编程的未来在哪里?
  • golang学习笔记12——Go 语言内存管理详解
  • 软件测试工程师面试整理-数据库与SQL
  • 微信小程序使用 ==== 粘性布局
  • Ubuntu 常用指令和作用解析
  • 16. MyBatis的延迟加载机制是什么?如何配置?有哪些优缺点?
  • 股票程序化交易赚钱吗,证监会如何监测股票多账户关联交易
  • Vue 创建自定义指令 以及创建自定义指令相关属性说明
  • DFS 算法:洛谷B3625迷宫寻路
  • Redis相关命令详解
  • Understanding the model of openAI 5 (1024 unit LSTM reinforcement learning)
  • WSL安装Redis
  • 【linux】 cd命令
  • 代码随想录算法训练营第62天| 图论 Floyd算法 A*算法
  • 鸿蒙 NEXT 生态应用核心技术理念:可分可合,自由流转
  • 开源 AI 智能名片 S2B2C 商城小程序相关角色的探索
  • 基于vue框架的宠物爱好者交流网站的设计与实现p2653(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
  • 黑马点评19——多级缓存-缓存同步
  • 基于SSM的银发在线教育云平台的设计与实现
  • Qt事件处理机制