Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created October 28, 2020 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/29e450e292fa7268c08e809cd2cce7b0 to your computer and use it in GitHub Desktop.
Save cowboyd/29e450e292fa7268c08e809cd2cce7b0 to your computer and use it in GitHub Desktop.
Do stuff on windows
use neon::prelude::*;
use winapi::shared::minwindef::{TRUE, FALSE};
use winapi::um::winnt::LPSTR;
//use winapi::um::synchapi::{WaitForSingleObject};
//use winapi::um::handleapi::CloseHandle;
use winapi::um::processthreadsapi::*;
use winapi::um::consoleapi::*;
use winapi::um::wincon::*;
use std::process::Command;
// https://gist.github.com/rdp/f51fb274d69c5c31b6be
fn ctrlc(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let pid = cx.argument::<JsNumber>(0)?.value() as u32;
unsafe {
let mut holder = Command::new("node")
.arg("-e")
.arg("setTimeout(() => {}, 65000000)")
.spawn().unwrap();
FreeConsole();
//let current_pid = GetCurrentProcessId();
if AttachConsole(pid) != 0 {
SetConsoleCtrlHandler(None, TRUE);
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
FreeConsole();
AttachConsole(holder.id());
// WaitForSingleObject(handle, 100);
//SetConsoleCtrlHandler(None, FALSE.into());
// AttachConsole(current_pid);
holder.kill().unwrap();
Ok(cx.undefined())
} else {
holder.kill().unwrap();
panic!("unable to attach console")
}
// CloseHandle(handle)
}
}
fn get_current_pid(mut cx: FunctionContext) -> JsResult<JsNumber> {
Ok(cx.number(unsafe { GetCurrentProcessId() } as f64))
}
register_module!(mut cx, {
cx.export_function("getCurrentPid", get_current_pid)?;
cx.export_function("ctrlc", ctrlc)?;
Ok(())
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment