Skip to content

Clean up Windows API dependencies: migrate from winapi to windows crate #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ dependencies = [
"tempfile",
"thiserror 2.0.12",
"vm_resource",
"winapi",
"windows 0.59.0",
]

[[package]]
Expand Down Expand Up @@ -2694,7 +2694,6 @@ dependencies = [
"inspect",
"mesh_protobuf",
"thiserror 2.0.12",
"winapi",
"windows 0.59.0",
"windows-sys 0.59.0",
"zerocopy 0.8.24",
Expand Down Expand Up @@ -5160,7 +5159,6 @@ dependencies = [
"thiserror 2.0.12",
"tracing",
"widestring",
"winapi",
"windows 0.59.0",
]

Expand Down Expand Up @@ -6805,7 +6803,7 @@ dependencies = [
"crossterm",
"libc",
"thiserror 2.0.12",
"winapi",
"windows 0.59.0",
]

[[package]]
Expand Down
6 changes: 0 additions & 6 deletions support/guid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,5 @@ zerocopy.workspace = true
windows-sys.workspace = true
windows.workspace = true

[target.'cfg(windows)'.dependencies.winapi]
workspace = true
features = [
"guiddef",
]

[lints]
workspace = true
22 changes: 0 additions & 22 deletions support/guid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,28 +266,6 @@ mod windows {
#![cfg(windows)]
use super::Guid;

impl From<winapi::shared::guiddef::GUID> for Guid {
fn from(guid: winapi::shared::guiddef::GUID) -> Self {
Self {
data1: guid.Data1,
data2: guid.Data2,
data3: guid.Data3,
data4: guid.Data4,
}
}
}

impl From<Guid> for winapi::shared::guiddef::GUID {
fn from(guid: Guid) -> Self {
Self {
Data1: guid.data1,
Data2: guid.data2,
Data3: guid.data3,
Data4: guid.data4,
}
}
}

impl From<windows_sys::core::GUID> for Guid {
fn from(guid: windows_sys::core::GUID) -> Self {
Self {
Expand Down
39 changes: 12 additions & 27 deletions support/pal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,24 @@ ntapi = { workspace = true, features = ["impl-default"] }
socket2 = { workspace = true, features = ["all"] }
widestring.workspace = true

[target.'cfg(windows)'.dependencies.winapi]
workspace = true
features = [
"errhandlingapi",
"fileapi",
"handleapi",
"heapapi",
"ioapiset",
"impl-default",
"jobapi2",
"memoryapi",
"namedpipeapi",
"ntstatus",
"processenv",
"processthreadsapi",
"sddl",
"securitybaseapi",
"std",
"synchapi",
"threadpoolapiset",
"winbase",
"winerror",
"winioctl",
"winnt",
"winsock2",
]


[target.'cfg(windows)'.dependencies.windows]
workspace = true
features = [
"Wdk_Foundation",
"Win32_Foundation",
"Win32_Security"
"Win32_Security",
"Win32_Security_Authorization",
"Win32_System_IO",
"Win32_System_Threading",
"Win32_System_ErrorReporting",
"Win32_System_Memory",
"Win32_System_SystemServices",
"Win32_System_JobObjects",
"Win32_System_Pipes",
"Win32_Networking_WinSock",
"Win32_Storage_FileSystem"
]

[target.'cfg(unix)'.dependencies]
Expand Down
95 changes: 46 additions & 49 deletions support/pal/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub mod security;
pub mod tp;

use self::security::SecurityDescriptor;
use handleapi::INVALID_HANDLE_VALUE;
use ntapi::ntdef::ANSI_STRING;
use ntapi::ntdef::UNICODE_STRING;
use ntapi::ntioapi::FILE_COMPLETION_INFORMATION;
use ntapi::ntioapi::FileReplaceCompletionInformation;
use ntapi::ntioapi::IO_STATUS_BLOCK;
Expand All @@ -28,13 +29,10 @@ use ntapi::ntioapi::NtSetInformationFile;
use ntapi::ntobapi::NtCreateDirectoryObject;
use ntapi::ntobapi::NtOpenDirectoryObject;
use ntapi::ntrtl;
use ntdef::ANSI_STRING;
use ntdef::UNICODE_STRING;
use ntrtl::RtlAllocateHeap;
use ntrtl::RtlDosPathNameToNtPathName_U_WithStatus;
use ntrtl::RtlFreeUnicodeString;
use ntrtl::RtlNtStatusToDosErrorNoTeb;
use processthreadsapi::GetExitCodeProcess;
use std::cell::UnsafeCell;
use std::ffi::OsStr;
use std::ffi::c_void;
Expand All @@ -55,32 +53,33 @@ use std::sync::atomic::Ordering;
use std::time::Duration;
use widestring::U16CString;
use widestring::Utf16Str;
use winapi::shared::ntdef;
use winapi::shared::ntdef::NTSTATUS;
use winapi::shared::ntstatus;
use winapi::shared::ntstatus::STATUS_PENDING;
use winapi::shared::winerror::ERROR_BAD_PATHNAME;
use winapi::shared::ws2def;
use winapi::um::errhandlingapi::GetErrorMode;
use winapi::um::errhandlingapi::SetErrorMode;
use winapi::um::handleapi;
use winapi::um::handleapi::CloseHandle;
use winapi::um::heapapi::GetProcessHeap;
use winapi::um::ioapiset::CreateIoCompletionPort;
use winapi::um::ioapiset::GetQueuedCompletionStatusEx;
use winapi::um::ioapiset::PostQueuedCompletionStatus;
use winapi::um::minwinbase::OVERLAPPED;
use winapi::um::minwinbase::OVERLAPPED_ENTRY;
use winapi::um::processenv::SetStdHandle;
use winapi::um::processthreadsapi;
use winapi::um::processthreadsapi::TerminateProcess;
use winapi::um::synchapi;
use winapi::um::winbase::INFINITE;
use winapi::um::winbase::SEM_FAILCRITICALERRORS;
use winapi::um::winbase::STD_OUTPUT_HANDLE;
use winapi::um::winbase::SetFileCompletionNotificationModes;
use winapi::um::winnt;
use winapi::um::winsock2;
use windows::Win32::Foundation::CloseHandle;
use windows::Win32::Foundation::DUPLICATE_SAME_ACCESS;
use windows::Win32::Foundation::DuplicateHandle;
use windows::Win32::Foundation::ERROR_BAD_PATHNAME;
use windows::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows::Win32::Foundation::NTSTATUS;
use windows::Win32::Foundation::STATUS_PENDING;
use windows::Win32::Networking::WinSock::IOC_IN;
use windows::Win32::Networking::WinSock::IOC_VENDOR;
use windows::Win32::Storage::FileSystem::SetFileCompletionNotificationModes;
use windows::Win32::System::Console::STD_OUTPUT_HANDLE;
use windows::Win32::System::Console::SetStdHandle;
use windows::Win32::System::IO::CreateIoCompletionPort;
use windows::Win32::System::IO::GetQueuedCompletionStatusEx;
use windows::Win32::System::IO::OVERLAPPED;
use windows::Win32::System::IO::OVERLAPPED_ENTRY;
use windows::Win32::System::IO::PostQueuedCompletionStatus;
use windows::Win32::System::Memory::GetProcessHeap;
use windows::Win32::System::SystemServices::GetErrorMode;
use windows::Win32::System::SystemServices::SEM_FAILCRITICALERRORS;
use windows::Win32::System::SystemServices::SetErrorMode;
use windows::Win32::System::Threading::GetCurrentProcess;
use windows::Win32::System::Threading::GetExitCodeProcess;
use windows::Win32::System::Threading::GetProcessId;
use windows::Win32::System::Threading::INFINITE;
use windows::Win32::System::Threading::TerminateProcess;
use windows::Win32::System::Threading::WaitForSingleObject;

#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand All @@ -99,11 +98,11 @@ impl BorrowedHandleExt for BorrowedHandle<'_> {
let options = if access.is_some() {
0
} else {
winnt::DUPLICATE_SAME_ACCESS
DUPLICATE_SAME_ACCESS.0
};
unsafe {
let process = processthreadsapi::GetCurrentProcess();
if handleapi::DuplicateHandle(
let process = GetCurrentProcess();
if DuplicateHandle(
process,
self.as_raw_handle(),
process,
Expand Down Expand Up @@ -134,8 +133,8 @@ pub trait OwnedSocketExt: Sized {
fn from_handle(handle: OwnedHandle) -> Result<Self>;
}

const SIO_SOCKET_TRANSFER_BEGIN: u32 = ws2def::IOC_IN | ws2def::IOC_VENDOR | 301;
const SIO_SOCKET_TRANSFER_END: u32 = ws2def::IOC_IN | ws2def::IOC_VENDOR | 302;
const SIO_SOCKET_TRANSFER_BEGIN: u32 = IOC_IN | IOC_VENDOR | 301;
const SIO_SOCKET_TRANSFER_END: u32 = IOC_IN | IOC_VENDOR | 302;

/// Ensures WSAStartup has been called for the process.
fn init_winsock() {
Expand Down Expand Up @@ -209,7 +208,7 @@ struct WaitObject(OwnedHandle);

impl WaitObject {
fn wait(&self) {
assert!(unsafe { synchapi::WaitForSingleObject(self.0.as_raw_handle(), INFINITE) } == 0);
assert!(unsafe { WaitForSingleObject(self.0.as_raw_handle(), INFINITE) } == 0);
}
}

Expand All @@ -233,7 +232,7 @@ impl Process {

pub fn id(&self) -> u32 {
unsafe {
let pid = processthreadsapi::GetProcessId(self.as_handle().as_raw_handle());
let pid = GetProcessId(self.as_handle().as_raw_handle());
assert_ne!(pid, 0);
pid
}
Expand Down Expand Up @@ -995,17 +994,15 @@ unsafe impl Sync for Overlapped {}
#[macro_export]
macro_rules! delayload {
{$dll:literal {$($($idents:ident)+ ($($params:ident : $types:ty),* $(,)?) -> $result:ty;)*}} => {
fn get_module() -> Result<::winapi::shared::minwindef::HINSTANCE, u32> {
fn get_module() -> Result<::windows::Win32::Foundation::HINSTANCE, u32> {
use ::std::ptr::null_mut;
use ::std::sync::atomic::{AtomicPtr, Ordering};
use ::winapi::{
um::{
errhandlingapi::GetLastError,
libloaderapi::{FreeLibrary, LoadLibraryA},
},
use ::windows::Win32::{
Foundation::GetLastError,
System::LibraryLoader::{FreeLibrary, LoadLibraryA},
};

static MODULE: AtomicPtr<::winapi::shared::minwindef::HINSTANCE__> = AtomicPtr::new(null_mut());
static MODULE: AtomicPtr<::windows::Win32::Foundation::HINSTANCE> = AtomicPtr::new(null_mut());
let mut module = MODULE.load(Ordering::Relaxed);
if module.is_null() {
module = unsafe { LoadLibraryA(concat!($dll, "\0").as_ptr() as *const i8) };
Expand Down Expand Up @@ -1041,9 +1038,9 @@ macro_rules! delayload {

(@body $name:ident($($params:ident : $types:ty),* $(,)?) -> $result:ty) => {
{
use ::winapi::{
shared::winerror::ERROR_PROC_NOT_FOUND,
um::libloaderapi::GetProcAddress,
use ::windows::Win32::{
Foundation::ERROR_PROC_NOT_FOUND,
System::LibraryLoader::GetProcAddress,
};
use ::std::concat;
use ::std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -1076,10 +1073,10 @@ macro_rules! delayload {
}
};

(@result_from_win32((i32), $val:expr)) => { ::winapi::shared::winerror::HRESULT_FROM_WIN32($val) };
(@result_from_win32((i32), $val:expr)) => { ::windows::Win32::Foundation::HRESULT::from_win32($val) };
(@result_from_win32((u32), $val:expr)) => { $val };
(@result_from_win32((DWORD), $val:expr)) => { $val };
(@result_from_win32((HRESULT), $val:expr)) => { ::winapi::shared::winerror::HRESULT_FROM_WIN32($val) };
(@result_from_win32((HRESULT), $val:expr)) => { ::windows::Win32::Foundation::HRESULT::from_win32($val) };
(@result_from_win32(($t:tt), $val:expr)) => { panic!("could not load: {}", $val) };
}

Expand Down
13 changes: 4 additions & 9 deletions support/pal/src/windows/afd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@
use super::SendSyncRawHandle;
use super::UnicodeString;
use super::chk_status;
use ioapiset::DeviceIoControl;
use minwinbase::OVERLAPPED;
use ntapi::ntdef::OBJECT_ATTRIBUTES;
use ntapi::ntioapi::NtOpenFile;
use ntdef::OBJECT_ATTRIBUTES;
use std::fs::File;
use std::mem::zeroed;
use std::os::windows::prelude::*;
use std::ptr::null_mut;
use winapi::shared::ntdef;
use winapi::shared::winerror;
use winapi::um::ioapiset;
use winapi::um::minwinbase;
use winapi::um::winnt;
use winerror::ERROR_IO_PENDING;
use windows::Win32::Foundation::ERROR_IO_PENDING;
use windows::Win32::System::IO::DeviceIoControl;
use windows::Win32::System::IO::OVERLAPPED;

#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
Expand Down
6 changes: 3 additions & 3 deletions support/pal/src/windows/alpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use std::os::windows::prelude::*;
use std::ptr::NonNull;
use std::ptr::null_mut;
use std::time::Duration;
use winapi::shared::ntstatus::STATUS_TIMEOUT;
use windows::Win32::Foundation::STATUS_TIMEOUT;

mod ntlpcapi {
#![allow(non_snake_case, dead_code, clippy::upper_case_acronyms)]

pub use ntapi::ntlpcapi::*;
use winapi::shared::ntdef::HANDLE;
use winapi::shared::ntdef::NTSTATUS;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::Foundation::NTSTATUS;

// These constants are not defined in ntapi.
pub const LPC_CONNECTION_REPLY: u32 = 11;
Expand Down
17 changes: 9 additions & 8 deletions support/pal/src/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use std::os::windows::io::AsRawHandle;
use std::path::Path;
use std::ptr::null_mut;
use widestring::U16CString;
use winapi::shared::ntdef::OBJ_CASE_INSENSITIVE;
use winapi::shared::ntdef::OBJECT_ATTRIBUTES;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::minwinbase::WIN32_FIND_DATAW;
use windows::Wdk::Foundation::OBJ_CASE_INSENSITIVE;
use windows::Wdk::Foundation::OBJECT_ATTRIBUTES;
use windows::Win32::Foundation::GetLastError;
use windows::Win32::Storage::FileSystem::WIN32_FIND_DATAW;

pub fn query_stat_lx_by_name(path: &Path) -> io::Result<ntioapi::FILE_STAT_LX_INFORMATION> {
let mut pathu = dos_to_nt_path(path)?;
Expand Down Expand Up @@ -68,13 +68,13 @@ fn find_first_file_data(path: &Path) -> io::Result<WIN32_FIND_DATAW> {

unsafe {
let mut data = zeroed();
let handle = winapi::um::fileapi::FindFirstFileW(path.as_ptr(), &mut data);
let handle = windows::Win32::Storage::FileSystem::FindFirstFileW(path.as_ptr(), &mut data);

if handle == winapi::um::handleapi::INVALID_HANDLE_VALUE {
if handle == windows::Win32::Foundation::INVALID_HANDLE_VALUE {
Err(io::Error::from_raw_os_error(GetLastError() as i32))
} else {
// Close the handle opened by FindFirstfileW.
winapi::um::fileapi::FindClose(handle);
windows::Win32::Storage::FileSystem::FindClose(handle);
Ok(data)
}
}
Expand All @@ -86,7 +86,8 @@ pub fn is_unix_socket(path: &Path) -> io::Result<bool> {

let data = find_first_file_data(path)?;
Ok(
data.dwFileAttributes & winapi::um::winnt::FILE_ATTRIBUTE_REPARSE_POINT != 0
data.dwFileAttributes & windows::Win32::Storage::FileSystem::FILE_ATTRIBUTE_REPARSE_POINT
!= 0
&& data.dwReserved0 == IO_REPARSE_TAG_AF_UNIX,
)
}
Expand Down
Loading
Loading