Skip to content

Commit

Permalink
refactor: Use windows instead of winapi
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Jul 25, 2024
1 parent 9958e7c commit 0c162b8
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 126 deletions.
127 changes: 99 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,17 @@ daemonize = "0.5"
optional = true
version = "0.1.15"

[target.'cfg(windows)'.dependencies.winapi]
[target.'cfg(windows)'.dependencies.windows]
features = [
"fileapi",
"handleapi",
"stringapiset",
"winnls",
"processenv",
"std",
"Win32_System_SystemServices",
"Win32_Foundation",
"Win32_Globalization",
"Win32_Storage_FileSystem",
"Win32_System_WindowsProgramming",
"Win32_System_Threading",
"Win32_System_Console",
]
version = "0.3"
version = "0.58"

[features]
all = [
Expand Down
49 changes: 22 additions & 27 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ fn redirect_stderr(f: File) {
#[cfg(windows)]
fn redirect_stderr(f: File) {
use std::os::windows::io::IntoRawHandle;
use winapi::um::processenv::SetStdHandle;
use winapi::um::winbase::STD_ERROR_HANDLE;
use windows::Win32::System::Console::{SetStdHandle, STD_ERROR_HANDLE};

// Ignore errors here.
unsafe {
SetStdHandle(STD_ERROR_HANDLE, f.into_raw_handle());
Expand Down Expand Up @@ -176,11 +176,10 @@ fn run_server_process(startup_timeout: Option<Duration>) -> Result<ServerStartup
use std::ptr;
use tokio::net::windows::named_pipe;
use uuid::Uuid;
use winapi::shared::minwindef::{DWORD, FALSE, LPVOID, TRUE};
use winapi::um::handleapi::CloseHandle;
use winapi::um::processthreadsapi::{CreateProcessW, PROCESS_INFORMATION, STARTUPINFOW};
use winapi::um::winbase::{
CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW, CREATE_UNICODE_ENVIRONMENT,
use windows::Win32::Foundation::{CloseHandle, BOOL, HANDLE};
use windows::Win32::System::Threading::{
CreateProcessW, CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW, CREATE_UNICODE_ENVIRONMENT,
PROCESS_INFORMATION, STARTUPINFOW,
};

trace!("run_server_process");
Expand Down Expand Up @@ -226,31 +225,27 @@ fn run_server_process(startup_timeout: Option<Duration>) -> Result<ServerStartup

// TODO: Expose `bInheritHandles` argument of `CreateProcessW` through the
// standard library's `Command` type and then use that instead.
let mut pi = PROCESS_INFORMATION {
hProcess: ptr::null_mut(),
hThread: ptr::null_mut(),
dwProcessId: 0,
dwThreadId: 0,
};
let mut si: STARTUPINFOW = unsafe { mem::zeroed() };
si.cb = mem::size_of::<STARTUPINFOW>() as DWORD;
if unsafe {
let mut pi = PROCESS_INFORMATION::default();
let mut si = STARTUPINFOW::default();
si.cb = mem::size_of::<STARTUPINFOW>() as u32;
let result = unsafe {
CreateProcessW(
exe.as_mut_ptr(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
FALSE,
windows::core::PCWSTR(exe.as_mut_ptr()),
windows::core::PWSTR::null(),
None,
None,
BOOL::from(false),
CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW,
envp.as_mut_ptr() as LPVOID,
workdir.as_ptr(),
Some(envp.as_mut_ptr() as *mut _),
windows::core::PCWSTR(workdir.as_ptr()),
&mut si,
&mut pi,
) == TRUE
} {
)
};
if result.as_bool() {
unsafe {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(HANDLE(pi.hProcess));
CloseHandle(HANDLE(pi.hThread));
}
} else {
return Err(io::Error::last_os_error().into());
Expand Down
Loading

0 comments on commit 0c162b8

Please sign in to comment.