Skip to content

Commit 2d9941b

Browse files
authored
Cleanup mod com, registry, setup_config and winapi (#828)
1 parent c83f5ee commit 2d9941b

File tree

5 files changed

+89
-184
lines changed

5 files changed

+89
-184
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobserver = { version = "0.1.16", optional = true }
2424
libc = "0.2.62"
2525

2626
[target.'cfg(windows)'.dependencies]
27-
windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_Security"] }
27+
windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_Security", "Win32_System_Com", "Win32_System_Registry"] }
2828

2929
[features]
3030
parallel = ["jobserver"]

src/com.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@
77

88
#![allow(unused)]
99

10-
use crate::winapi::CoInitializeEx;
11-
use crate::winapi::IUnknown;
12-
use crate::winapi::Interface;
13-
use crate::winapi::BSTR;
14-
use crate::winapi::COINIT_MULTITHREADED;
15-
use crate::winapi::{SysFreeString, SysStringLen};
16-
use crate::winapi::{HRESULT, S_FALSE, S_OK};
17-
use std::ffi::{OsStr, OsString};
18-
use std::mem::forget;
19-
use std::ops::Deref;
20-
use std::os::windows::ffi::{OsStrExt, OsStringExt};
21-
use std::ptr::null_mut;
22-
use std::slice::from_raw_parts;
10+
use crate::winapi::{IUnknown, Interface};
11+
use std::{
12+
ffi::{OsStr, OsString},
13+
mem::ManuallyDrop,
14+
ops::Deref,
15+
os::windows::ffi::{OsStrExt, OsStringExt},
16+
ptr::{null, null_mut},
17+
slice::from_raw_parts,
18+
};
19+
use windows_sys::{
20+
core::{BSTR, HRESULT},
21+
Win32::{
22+
Foundation::{SysFreeString, SysStringLen, S_FALSE, S_OK},
23+
System::Com::{CoInitializeEx, COINIT_MULTITHREADED},
24+
},
25+
};
2326

2427
pub fn initialize() -> Result<(), HRESULT> {
25-
let err = unsafe { CoInitializeEx(null_mut(), COINIT_MULTITHREADED) };
28+
let err = unsafe { CoInitializeEx(null(), COINIT_MULTITHREADED) };
2629
if err != S_OK && err != S_FALSE {
2730
// S_FALSE just means COM is already initialized
28-
return Err(err);
31+
Err(err)
32+
} else {
33+
Ok(())
2934
}
30-
Ok(())
3135
}
3236

3337
pub struct ComPtr<T>(*mut T)
@@ -55,9 +59,7 @@ where
5559
/// Extracts the raw pointer.
5660
/// You are now responsible for releasing it yourself.
5761
pub fn into_raw(self) -> *mut T {
58-
let p = self.0;
59-
forget(self);
60-
p
62+
ManuallyDrop::new(self).0
6163
}
6264
/// For internal use only.
6365
fn as_unknown(&self) -> &IUnknown {

src/registry.rs

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,25 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::ffi::{OsStr, OsString};
12-
use std::io;
13-
use std::ops::RangeFrom;
14-
use std::os::raw;
15-
use std::os::windows::prelude::*;
11+
use std::{
12+
ffi::{OsStr, OsString},
13+
io,
14+
ops::RangeFrom,
15+
os::windows::prelude::*,
16+
ptr::null_mut,
17+
};
18+
use windows_sys::Win32::{
19+
Foundation::{ERROR_NO_MORE_ITEMS, ERROR_SUCCESS},
20+
System::Registry::{
21+
RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, HKEY, HKEY_LOCAL_MACHINE,
22+
KEY_READ, KEY_WOW64_32KEY, REG_SZ,
23+
},
24+
};
1625

1726
/// Must never be `HKEY_PERFORMANCE_DATA`.
1827
pub(crate) struct RegistryKey(Repr);
1928

20-
type HKEY = *mut u8;
2129
type DWORD = u32;
22-
type LPDWORD = *mut DWORD;
23-
type LPCWSTR = *const u16;
24-
type LPWSTR = *mut u16;
25-
type LONG = raw::c_long;
26-
type PHKEY = *mut HKEY;
27-
type PFILETIME = *mut u8;
28-
type LPBYTE = *mut u8;
29-
type REGSAM = u32;
30-
31-
const ERROR_SUCCESS: DWORD = 0;
32-
const ERROR_NO_MORE_ITEMS: DWORD = 259;
33-
// Sign-extend into 64 bits if needed.
34-
const HKEY_LOCAL_MACHINE: HKEY = 0x80000002u32 as i32 as isize as HKEY;
35-
const REG_SZ: DWORD = 1;
36-
const KEY_READ: DWORD = 0x20019;
37-
const KEY_WOW64_32KEY: DWORD = 0x200;
38-
39-
#[link(name = "advapi32")]
40-
extern "system" {
41-
fn RegOpenKeyExW(
42-
key: HKEY,
43-
lpSubKey: LPCWSTR,
44-
ulOptions: DWORD,
45-
samDesired: REGSAM,
46-
phkResult: PHKEY,
47-
) -> LONG;
48-
fn RegEnumKeyExW(
49-
key: HKEY,
50-
dwIndex: DWORD,
51-
lpName: LPWSTR,
52-
lpcName: LPDWORD,
53-
lpReserved: LPDWORD,
54-
lpClass: LPWSTR,
55-
lpcClass: LPDWORD,
56-
lpftLastWriteTime: PFILETIME,
57-
) -> LONG;
58-
fn RegQueryValueExW(
59-
hKey: HKEY,
60-
lpValueName: LPCWSTR,
61-
lpReserved: LPDWORD,
62-
lpType: LPDWORD,
63-
lpData: LPBYTE,
64-
lpcbData: LPDWORD,
65-
) -> LONG;
66-
fn RegCloseKey(hKey: HKEY) -> LONG;
67-
}
6830

6931
struct OwnedKey(HKEY);
7032

@@ -97,7 +59,7 @@ impl RegistryKey {
9759
/// Open a sub-key of `self`.
9860
pub fn open(&self, key: &OsStr) -> io::Result<RegistryKey> {
9961
let key = key.encode_wide().chain(Some(0)).collect::<Vec<_>>();
100-
let mut ret = 0 as *mut _;
62+
let mut ret = 0;
10163
let err = unsafe {
10264
RegOpenKeyExW(
10365
self.raw(),
@@ -107,7 +69,7 @@ impl RegistryKey {
10769
&mut ret,
10870
)
10971
};
110-
if err == ERROR_SUCCESS as LONG {
72+
if err == ERROR_SUCCESS {
11173
Ok(RegistryKey(Repr::Owned(OwnedKey(ret))))
11274
} else {
11375
Err(io::Error::from_raw_os_error(err as i32))
@@ -130,12 +92,12 @@ impl RegistryKey {
13092
let err = RegQueryValueExW(
13193
self.raw(),
13294
name.as_ptr(),
133-
0 as *mut _,
95+
null_mut(),
13496
&mut kind,
135-
0 as *mut _,
97+
null_mut(),
13698
&mut len,
13799
);
138-
if err != ERROR_SUCCESS as LONG {
100+
if err != ERROR_SUCCESS {
139101
return Err(io::Error::from_raw_os_error(err as i32));
140102
}
141103
if kind != REG_SZ {
@@ -156,16 +118,16 @@ impl RegistryKey {
156118
let err = RegQueryValueExW(
157119
self.raw(),
158120
name.as_ptr(),
159-
0 as *mut _,
160-
0 as *mut _,
121+
null_mut(),
122+
null_mut(),
161123
v.as_mut_ptr() as *mut _,
162124
&mut len,
163125
);
164126
// We don't check for `ERROR_MORE_DATA` (which would if the value
165127
// grew between the first and second call to `RegQueryValueExW`),
166128
// both because it's extremely unlikely, and this is a bit more
167129
// defensive more defensive against weird types of registry keys.
168-
if err != ERROR_SUCCESS as LONG {
130+
if err != ERROR_SUCCESS {
169131
return Err(io::Error::from_raw_os_error(err as i32));
170132
}
171133
// The length is allowed to change, but should still be even, as
@@ -213,14 +175,14 @@ impl<'a> Iterator for Iter<'a> {
213175
i,
214176
v.as_mut_ptr(),
215177
&mut len,
216-
0 as *mut _,
217-
0 as *mut _,
218-
0 as *mut _,
219-
0 as *mut _,
178+
null_mut(),
179+
null_mut(),
180+
null_mut(),
181+
null_mut(),
220182
);
221-
if ret == ERROR_NO_MORE_ITEMS as LONG {
183+
if ret == ERROR_NO_MORE_ITEMS {
222184
None
223-
} else if ret != ERROR_SUCCESS as LONG {
185+
} else if ret != ERROR_SUCCESS {
224186
Some(Err(io::Error::from_raw_os_error(ret as i32)))
225187
} else {
226188
v.set_len(len as usize);

src/setup_config.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@
88
#![allow(bad_style)]
99
#![allow(unused)]
1010

11-
use crate::winapi::Interface;
12-
use crate::winapi::BSTR;
13-
use crate::winapi::LPCOLESTR;
14-
use crate::winapi::LPSAFEARRAY;
15-
use crate::winapi::S_FALSE;
16-
use crate::winapi::{CoCreateInstance, CLSCTX_ALL};
17-
use crate::winapi::{IUnknown, IUnknownVtbl};
18-
use crate::winapi::{HRESULT, LCID, LPCWSTR, PULONGLONG};
19-
use crate::winapi::{LPFILETIME, ULONG};
20-
use std::ffi::OsString;
21-
use std::ptr::null_mut;
11+
use crate::{
12+
com::{BStr, ComPtr},
13+
winapi::{
14+
IUnknown, IUnknownVtbl, Interface, LCID, LPCOLESTR, LPCWSTR, LPFILETIME, LPSAFEARRAY,
15+
PULONGLONG, ULONG,
16+
},
17+
};
2218

23-
use crate::com::{BStr, ComPtr};
19+
use std::{
20+
ffi::OsString,
21+
ptr::{null, null_mut},
22+
};
23+
use windows_sys::{
24+
core::{BSTR, HRESULT},
25+
Win32::{
26+
Foundation::S_FALSE,
27+
System::Com::{CoCreateInstance, CLSCTX_ALL},
28+
},
29+
};
2430

2531
// Bindings to the Setup.Configuration stuff
2632
pub type InstanceState = u32;
@@ -212,7 +218,7 @@ impl SetupInstance {
212218
SetupInstance(ComPtr::from_raw(obj))
213219
}
214220
pub fn instance_id(&self) -> Result<OsString, i32> {
215-
let mut s = null_mut();
221+
let mut s = null();
216222
let err = unsafe { self.0.GetInstanceId(&mut s) };
217223
let bstr = unsafe { BStr::from_raw(s) };
218224
if err < 0 {
@@ -221,7 +227,7 @@ impl SetupInstance {
221227
Ok(bstr.to_osstring())
222228
}
223229
pub fn installation_name(&self) -> Result<OsString, i32> {
224-
let mut s = null_mut();
230+
let mut s = null();
225231
let err = unsafe { self.0.GetInstallationName(&mut s) };
226232
let bstr = unsafe { BStr::from_raw(s) };
227233
if err < 0 {
@@ -230,7 +236,7 @@ impl SetupInstance {
230236
Ok(bstr.to_osstring())
231237
}
232238
pub fn installation_path(&self) -> Result<OsString, i32> {
233-
let mut s = null_mut();
239+
let mut s = null();
234240
let err = unsafe { self.0.GetInstallationPath(&mut s) };
235241
let bstr = unsafe { BStr::from_raw(s) };
236242
if err < 0 {
@@ -239,7 +245,7 @@ impl SetupInstance {
239245
Ok(bstr.to_osstring())
240246
}
241247
pub fn installation_version(&self) -> Result<OsString, i32> {
242-
let mut s = null_mut();
248+
let mut s = null();
243249
let err = unsafe { self.0.GetInstallationVersion(&mut s) };
244250
let bstr = unsafe { BStr::from_raw(s) };
245251
if err < 0 {
@@ -248,7 +254,7 @@ impl SetupInstance {
248254
Ok(bstr.to_osstring())
249255
}
250256
pub fn product_path(&self) -> Result<OsString, i32> {
251-
let mut s = null_mut();
257+
let mut s = null();
252258
let this = self.0.cast::<ISetupInstance2>()?;
253259
let err = unsafe { this.GetProductPath(&mut s) };
254260
let bstr = unsafe { BStr::from_raw(s) };

0 commit comments

Comments
 (0)