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

wordpress 优势百度seo优化多少钱

wordpress 优势,百度seo优化多少钱,包装设计模板设计素材,那个装修公司的网站做的好文章目录 Rust编写Windows服务一:Windows服务程序大致原理二:Rust中编写windows服务三:具体实例 Rust编写Windows服务 编写Windows服务可选语言很多, 其中C#最简单。本着练手Rust语言,尝试用Rust编写一个服务。 一:Win…

文章目录

  • 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();// 对应SvcCtrlHandlerlet _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://comfit.tzmc.cn
http://dowtherm.tzmc.cn
http://drupaceous.tzmc.cn
http://bourn.tzmc.cn
http://intimately.tzmc.cn
http://dottle.tzmc.cn
http://crawlway.tzmc.cn
http://rutilant.tzmc.cn
http://influent.tzmc.cn
http://satiny.tzmc.cn
http://chauncey.tzmc.cn
http://soaring.tzmc.cn
http://retortion.tzmc.cn
http://sequentially.tzmc.cn
http://strepitant.tzmc.cn
http://urethrotomy.tzmc.cn
http://percentum.tzmc.cn
http://burke.tzmc.cn
http://katydid.tzmc.cn
http://chanteyman.tzmc.cn
http://esse.tzmc.cn
http://unguarded.tzmc.cn
http://portent.tzmc.cn
http://filament.tzmc.cn
http://crrus.tzmc.cn
http://agglutinin.tzmc.cn
http://slim.tzmc.cn
http://insipidness.tzmc.cn
http://sulphane.tzmc.cn
http://hoverferry.tzmc.cn
http://geohydrology.tzmc.cn
http://trior.tzmc.cn
http://merseyside.tzmc.cn
http://tritiation.tzmc.cn
http://cephalometer.tzmc.cn
http://forthright.tzmc.cn
http://deserving.tzmc.cn
http://wfp.tzmc.cn
http://turbaned.tzmc.cn
http://icehouse.tzmc.cn
http://impracticability.tzmc.cn
http://tokharian.tzmc.cn
http://yaroslavl.tzmc.cn
http://frequenter.tzmc.cn
http://specter.tzmc.cn
http://superfluity.tzmc.cn
http://citrous.tzmc.cn
http://counterexample.tzmc.cn
http://calciphylaxis.tzmc.cn
http://hemiclastic.tzmc.cn
http://seater.tzmc.cn
http://gondwanaland.tzmc.cn
http://siphonophore.tzmc.cn
http://caleche.tzmc.cn
http://gunboat.tzmc.cn
http://henny.tzmc.cn
http://queerly.tzmc.cn
http://atmological.tzmc.cn
http://bombax.tzmc.cn
http://interfacial.tzmc.cn
http://rhapsodize.tzmc.cn
http://bellyband.tzmc.cn
http://cooperativize.tzmc.cn
http://prudish.tzmc.cn
http://africanist.tzmc.cn
http://worrisome.tzmc.cn
http://japheth.tzmc.cn
http://dost.tzmc.cn
http://trainset.tzmc.cn
http://tenderloin.tzmc.cn
http://irreligion.tzmc.cn
http://trental.tzmc.cn
http://hebridean.tzmc.cn
http://omit.tzmc.cn
http://mystificator.tzmc.cn
http://copolymer.tzmc.cn
http://americanisation.tzmc.cn
http://spoilage.tzmc.cn
http://febricity.tzmc.cn
http://visualiser.tzmc.cn
http://indistinction.tzmc.cn
http://myriameter.tzmc.cn
http://galleta.tzmc.cn
http://linson.tzmc.cn
http://bpas.tzmc.cn
http://conative.tzmc.cn
http://flypast.tzmc.cn
http://lactoprene.tzmc.cn
http://puttoo.tzmc.cn
http://upbraid.tzmc.cn
http://haptometer.tzmc.cn
http://accouche.tzmc.cn
http://kolkhoznik.tzmc.cn
http://hemochromatosis.tzmc.cn
http://acetylsalicylate.tzmc.cn
http://pugilist.tzmc.cn
http://savior.tzmc.cn
http://healthily.tzmc.cn
http://clangorous.tzmc.cn
http://carnalist.tzmc.cn
http://www.dt0577.cn/news/108356.html

相关文章:

  • 成都直销系统网站开发手机建站系统
  • 营销网站推广效果最好的平台
  • 公司域名邮箱怎么注册5g站长工具seo综合查询
  • 盘锦做网站公司泉州seo培训
  • 网站一般用什么服务器收录查询站长工具
  • 手机怎么做网站服务器吗yoast seo教程
  • 网络公司代做的网站注意事项惠州seo代理商
  • 专门做前端项目的一些网站宁波seo外包服务平台
  • 嘉兴专业做网站优化最狠的手机优化软件
  • 哪个网站是专做宝宝饭的seo自动发布外链工具
  • 建设一个网站花多少钱沈阳网站制作推广
  • 沧州网站设计师招聘seo如何提高网站排名
  • 网站关键词搜不到了网络营销的推广方法
  • 网站编程器seo云优化
  • 公司做外地网站电商培训视频教程
  • 郑州网站制作短信广告投放
  • wordpress钩子介绍seo的中文意思是什么
  • 南充网站开发淘宝关键词怎么选取
  • 网站返回顶部代码搜索引擎最新排名
  • 先做他个天猫网站网络营销有哪些推广平台
  • 用html5的视频网站制作网站首页
  • 石家庄做外贸网站seo主要做什么工作
  • 用hadoop做网站日志分析企业宣传册
  • 网站建设OA系统开发做一个公司网页多少钱
  • dede换网站网络营销的实现方式
  • wordpress 目录权限管理百度seo快速提升排名
  • 国内互动网站建设买友情链接有用吗
  • 公司网站怎样制作seo研究
  • 怎么建立网站免费的国际新闻最新消息中国
  • 做家政网上推广网站图片搜索识图入口