#windows service, no documentation, bad examples, how do i use this?

33 messages · Page 1 of 1 (latest)

sacred snow
#
extern crate windows_service;

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

fn my_service_main(arguments: Vec<OsString>) {
    if let Err(e) = run_service(arguments) {
        println!("{}", e);
    }
}

fn run_service(arguments: Vec<OsString>) -> windows_service::Result<()> {
    let event_handler = move |control_event| -> ServiceControlHandlerResult {
        match control_event {
            ServiceControl::Stop | ServiceControl::Interrogate => {
                ServiceControlHandlerResult::NoError
            }
            _ => ServiceControlHandlerResult::NotImplemented,
        }
    };
    let status_handle = service_control_handler::register("my_service_name", event_handler)?;

    let next_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,
    };
    status_handle.set_service_status(next_status)?;

    // for i in 0..100000{
    //     println!("{}", i);
    //     thread::sleep(Duration::from_secs(10));
    // }

    Ok(())
}

fn main() {
    let args: Vec<OsString> = std::env::args_os().collect();
    my_service_main(args);
}
}```
from the example in https://docs.rs/windows-service/latest/windows_service/ 
`IO error in winapi call`
vital ocean
vital ocean
sacred snow
#

which doesnt compile

vital ocean
#

Anyway, if you just want to run a program in the background, it should be enough with windows_subsystem = "windows"

vital ocean
vapid terrace
vital ocean
#

cheers!

sacred snow
vital ocean
#

sure, although you won't be able to see the output anywhere

vital ocean
#

because you don't have a console window

sacred snow
#

why

vital ocean
#

because you don't want something that appears in the taskbar??

sacred snow
#

so its doing its thing in the background

vital ocean
#

yes

sacred snow
#

and it can read key inputs?

vital ocean
#

uhh

#

why do you want to do that ferrisHmm

sacred snow
#

macro

vital ocean
#

?crate device_query

mighty cloudBOT
#

A basic library for querying keyboard and mouse state on-demand without a window.

Version

2.0.0

Downloads

75 273

sacred snow
#

W it works

vital ocean
#

Nice!

#

you probably want to use some logging crate now to see output heh

sacred snow
#

who needs logging crates when i can just create a txt file

sacred snow
#

if its running in the background

vital ocean
sacred snow
#

w rizz