8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
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
+ } ;
16
25
17
26
/// Must never be `HKEY_PERFORMANCE_DATA`.
18
27
pub ( crate ) struct RegistryKey ( Repr ) ;
19
28
20
- type HKEY = * mut u8 ;
21
29
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
- }
68
30
69
31
struct OwnedKey ( HKEY ) ;
70
32
@@ -97,7 +59,7 @@ impl RegistryKey {
97
59
/// Open a sub-key of `self`.
98
60
pub fn open ( & self , key : & OsStr ) -> io:: Result < RegistryKey > {
99
61
let key = key. encode_wide ( ) . chain ( Some ( 0 ) ) . collect :: < Vec < _ > > ( ) ;
100
- let mut ret = 0 as * mut _ ;
62
+ let mut ret = 0 ;
101
63
let err = unsafe {
102
64
RegOpenKeyExW (
103
65
self . raw ( ) ,
@@ -107,7 +69,7 @@ impl RegistryKey {
107
69
& mut ret,
108
70
)
109
71
} ;
110
- if err == ERROR_SUCCESS as LONG {
72
+ if err == ERROR_SUCCESS {
111
73
Ok ( RegistryKey ( Repr :: Owned ( OwnedKey ( ret) ) ) )
112
74
} else {
113
75
Err ( io:: Error :: from_raw_os_error ( err as i32 ) )
@@ -130,12 +92,12 @@ impl RegistryKey {
130
92
let err = RegQueryValueExW (
131
93
self . raw ( ) ,
132
94
name. as_ptr ( ) ,
133
- 0 as * mut _ ,
95
+ null_mut ( ) ,
134
96
& mut kind,
135
- 0 as * mut _ ,
97
+ null_mut ( ) ,
136
98
& mut len,
137
99
) ;
138
- if err != ERROR_SUCCESS as LONG {
100
+ if err != ERROR_SUCCESS {
139
101
return Err ( io:: Error :: from_raw_os_error ( err as i32 ) ) ;
140
102
}
141
103
if kind != REG_SZ {
@@ -156,16 +118,16 @@ impl RegistryKey {
156
118
let err = RegQueryValueExW (
157
119
self . raw ( ) ,
158
120
name. as_ptr ( ) ,
159
- 0 as * mut _ ,
160
- 0 as * mut _ ,
121
+ null_mut ( ) ,
122
+ null_mut ( ) ,
161
123
v. as_mut_ptr ( ) as * mut _ ,
162
124
& mut len,
163
125
) ;
164
126
// We don't check for `ERROR_MORE_DATA` (which would if the value
165
127
// grew between the first and second call to `RegQueryValueExW`),
166
128
// both because it's extremely unlikely, and this is a bit more
167
129
// defensive more defensive against weird types of registry keys.
168
- if err != ERROR_SUCCESS as LONG {
130
+ if err != ERROR_SUCCESS {
169
131
return Err ( io:: Error :: from_raw_os_error ( err as i32 ) ) ;
170
132
}
171
133
// The length is allowed to change, but should still be even, as
@@ -213,14 +175,14 @@ impl<'a> Iterator for Iter<'a> {
213
175
i,
214
176
v. as_mut_ptr ( ) ,
215
177
& 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 ( ) ,
220
182
) ;
221
- if ret == ERROR_NO_MORE_ITEMS as LONG {
183
+ if ret == ERROR_NO_MORE_ITEMS {
222
184
None
223
- } else if ret != ERROR_SUCCESS as LONG {
185
+ } else if ret != ERROR_SUCCESS {
224
186
Some ( Err ( io:: Error :: from_raw_os_error ( ret as i32 ) ) )
225
187
} else {
226
188
v. set_len ( len as usize ) ;
0 commit comments