1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![allow(non_snake_case)]

use crate::aliases::WinResult;
use crate::ffi::kernel32;
use crate::handles::HFILE;
use crate::privs::bool_to_winresult;
use crate::structs::{OVERLAPPED, SECURITY_ATTRIBUTES};

pub_struct_handle_closeable! {
	/// Handle to an
	/// [anonymous pipe](https://docs.microsoft.com/en-us/windows/win32/ipc/anonymous-pipes).
	/// Originally just a `HANDLE`.
	HPIPE
}

impl HPIPE {
	/// [`CreatePipe`](https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createpipe)
	/// static method.
	///
	/// Returns handles to the read and write pipes.
	///
	/// **Note:** Must be paired with
	/// [`HPIPE::CloseHandle`](crate::HPIPE::CloseHandle) calls.
	pub fn CreatePipe(
		attrs: Option<&mut SECURITY_ATTRIBUTES>,
		size: u32) -> WinResult<(HPIPE, HPIPE)>
	{
		let (mut hread, mut hwrite) = (Self::NULL, Self::NULL);
		bool_to_winresult(
			unsafe {
				kernel32::CreatePipe(
					&mut hread.ptr,
					&mut hwrite.ptr,
					attrs.map_or(std::ptr::null_mut(), |lp| lp as *mut _ as _),
					size,
				)
			},
		).map(|_| (hread, hwrite))
	}

	/// [`ReadFile`](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile)
	/// method.
	pub fn ReadFile(self,
		num_bytes_to_read: u32,
		overlapped: Option<&mut OVERLAPPED>) -> WinResult<Vec<u8>>
	{
		HFILE { ptr: self.ptr }.ReadFile(num_bytes_to_read, overlapped)
	}

	/// [`WriteFile`](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile)
	/// method.
	pub fn WriteFile(self,
		data: &[u8],
		overlapped: Option<&mut OVERLAPPED>) -> WinResult<u32>
	{
		HFILE { ptr: self.ptr }.WriteFile(data, overlapped)
	}
}