Skip to content

Commit

Permalink
Upgrade winapi to 0.3 (WIP)
Browse files Browse the repository at this point in the history
this should be merged when retep998/winapi-rs#316 is landed
  • Loading branch information
gentoo90 committed Nov 12, 2017
1 parent 4d1b9a8 commit 0d1cab4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 52 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -24,7 +21,7 @@ serde_derive = "1"

[features]
default = ["transactions", "serialization-serde"]
transactions = ["ktmw32-sys"]
transactions = []
serialization-serde = ["transactions", "serde"]

[[example]]
Expand All @@ -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' }
6 changes: 3 additions & 3 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 ),*
}
)
}
Expand Down
72 changes: 35 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand Down Expand Up @@ -224,7 +222,7 @@ impl RegKey {
/// .open_subkey("Software").unwrap();
/// ```
pub fn open_subkey<P: AsRef<OsStr>>(&self, path: P) -> io::Result<RegKey> {
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.
Expand All @@ -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<P: AsRef<OsStr>>(&self, path: P, perms: winapi::REGSAM) -> io::Result<RegKey> {
pub fn open_subkey_with_flags<P: AsRef<OsStr>>(&self, path: P, perms: winapi_reg::REGSAM) -> io::Result<RegKey> {
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,
Expand All @@ -258,18 +256,18 @@ impl RegKey {
/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn open_subkey_transacted<P: AsRef<OsStr>>(&self, path: P, t: &Transaction) -> io::Result<RegKey> {
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<P: AsRef<OsStr>>(&self, path: P, t: &Transaction, perms: winapi::REGSAM)
pub fn open_subkey_transacted_with_flags<P: AsRef<OsStr>>(&self, path: P, t: &Transaction, perms: winapi_reg::REGSAM)
-> io::Result<RegKey>
{
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,
Expand Down Expand Up @@ -299,20 +297,20 @@ impl RegKey {
/// let settings = hkcu.create_subkey("Software\\MyProduct\\Settings").unwrap();
/// ```
pub fn create_subkey<P: AsRef<OsStr>>(&self, path: P) -> io::Result<RegKey> {
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<P: AsRef<OsStr>>(&self, path: P, perms: winapi::REGSAM) -> io::Result<RegKey> {
pub fn create_subkey_with_flags<P: AsRef<OsStr>>(&self, path: P, perms: winapi_reg::REGSAM) -> io::Result<RegKey> {
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,
Expand All @@ -327,24 +325,24 @@ impl RegKey {
/// Part of `transactions` feature.
#[cfg(feature = "transactions")]
pub fn create_subkey_transacted<P: AsRef<OsStr>>(&self, path: P, t: &Transaction) -> io::Result<RegKey> {
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<P: AsRef<OsStr>>(&self, path: P, t: &Transaction, perms: winapi::REGSAM)
pub fn create_subkey_transacted_with_flags<P: AsRef<OsStr>>(&self, path: P, t: &Transaction, perms: winapi_reg::REGSAM)
-> io::Result<RegKey>
{
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,
Expand Down Expand Up @@ -374,7 +372,7 @@ impl RegKey {
pub fn copy_tree<P: AsRef<OsStr>>(&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,
Expand All @@ -388,7 +386,7 @@ impl RegKey {
pub fn query_info(&self) -> io::Result<RegKeyMetadata> {
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,
Expand Down Expand Up @@ -460,7 +458,7 @@ impl RegKey {
pub fn delete_subkey<P: AsRef<OsStr>>(&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
Expand All @@ -475,7 +473,7 @@ impl RegKey {
pub fn delete_subkey_transacted<P: AsRef<OsStr>>(&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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -561,19 +559,19 @@ impl RegKey {
let mut buf: Vec<u8> = 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) };
Expand Down Expand Up @@ -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
} {
Expand All @@ -653,7 +651,7 @@ impl RegKey {
pub fn delete_value<N: AsRef<OsStr>>(&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
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
Expand Down Expand Up @@ -803,14 +801,14 @@ impl RegKey {
let mut buf: Vec<u8> = 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
} {
Expand All @@ -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) };
Expand Down
12 changes: 6 additions & 6 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 })
Expand All @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 0d1cab4

Please sign in to comment.