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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt official windows-rs API #5

Open
wants to merge 5 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 7 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ log = "0.4.11"
thiserror = "1.0.20"
utfx = "0.1"

[dependencies.winapi]
version = "0.3.9"
[dependencies.windows]
version = "0.28.0"
features = [
"winerror",
"winreg",
"processthreadsapi",
"winnt",
"winbase",
"securitybaseapi",
"ntdef",
]
"Win32_System_Registry",
"Win32_Foundation",
"Win32_Security",
"Win32_System_Threading"
]
32 changes: 16 additions & 16 deletions examples/private-hive.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use registry::{Hive, Security};
use std::convert::TryInto;
use utfx::U16CString;
use winapi::um::processthreadsapi::GetCurrentProcess;
use winapi::um::winnt::TOKEN_ADJUST_PRIVILEGES;
use winapi::{
shared::ntdef::LUID,
um::{
processthreadsapi::OpenProcessToken,
securitybaseapi::AdjustTokenPrivileges,
winbase::LookupPrivilegeValueW,
winnt::LUID_AND_ATTRIBUTES,
winnt::{HANDLE, SE_BACKUP_NAME, SE_PRIVILEGE_ENABLED, SE_RESTORE_NAME, TOKEN_PRIVILEGES},
use windows::Win32::{System::Threading::GetCurrentProcess,
Security::{
SE_PRIVILEGE_ENABLED, TOKEN_PRIVILEGES, TOKEN_ADJUST_PRIVILEGES, LUID_AND_ATTRIBUTES,
AdjustTokenPrivileges, LookupPrivilegeValueW
},
Foundation::{LUID, HANDLE, PWSTR},
System::Threading::OpenProcessToken
};

const SE_BACKUP_NAME: &'static str = "SeBackupPrivilege";
const SE_RESTORE_NAME: &'static str = "SeRestorePrivilege";

fn main() -> Result<(), std::io::Error> {
let mut token = std::ptr::null_mut();
let mut token = HANDLE::default();
let r = unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &mut token) };
if r == 0 {
if r == false {
return Err(std::io::Error::last_os_error());
}

Expand All @@ -40,8 +40,8 @@ fn set_privilege(handle: HANDLE, name: &str) -> Result<(), std::io::Error> {
HighPart: 0,
};
let name: U16CString = name.try_into().unwrap();
let r = unsafe { LookupPrivilegeValueW(std::ptr::null(), name.as_ptr(), &mut luid) };
if r == 0 {
let r = unsafe { LookupPrivilegeValueW(PWSTR::default(), PWSTR(name.as_ptr() as *mut u16), &mut luid) };
if r == false {
return Err(std::io::Error::last_os_error());
}

Expand All @@ -56,15 +56,15 @@ fn set_privilege(handle: HANDLE, name: &str) -> Result<(), std::io::Error> {
let r = unsafe {
AdjustTokenPrivileges(
handle,
false as i32,
false,
&mut privilege,
std::mem::size_of::<TOKEN_PRIVILEGES>() as u32,
std::ptr::null_mut(),
std::ptr::null_mut(),
)
};

if r == 0 {
if r == false {
return Err(std::io::Error::last_os_error());
}
Ok(())
Expand Down
18 changes: 10 additions & 8 deletions src/hive.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{convert::TryInto, fmt::Display};

use utfx::{U16CStr, U16CString};
use winapi::um::winreg::{
HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_CURRENT_USER_LOCAL_SETTINGS,
HKEY_LOCAL_MACHINE, HKEY_PERFORMANCE_DATA, HKEY_USERS,
use windows::Win32::{
Foundation::PWSTR,
System::Registry::{
HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_CURRENT_USER_LOCAL_SETTINGS,
HKEY_LOCAL_MACHINE, HKEY_PERFORMANCE_DATA, HKEY_USERS, HKEY, RegLoadAppKeyW
}
};
use winapi::{shared::minwindef::HKEY, um::winreg::RegLoadAppKeyW};

use crate::key::{self, Error};
use crate::{sec::Security, RegKey};
Expand Down Expand Up @@ -131,12 +133,12 @@ where
P: AsRef<U16CStr>,
{
let path = path.as_ref();
let mut hkey = std::ptr::null_mut();
let result = unsafe { RegLoadAppKeyW(path.as_ptr(), &mut hkey, sec.bits(), 0, 0) };
let mut hkey = HKEY::default();
let result = unsafe { RegLoadAppKeyW(PWSTR(path.as_ptr() as *mut u16), &mut hkey, sec.bits(), 0, 0) };

if result == 0 {
if result.0 == 0 {
return Ok(hkey);
}

Err(std::io::Error::from_raw_os_error(result))
Err(std::io::Error::from_raw_os_error(result.0))
}
21 changes: 11 additions & 10 deletions src/iter/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::{
};

use utfx::{U16CString, U16String};
use winapi::shared::winerror::ERROR_NO_MORE_ITEMS;
use winapi::um::winreg::{RegEnumKeyExW, RegQueryInfoKeyW};

use windows::Win32::{
Foundation::{PWSTR, ERROR_NO_MORE_ITEMS},
System::Registry::{RegEnumKeyExW, RegQueryInfoKeyW}
};
use crate::key::RegKey;
use crate::sec::Security;

Expand Down Expand Up @@ -84,22 +85,22 @@ impl<'a> Iterator for Keys<'a> {
RegEnumKeyExW(
self.regkey.handle,
self.index,
self.buf.as_mut_ptr(),
PWSTR(self.buf.as_mut_ptr()),
&mut len,
null_mut(),
null_mut(),
PWSTR::default(),
null_mut(),
null_mut(),
)
};

if result == ERROR_NO_MORE_ITEMS as i32 {
if result.0 == ERROR_NO_MORE_ITEMS.0 as i32 {
return None;
}

self.index += 1;

if result != 0 {
if result.0 != 0 {
// TODO: don't panic
panic!();
}
Expand All @@ -124,7 +125,7 @@ impl<'a> Keys<'a> {
let result = unsafe {
RegQueryInfoKeyW(
regkey.handle,
null_mut(),
PWSTR::default(),
null_mut(),
null_mut(),
null_mut(), // &mut subkeys_len,
Expand All @@ -138,14 +139,14 @@ impl<'a> Keys<'a> {
)
};

if result == 0 {
if result.0 == 0 {
return Ok(Keys {
regkey,
buf: vec![0u16; subkeys_max_str_len as usize + 1],
index: 0,
});
}

Err(std::io::Error::from_raw_os_error(result))
Err(std::io::Error::from_raw_os_error(result.0))
}
}
20 changes: 11 additions & 9 deletions src/iter/values.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{convert::TryInto, fmt::Debug, ptr::null_mut};

use utfx::{U16CStr, U16CString};
use winapi::shared::winerror::ERROR_NO_MORE_ITEMS;
use winapi::um::winreg::{RegEnumValueW, RegQueryInfoKeyW};
use windows::Win32::{
Foundation::{PWSTR, ERROR_NO_MORE_ITEMS},
System::Registry::{RegQueryInfoKeyW, RegEnumValueW}
};

use crate::{key::RegKey, Data};

Expand Down Expand Up @@ -108,7 +110,7 @@ impl<'a> Iterator for Values<'a> {
RegEnumValueW(
self.regkey.handle,
self.index,
self.name_buf.as_mut_ptr(),
PWSTR(self.name_buf.as_mut_ptr()),
&mut name_len,
null_mut(),
&mut data_type,
Expand All @@ -117,14 +119,14 @@ impl<'a> Iterator for Values<'a> {
)
};

if result == ERROR_NO_MORE_ITEMS as i32 {
if result.0 == ERROR_NO_MORE_ITEMS.0 as i32 {
return None;
}

if result != 0 {
if result.0 != 0 {
return Some(Err(Error::Unknown(
self.index,
std::io::Error::from_raw_os_error(result),
std::io::Error::from_raw_os_error(result.0),
)));
}

Expand Down Expand Up @@ -157,7 +159,7 @@ impl<'a> Values<'a> {
let result = unsafe {
RegQueryInfoKeyW(
regkey.handle,
null_mut(),
PWSTR::default(),
null_mut(),
null_mut(),
null_mut(),
Expand All @@ -171,7 +173,7 @@ impl<'a> Values<'a> {
)
};

if result == 0 {
if result.0 == 0 {
return Ok(Values {
regkey,
name_buf: vec![0u16; max_value_name_len as usize + 1],
Expand All @@ -180,6 +182,6 @@ impl<'a> Values<'a> {
});
}

Err(std::io::Error::from_raw_os_error(result))
Err(std::io::Error::from_raw_os_error(result.0))
}
}
53 changes: 27 additions & 26 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::{
convert::{Infallible, TryInto},
fmt::Display,
io,
ptr::null_mut,
};

use utfx::{U16CStr, U16CString};
use winapi::shared::minwindef::HKEY;
use winapi::um::winreg::{
RegCloseKey, RegCreateKeyExW, RegDeleteKeyW, RegDeleteTreeW, RegOpenCurrentUser, RegOpenKeyExW,
RegSaveKeyExW,
use windows::Win32::{
Foundation::PWSTR,
System::Registry::{
REG_SAM_FLAGS, REG_SAVE_FORMAT, REG_OPEN_CREATE_OPTIONS, HKEY, RegCloseKey, RegCreateKeyExW,
RegDeleteKeyW, RegDeleteTreeW, RegOpenCurrentUser, RegOpenKeyExW, RegSaveKeyExW
}
};

use crate::iter;
Expand Down Expand Up @@ -199,11 +200,11 @@ impl RegKey {
}

pub fn open_current_user(sec: Security) -> Result<RegKey, Error> {
let mut hkey = null_mut();
let mut hkey = HKEY::default();

let result = unsafe { RegOpenCurrentUser(sec.bits(), &mut hkey) };

if result == 0 {
if result.0 == 0 {
// TODO: use NT API to query path
return Ok(RegKey {
hive: Hive::CurrentUser,
Expand All @@ -213,7 +214,7 @@ impl RegKey {
}

let path = "<current user>".to_string();
Err(Error::from_code(result, path))
Err(Error::from_code(result.0, path))
}
}

Expand All @@ -223,15 +224,15 @@ where
P: AsRef<U16CStr>,
{
let path = path.as_ref();
let mut hkey = std::ptr::null_mut();
let result = unsafe { RegOpenKeyExW(base, path.as_ptr(), 0, sec.bits(), &mut hkey) };
let mut hkey = HKEY::default();
let result = unsafe { RegOpenKeyExW(base, PWSTR(path.as_ptr() as *mut u16), 0, REG_SAM_FLAGS(sec.bits()), &mut hkey) };

if result == 0 {
if result.0 == 0 {
return Ok(hkey);
}

let path = path.to_string_lossy();
Err(Error::from_code(result, path))
Err(Error::from_code(result.0, path))
}

#[inline]
Expand All @@ -240,14 +241,14 @@ where
P: AsRef<U16CStr>,
{
let path = path.as_ref();
let result = unsafe { RegSaveKeyExW(hkey, path.as_ptr(), std::ptr::null_mut(), 4) };
let result = unsafe { RegSaveKeyExW(hkey, PWSTR(path.as_ptr() as *mut u16), std::ptr::null_mut(), REG_SAVE_FORMAT(4)) };

if result == 0 {
if result.0 == 0 {
return Ok(());
}

let path = path.to_string_lossy();
Err(Error::from_code(result, path))
Err(Error::from_code(result.0, path))
}

#[inline]
Expand All @@ -258,17 +259,17 @@ where
let path = path.as_ref();

let result = if is_recursive {
unsafe { RegDeleteTreeW(base, path.as_ptr()) }
unsafe { RegDeleteTreeW(base, PWSTR(path.as_ptr() as *mut u16)) }
} else {
unsafe { RegDeleteKeyW(base, path.as_ptr()) }
unsafe { RegDeleteKeyW(base, PWSTR(path.as_ptr() as *mut u16)) }
};

if result == 0 {
if result.0 == 0 {
return Ok(());
}

let path = path.to_string_lossy();
Err(Error::from_code(result, path))
Err(Error::from_code(result.0, path))
}

#[inline]
Expand All @@ -277,27 +278,27 @@ where
P: AsRef<U16CStr>,
{
let path = path.as_ref();
let mut hkey = std::ptr::null_mut();
let mut hkey = HKEY::default();
let result = unsafe {
RegCreateKeyExW(
base,
path.as_ptr(),
0,
std::ptr::null_mut(),
PWSTR(path.as_ptr() as *mut u16),
0,
sec.bits(),
PWSTR::default(),
REG_OPEN_CREATE_OPTIONS(0),
REG_SAM_FLAGS(sec.bits()),
std::ptr::null_mut(),
&mut hkey,
std::ptr::null_mut(),
)
};

if result == 0 {
if result.0 == 0 {
return Ok(hkey);
}

let path = path.to_string_lossy();
Err(Error::from_code(result, path))
Err(Error::from_code(result.0, path))
}

#[cfg(test)]
Expand Down