From 48178440c0a6a1c4070b2fb29bdb04ffa8be7198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Thu, 25 Jul 2024 16:27:59 +0200 Subject: [PATCH] refactor: use windows-sys instead of winapi (#2233) --- Cargo.lock | 2 +- Cargo.toml | 15 +++++++-------- src/commands.rs | 28 +++++++++++++--------------- src/compiler/msvc.rs | 16 +++++++++------- src/util.rs | 28 +++++++++++++++++----------- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc255b955..bf4c3425d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2605,7 +2605,7 @@ dependencies = [ "version-compare", "walkdir", "which", - "winapi", + "windows-sys 0.52.0", "zip", "zstd", ] diff --git a/Cargo.toml b/Cargo.toml index 47f1090ab..b2779cbe8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -140,16 +140,15 @@ daemonize = "0.5" optional = true version = "0.1.15" -[target.'cfg(windows)'.dependencies.winapi] +[target.'cfg(windows)'.dependencies.windows-sys] features = [ - "fileapi", - "handleapi", - "stringapiset", - "winnls", - "processenv", - "std", + "Win32_Foundation", + "Win32_Globalization", + "Win32_Storage_FileSystem", + "Win32_System_Threading", + "Win32_System_Console", ] -version = "0.3" +version = "0.52" [features] all = [ diff --git a/src/commands.rs b/src/commands.rs index ab740582e..2b68df1e0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -133,11 +133,10 @@ 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_sys::Win32::System::Console::{SetStdHandle, STD_ERROR_HANDLE}; // Ignore errors here. unsafe { - SetStdHandle(STD_ERROR_HANDLE, f.into_raw_handle()); + SetStdHandle(STD_ERROR_HANDLE, f.into_raw_handle() as _); } } @@ -176,11 +175,10 @@ fn run_server_process(startup_timeout: Option) -> Result) -> Result() as DWORD; + si.cb = mem::size_of::() as _; if unsafe { CreateProcessW( exe.as_mut_ptr(), ptr::null_mut(), ptr::null_mut(), ptr::null_mut(), - FALSE, + 0, CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW, - envp.as_mut_ptr() as LPVOID, + envp.as_mut_ptr().cast(), workdir.as_ptr(), - &mut si, + &si, &mut pi, - ) == TRUE + ) != 0 } { unsafe { CloseHandle(pi.hProcess); diff --git a/src/compiler/msvc.rs b/src/compiler/msvc.rs index 4ec7bf155..ccf77ce75 100644 --- a/src/compiler/msvc.rs +++ b/src/compiler/msvc.rs @@ -112,8 +112,10 @@ fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result { #[cfg(windows)] pub fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result { - let codepage = winapi::um::winnls::CP_OEMCP; - let flags = winapi::um::winnls::MB_ERR_INVALID_CHARS; + use windows_sys::Win32::Globalization::{MultiByteToWideChar, CP_OEMCP, MB_ERR_INVALID_CHARS}; + + let codepage = CP_OEMCP; + let flags = MB_ERR_INVALID_CHARS; // Empty string if multi_byte_str.is_empty() { @@ -121,7 +123,7 @@ pub fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result { } unsafe { // Get length of UTF-16 string - let len = winapi::um::stringapiset::MultiByteToWideChar( + let len = MultiByteToWideChar( codepage, flags, multi_byte_str.as_ptr() as _, @@ -132,7 +134,7 @@ pub fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result { if len > 0 { // Convert to UTF-16 let mut wstr: Vec = Vec::with_capacity(len as usize); - let len = winapi::um::stringapiset::MultiByteToWideChar( + let len = MultiByteToWideChar( codepage, flags, multi_byte_str.as_ptr() as _, @@ -832,10 +834,10 @@ fn normpath(path: &str) -> String { use std::os::windows::ffi::OsStringExt; use std::os::windows::io::AsRawHandle; use std::ptr; - use winapi::um::fileapi::GetFinalPathNameByHandleW; + use windows_sys::Win32::Storage::FileSystem::GetFinalPathNameByHandleW; File::open(path) .and_then(|f| { - let handle = f.as_raw_handle(); + let handle = f.as_raw_handle() as _; let size = unsafe { GetFinalPathNameByHandleW(handle, ptr::null_mut(), 0, 0) }; if size == 0 { return Err(io::Error::last_os_error()); @@ -2582,7 +2584,7 @@ mod test { #[cfg(windows)] fn local_oem_codepage_conversions() { use crate::util::wide_char_to_multi_byte; - use winapi::um::winnls::GetOEMCP; + use windows_sys::Win32::Globalization::GetOEMCP; let current_oemcp = unsafe { GetOEMCP() }; // We don't control the local OEM codepage so test only if it is one of: diff --git a/src/util.rs b/src/util.rs index 4fc45af2e..2000ae832 100644 --- a/src/util.rs +++ b/src/util.rs @@ -636,15 +636,19 @@ pub fn decode_path(bytes: &[u8]) -> std::io::Result { #[cfg(windows)] pub fn decode_path(bytes: &[u8]) -> std::io::Result { - let codepage = winapi::um::winnls::CP_OEMCP; - let flags = winapi::um::winnls::MB_ERR_INVALID_CHARS; + use windows_sys::Win32::Globalization::{CP_OEMCP, MB_ERR_INVALID_CHARS}; + + let codepage = CP_OEMCP; + let flags = MB_ERR_INVALID_CHARS; Ok(OsString::from_wide(&multi_byte_to_wide_char(codepage, flags, bytes)?).into()) } #[cfg(windows)] pub fn wide_char_to_multi_byte(wide_char_str: &[u16]) -> std::io::Result> { - let codepage = winapi::um::winnls::CP_OEMCP; + use windows_sys::Win32::Globalization::{WideCharToMultiByte, CP_OEMCP}; + + let codepage = CP_OEMCP; let flags = 0; // Empty string if wide_char_str.is_empty() { @@ -652,7 +656,7 @@ pub fn wide_char_to_multi_byte(wide_char_str: &[u16]) -> std::io::Result } unsafe { // Get length of multibyte string - let len = winapi::um::stringapiset::WideCharToMultiByte( + let len = WideCharToMultiByte( codepage, flags, wide_char_str.as_ptr(), @@ -666,7 +670,7 @@ pub fn wide_char_to_multi_byte(wide_char_str: &[u16]) -> std::io::Result if len > 0 { // Convert from UTF-16 to multibyte let mut astr: Vec = Vec::with_capacity(len as usize); - let len = winapi::um::stringapiset::WideCharToMultiByte( + let len = WideCharToMultiByte( codepage, flags, wide_char_str.as_ptr(), @@ -695,19 +699,21 @@ pub fn wide_char_to_multi_byte(wide_char_str: &[u16]) -> std::io::Result /// See https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx /// for more details. pub fn multi_byte_to_wide_char( - codepage: winapi::shared::minwindef::DWORD, - flags: winapi::shared::minwindef::DWORD, + codepage: u32, + flags: u32, multi_byte_str: &[u8], ) -> std::io::Result> { + use windows_sys::Win32::Globalization::MultiByteToWideChar; + if multi_byte_str.is_empty() { return Ok(vec![]); } unsafe { // Get length of UTF-16 string - let len = winapi::um::stringapiset::MultiByteToWideChar( + let len = MultiByteToWideChar( codepage, flags, - multi_byte_str.as_ptr() as winapi::um::winnt::LPSTR, + multi_byte_str.as_ptr(), multi_byte_str.len() as i32, std::ptr::null_mut(), 0, @@ -715,10 +721,10 @@ pub fn multi_byte_to_wide_char( if len > 0 { // Convert to UTF-16 let mut wstr: Vec = Vec::with_capacity(len as usize); - let len = winapi::um::stringapiset::MultiByteToWideChar( + let len = MultiByteToWideChar( codepage, flags, - multi_byte_str.as_ptr() as winapi::um::winnt::LPSTR, + multi_byte_str.as_ptr(), multi_byte_str.len() as i32, wstr.as_mut_ptr(), len,