Skip to content

Commit

Permalink
refactor: use windows-sys instead of winapi (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft authored Jul 25, 2024
1 parent 9958e7c commit 4817844
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
28 changes: 13 additions & 15 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _);
}
}

Expand Down Expand Up @@ -176,11 +175,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_sys::Win32::Foundation::CloseHandle;
use windows_sys::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 @@ -227,26 +225,26 @@ 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(),
hProcess: 0,
hThread: 0,
dwProcessId: 0,
dwThreadId: 0,
};
let mut si: STARTUPINFOW = unsafe { mem::zeroed() };
si.cb = mem::size_of::<STARTUPINFOW>() as DWORD;
si.cb = mem::size_of::<STARTUPINFOW>() 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);
Expand Down
16 changes: 9 additions & 7 deletions src/compiler/msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,18 @@ fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result<String> {

#[cfg(windows)]
pub fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result<String> {
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() {
return Ok(String::new());
}
unsafe {
// Get length of UTF-16 string
let len = winapi::um::stringapiset::MultiByteToWideChar(
let len = MultiByteToWideChar(
codepage,
flags,
multi_byte_str.as_ptr() as _,
Expand All @@ -132,7 +134,7 @@ pub fn from_local_codepage(multi_byte_str: &[u8]) -> io::Result<String> {
if len > 0 {
// Convert to UTF-16
let mut wstr: Vec<u16> = Vec::with_capacity(len as usize);
let len = winapi::um::stringapiset::MultiByteToWideChar(
let len = MultiByteToWideChar(
codepage,
flags,
multi_byte_str.as_ptr() as _,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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:
Expand Down
28 changes: 17 additions & 11 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,23 +636,27 @@ pub fn decode_path(bytes: &[u8]) -> std::io::Result<PathBuf> {

#[cfg(windows)]
pub fn decode_path(bytes: &[u8]) -> std::io::Result<PathBuf> {
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<Vec<u8>> {
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() {
return Ok(Vec::new());
}
unsafe {
// Get length of multibyte string
let len = winapi::um::stringapiset::WideCharToMultiByte(
let len = WideCharToMultiByte(
codepage,
flags,
wide_char_str.as_ptr(),
Expand All @@ -666,7 +670,7 @@ pub fn wide_char_to_multi_byte(wide_char_str: &[u16]) -> std::io::Result<Vec<u8>
if len > 0 {
// Convert from UTF-16 to multibyte
let mut astr: Vec<u8> = Vec::with_capacity(len as usize);
let len = winapi::um::stringapiset::WideCharToMultiByte(
let len = WideCharToMultiByte(
codepage,
flags,
wide_char_str.as_ptr(),
Expand Down Expand Up @@ -695,30 +699,32 @@ pub fn wide_char_to_multi_byte(wide_char_str: &[u16]) -> std::io::Result<Vec<u8>
/// 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<Vec<u16>> {
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,
);
if len > 0 {
// Convert to UTF-16
let mut wstr: Vec<u16> = 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,
Expand Down

0 comments on commit 4817844

Please sign in to comment.