Skip to content
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

Switch from winapi to windows-sys #26

Merged
merged 1 commit into from Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -14,7 +14,7 @@ edition = "2018"
libc = "0.2.62"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.5", features = ["handleapi", "namedpipeapi", "processthreadsapi", "winnt"] }
windows-sys = { version = "0.42.0", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_Security", "Win32_System_Threading"] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks like winapi hasn't been updated in a couple years, so this seems like a good change. Do you know if any of these features can be removed, or are they all needed? (I don't have a Windows machine with me today to just check.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The features are taken from its documentation, for example https://docs.rs/windows-sys/latest/windows_sys/Win32/System/Pipes/fn.CreatePipe.html, all of them are required


[features]
# Uses I/O safety features introduced in Rust 1.63
Expand Down
22 changes: 8 additions & 14 deletions src/windows.rs
Expand Up @@ -4,26 +4,20 @@ use std::fs::File;
use std::io;
use std::os::windows::prelude::*;
use std::ptr;
use winapi::shared::minwindef::BOOL;
use winapi::shared::ntdef::{HANDLE, PHANDLE};
use winapi::um::handleapi::DuplicateHandle;
use winapi::um::namedpipeapi;
use winapi::um::processthreadsapi::GetCurrentProcess;
use winapi::um::winnt::DUPLICATE_SAME_ACCESS;
use windows_sys::Win32::Foundation::{
DuplicateHandle, BOOL, DUPLICATE_SAME_ACCESS, HANDLE, INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::System::Pipes::CreatePipe;
use windows_sys::Win32::System::Threading::GetCurrentProcess;

pub(crate) fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
let mut read_pipe: HANDLE = ptr::null_mut();
let mut write_pipe: HANDLE = ptr::null_mut();
let mut read_pipe = INVALID_HANDLE_VALUE;
let mut write_pipe = INVALID_HANDLE_VALUE;

let ret = unsafe {
// NOTE: These pipes do not support IOCP. We might want to emulate
// anonymous pipes with CreateNamedPipe, as Rust's stdlib does.
namedpipeapi::CreatePipe(
&mut read_pipe as PHANDLE,
&mut write_pipe as PHANDLE,
ptr::null_mut(),
0,
)
CreatePipe(&mut read_pipe, &mut write_pipe, ptr::null_mut(), 0)
};

if ret == 0 {
Expand Down