From 0d1cab4f4e88476cdd0b783b44e04b2696018bb3 Mon Sep 17 00:00:00 2001 From: gentoo90 Date: Sat, 11 Nov 2017 13:00:06 +0200 Subject: [PATCH] Upgrade winapi to 0.3 (WIP) this should be merged when retep998/winapi-rs#316 is landed --- Cargo.toml | 10 +++---- src/enums.rs | 6 ++-- src/lib.rs | 72 ++++++++++++++++++++++------------------------ src/transaction.rs | 12 ++++---- src/types.rs | 2 +- 5 files changed, 50 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index db6f9a2..f197092 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,7 @@ readme = "README.md" keywords = ["Windows", "WinSDK", "Registry"] [dependencies] -winapi = "0.2" -kernel32-sys = "0.2" -advapi32-sys = "0.2" -ktmw32-sys = { version = "0.1", optional = true } +winapi = { version = "0.3.0-alpha.0", features = ["minwindef", "winerror", "winnt", "winreg", "handleapi", "ktmw32"] } rustc-serialize = { version = "0.3.19", optional = true } serde = { version = "1", optional = true } clippy = { version = "^0", optional = true } @@ -24,7 +21,7 @@ serde_derive = "1" [features] default = ["transactions", "serialization-serde"] -transactions = ["ktmw32-sys"] +transactions = [] serialization-serde = ["transactions", "serde"] [[example]] @@ -38,3 +35,6 @@ required-features = ["serialization-serde"] [[example]] name = "installed_apps" required-features = ["serialization-serde"] + +[patch.crates-io] +winapi = { git = 'https://github.com/retep998/winapi-rs' } diff --git a/src/enums.rs b/src/enums.rs index 1444720..00518cb 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -6,7 +6,7 @@ //! `use winreg::enums::*;` to import all needed enumerations and constants use super::winapi; -pub use winapi::{HKEY_CLASSES_ROOT, +pub use winapi::um::winreg::{HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, @@ -16,7 +16,7 @@ pub use winapi::{HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA, HKEY_CURRENT_USER_LOCAL_SETTINGS}; -pub use winapi::{KEY_QUERY_VALUE, +pub use winapi::um::winnt::{KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, @@ -36,7 +36,7 @@ macro_rules! winapi_enum{ #[allow(non_camel_case_types)] #[derive(Debug,Clone,PartialEq)] pub enum $t { - $( $v = winapi::$v as isize ),* + $( $v = winapi::um::winnt::$v as isize ),* } ) } diff --git a/src/lib.rs b/src/lib.rs index 9e9f70d..5dfb77f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,10 +95,6 @@ #![cfg_attr(feature="clippy", warn(option_unwrap_used))] #![cfg_attr(feature="clippy", warn(result_unwrap_used))] extern crate winapi; -extern crate kernel32; -extern crate advapi32; -#[cfg(feature = "transactions")] -extern crate ktmw32; #[cfg(feature = "serialization-serde")] extern crate serde; use std::ptr; @@ -109,8 +105,10 @@ use std::ffi::OsStr; use std::os::windows::ffi::OsStrExt; use std::mem::transmute; use std::io; -use winapi::winerror; -use winapi::{HKEY, DWORD, WCHAR}; +use winapi::shared::winerror; +use winapi::shared::minwindef::{HKEY, DWORD, BYTE, LPBYTE}; +use winapi::um::winnt::{self, WCHAR}; +use winapi::um::winreg as winapi_reg; use enums::*; use types::{FromRegValue, ToRegValue}; #[cfg(feature = "transactions")] @@ -224,7 +222,7 @@ impl RegKey { /// .open_subkey("Software").unwrap(); /// ``` pub fn open_subkey>(&self, path: P) -> io::Result { - self.open_subkey_with_flags(path, winapi::KEY_ALL_ACCESS) + self.open_subkey_with_flags(path, enums::KEY_ALL_ACCESS) } /// Open subkey with desired permissions. @@ -238,11 +236,11 @@ impl RegKey { /// let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); /// hklm.open_subkey_with_flags("SOFTWARE\\Microsoft", KEY_READ).unwrap(); /// ``` - pub fn open_subkey_with_flags>(&self, path: P, perms: winapi::REGSAM) -> io::Result { + pub fn open_subkey_with_flags>(&self, path: P, perms: winapi_reg::REGSAM) -> io::Result { let c_path = to_utf16(path); let mut new_hkey: HKEY = ptr::null_mut(); match unsafe { - advapi32::RegOpenKeyExW( + winapi_reg::RegOpenKeyExW( self.hkey, c_path.as_ptr(), 0, @@ -258,18 +256,18 @@ impl RegKey { /// Part of `transactions` feature. #[cfg(feature = "transactions")] pub fn open_subkey_transacted>(&self, path: P, t: &Transaction) -> io::Result { - self.open_subkey_transacted_with_flags(path, t, winapi::KEY_ALL_ACCESS) + self.open_subkey_transacted_with_flags(path, t, winnt::KEY_ALL_ACCESS) } /// Part of `transactions` feature. #[cfg(feature = "transactions")] - pub fn open_subkey_transacted_with_flags>(&self, path: P, t: &Transaction, perms: winapi::REGSAM) + pub fn open_subkey_transacted_with_flags>(&self, path: P, t: &Transaction, perms: winapi_reg::REGSAM) -> io::Result { let c_path = to_utf16(path); let mut new_hkey: HKEY = ptr::null_mut(); match unsafe { - advapi32::RegOpenKeyTransactedW( + winapi_reg::RegOpenKeyTransactedW( self.hkey, c_path.as_ptr(), 0, @@ -299,20 +297,20 @@ impl RegKey { /// let settings = hkcu.create_subkey("Software\\MyProduct\\Settings").unwrap(); /// ``` pub fn create_subkey>(&self, path: P) -> io::Result { - self.create_subkey_with_flags(path, winapi::KEY_ALL_ACCESS) + self.create_subkey_with_flags(path, enums::KEY_ALL_ACCESS) } - pub fn create_subkey_with_flags>(&self, path: P, perms: winapi::REGSAM) -> io::Result { + pub fn create_subkey_with_flags>(&self, path: P, perms: winapi_reg::REGSAM) -> io::Result { let c_path = to_utf16(path); let mut new_hkey: HKEY = ptr::null_mut(); let mut disp: DWORD = 0; match unsafe { - advapi32::RegCreateKeyExW( + winapi_reg::RegCreateKeyExW( self.hkey, c_path.as_ptr(), 0, ptr::null_mut(), - winapi::REG_OPTION_NON_VOLATILE, + winnt::REG_OPTION_NON_VOLATILE, perms, ptr::null_mut(), &mut new_hkey, @@ -327,24 +325,24 @@ impl RegKey { /// Part of `transactions` feature. #[cfg(feature = "transactions")] pub fn create_subkey_transacted>(&self, path: P, t: &Transaction) -> io::Result { - self.create_subkey_transacted_with_flags(path, t, winapi::KEY_ALL_ACCESS) + self.create_subkey_transacted_with_flags(path, t, winnt::KEY_ALL_ACCESS) } /// Part of `transactions` feature. #[cfg(feature = "transactions")] - pub fn create_subkey_transacted_with_flags>(&self, path: P, t: &Transaction, perms: winapi::REGSAM) + pub fn create_subkey_transacted_with_flags>(&self, path: P, t: &Transaction, perms: winapi_reg::REGSAM) -> io::Result { let c_path = to_utf16(path); let mut new_hkey: HKEY = ptr::null_mut(); let mut disp: DWORD = 0; match unsafe { - advapi32::RegCreateKeyTransactedW( + winapi_reg::RegCreateKeyTransactedW( self.hkey, c_path.as_ptr(), 0, ptr::null_mut(), - winapi::REG_OPTION_NON_VOLATILE, + winnt::REG_OPTION_NON_VOLATILE, perms, ptr::null_mut(), &mut new_hkey, @@ -374,7 +372,7 @@ impl RegKey { pub fn copy_tree>(&self, path: P, dest: &RegKey) -> io::Result<()> { let c_path = to_utf16(path); match unsafe { - advapi32::RegCopyTreeW( + winapi_reg::RegCopyTreeW( self.hkey, c_path.as_ptr(), dest.hkey, @@ -388,7 +386,7 @@ impl RegKey { pub fn query_info(&self) -> io::Result { let mut info: RegKeyMetadata = Default::default(); match unsafe { - advapi32::RegQueryInfoKeyW( + winapi_reg::RegQueryInfoKeyW( self.hkey, ptr::null_mut(), // Class: winapi::LPWSTR, ptr::null_mut(), // ClassLen: DWORD, @@ -460,7 +458,7 @@ impl RegKey { pub fn delete_subkey>(&self, path: P) -> io::Result<()> { let c_path = to_utf16(path); match unsafe { - advapi32::RegDeleteKeyW( + winapi_reg::RegDeleteKeyW( self.hkey, c_path.as_ptr(), //This parameter cannot be NULL. ) as DWORD @@ -475,7 +473,7 @@ impl RegKey { pub fn delete_subkey_transacted>(&self, path: P, t: &Transaction) -> io::Result<()> { let c_path = to_utf16(path); match unsafe { - advapi32::RegDeleteKeyTransactedW( + winapi_reg::RegDeleteKeyTransactedW( self.hkey, c_path.as_ptr(), //The value of this parameter cannot be NULL. 0, @@ -510,7 +508,7 @@ impl RegKey { path_ptr = c_path.as_ptr(); } match unsafe{ - advapi32::RegDeleteTreeW( + winapi_reg::RegDeleteTreeW( self.hkey, path_ptr,//If this parameter is NULL, the subkeys and values of this key are deleted. ) as DWORD @@ -561,19 +559,19 @@ impl RegKey { let mut buf: Vec = Vec::with_capacity(buf_len as usize); loop { match unsafe { - advapi32::RegQueryValueExW( + winapi_reg::RegQueryValueExW( self.hkey, c_name.as_ptr() as *const u16, ptr::null_mut(), &mut buf_type, - buf.as_mut_ptr() as winapi::LPBYTE, + buf.as_mut_ptr() as LPBYTE, &mut buf_len ) as DWORD } { 0 => { unsafe{ buf.set_len(buf_len as usize); } // minimal check before transmute to RegType - if buf_type > winapi::REG_QWORD { + if buf_type > winnt::REG_QWORD { return werr!(winerror::ERROR_BAD_FILE_TYPE); } let t: RegType = unsafe{ transmute(buf_type as u8) }; @@ -624,12 +622,12 @@ impl RegKey { let c_name = to_utf16(name); let t = value.vtype.clone() as DWORD; match unsafe{ - advapi32::RegSetValueExW( + winapi_reg::RegSetValueExW( self.hkey, c_name.as_ptr(), 0, t, - value.bytes.as_ptr() as *const winapi::BYTE, + value.bytes.as_ptr() as *const BYTE, value.bytes.len() as u32 ) as DWORD } { @@ -653,7 +651,7 @@ impl RegKey { pub fn delete_value>(&self, name: N) -> io::Result<()> { let c_name = to_utf16(name); match unsafe { - advapi32::RegDeleteValueW( + winapi_reg::RegDeleteValueW( self.hkey, c_name.as_ptr(), ) as DWORD @@ -757,9 +755,9 @@ impl RegKey { fn close_(&mut self) -> io::Result<()> { // don't try to close predefined keys - if self.hkey >= winapi::HKEY_CLASSES_ROOT { return Ok(()) }; + if self.hkey >= enums::HKEY_CLASSES_ROOT { return Ok(()) }; match unsafe { - advapi32::RegCloseKey(self.hkey) as DWORD + winapi_reg::RegCloseKey(self.hkey) as DWORD } { 0 => Ok(()), err => werr!(err) @@ -770,7 +768,7 @@ impl RegKey { let mut name_len = 2048; let mut name = [0 as WCHAR; 2048]; match unsafe { - advapi32::RegEnumKeyExW( + winapi_reg::RegEnumKeyExW( self.hkey, index, name.as_mut_ptr(), @@ -803,14 +801,14 @@ impl RegKey { let mut buf: Vec = Vec::with_capacity(buf_len as usize); loop { match unsafe { - advapi32::RegEnumValueW( + winapi_reg::RegEnumValueW( self.hkey, index, name.as_mut_ptr(), &mut name_len, ptr::null_mut(), // reserved &mut buf_type, - buf.as_mut_ptr() as winapi::LPBYTE, + buf.as_mut_ptr() as LPBYTE, &mut buf_len, ) as DWORD } { @@ -821,7 +819,7 @@ impl RegKey { }; unsafe{ buf.set_len(buf_len as usize); } // minimal check before transmute to RegType - if buf_type > winapi::REG_QWORD { + if buf_type > winnt::REG_QWORD { return Some(werr!(winerror::ERROR_BAD_FILE_TYPE)); } let t: RegType = unsafe{ transmute(buf_type as u8) }; diff --git a/src/transaction.rs b/src/transaction.rs index f71ce13..5686e96 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -41,13 +41,13 @@ #![cfg(feature = "transactions")] use std::ptr; use std::io; -use super::winapi; -use super::kernel32; -use super::ktmw32; +use winapi::um::winnt; +use winapi::um::handleapi; +use winapi::um::ktmw32; #[derive(Debug)] pub struct Transaction { - pub handle: winapi::HANDLE, + pub handle: winnt::HANDLE, } impl Transaction { @@ -63,7 +63,7 @@ impl Transaction { 0, ptr::null_mut(), ); - if handle == winapi::INVALID_HANDLE_VALUE { + if handle == handleapi::INVALID_HANDLE_VALUE { return Err(io::Error::last_os_error()) }; Ok(Transaction{ handle: handle }) @@ -90,7 +90,7 @@ impl Transaction { fn close_(&mut self) -> io::Result<()> { unsafe { - match kernel32::CloseHandle(self.handle) { + match handleapi::CloseHandle(self.handle) { 0 => Err(io::Error::last_os_error()), _ => Ok(()) } diff --git a/src/types.rs b/src/types.rs index d3c9397..fd605fa 100644 --- a/src/types.rs +++ b/src/types.rs @@ -9,7 +9,7 @@ use std::slice; use std::io; use std::ffi::{OsStr,OsString}; use std::os::windows::ffi::{OsStrExt,OsStringExt}; -use super::winapi::winerror; +use super::winapi::shared::winerror; use super::{RegValue}; use super::enums::*; use super::{to_utf16,v16_to_v8};