winsafe 0.0.27

Windows API and GUI in safe, idiomatic Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#![allow(non_camel_case_types, non_snake_case)]

use crate::advapi::{ffi, iterators::*};
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::privs::*;
use crate::prelude::*;

handle! { HKEY;
	/// Handle to a
	/// [registry key](https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types#hkey).
	///
	/// This handle also exposes several
	/// [predefined registry keys](https://learn.microsoft.com/en-us/windows/win32/sysinfo/predefined-keys),
	/// like `HKEY::CURRENT_USER`, which are always open and ready to be used.
	/// Usually, they are the starting point to open a registry key.
}

macro_rules! predef_key {
	($name:ident, $val:expr) => {
		/// Predefined registry key, always open.
		pub const $name: HKEY = HKEY($val as *mut _);
	};
}

impl HKEY {
	predef_key!(CLASSES_ROOT, 0x8000_0000);
	predef_key!(CURRENT_USER, 0x8000_0001);
	predef_key!(LOCAL_MACHINE, 0x8000_0002);
	predef_key!(USERS, 0x8000_0003);
	predef_key!(PERFORMANCE_DATA, 0x8000_0004);
	predef_key!(CURRENT_CONFIG, 0x8000_0005);
	predef_key!(DYN_DATA, 0x8000_0006);
	predef_key!(CURRENT_USER_LOCAL_SETTINGS, 0x8000_0007);
	predef_key!(PERFORMANCE_TEXT, 0x8000_0050);
	predef_key!(PERFORMANCE_NLSTEXT, 0x8000_0060);

	pub(in crate::advapi) fn is_predef_key(&self) -> bool {
		// Note that we are not constructing HKEY objects, so no drop() is called.
		(self.0 as usize) >= (Self::CLASSES_ROOT.0 as usize)
			&& (self.0 as usize) <= (Self::PERFORMANCE_NLSTEXT.0 as usize)
	}

	/// [`RegConnectRegistry`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regconnectregistryw)
	/// function.
	///
	/// # Panics
	///
	/// Panics if `predef_key` is different from:
	///
	/// - [`HKEY::LOCAL_MACHINE`](crate::HKEY::LOCAL_MACHINE);
	/// - [`HKEY::PERFORMANCE_DATA`](crate::HKEY::PERFORMANCE_DATA);
	/// - [`HKEY::USERS`](crate::HKEY::USERS).
	#[must_use]
	pub fn RegConnectRegistry(
		machine_name: Option<&str>,
		predef_hkey: &HKEY,
	) -> SysResult<RegCloseKeyGuard> {
		if *predef_hkey != HKEY::LOCAL_MACHINE
			&& *predef_hkey != HKEY::PERFORMANCE_DATA
			&& *predef_hkey != HKEY::USERS
		{
			panic!("Invalid predef_key.");
		}

		let mut hkey = HKEY::NULL;
		unsafe {
			ErrorRet(ffi::RegConnectRegistryW(
				WString::from_opt_str(machine_name).as_ptr(),
				predef_hkey.ptr(),
				hkey.as_mut(),
			))
			.to_sysresult()
			.map(|_| RegCloseKeyGuard::new(hkey))
		}
	}

	/// [`RegCopyTree`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regcopytreew)
	/// function.
	pub fn RegCopyTree(&self, sub_key: Option<&str>, dest: &HKEY) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegCopyTreeW(self.ptr(), WString::from_opt_str(sub_key).as_ptr(), dest.ptr())
		})
		.to_sysresult()
	}

	/// [`RegCreateKeyEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regcreatekeyexw)
	/// function.
	#[must_use]
	pub fn RegCreateKeyEx(
		&self,
		sub_key: &str,
		class: Option<&str>,
		options: co::REG_OPTION,
		access_rights: co::KEY,
		security_attributes: Option<&SECURITY_ATTRIBUTES>,
	) -> SysResult<(RegCloseKeyGuard, co::REG_DISPOSITION)> {
		let mut hkey = HKEY::NULL;
		let mut disposition = co::REG_DISPOSITION::default();

		unsafe {
			ErrorRet(ffi::RegCreateKeyExW(
				self.ptr(),
				WString::from_str(sub_key).as_ptr(),
				0,
				WString::from_opt_str(class).as_ptr(),
				options.raw(),
				access_rights.raw(),
				pcvoid_or_null(security_attributes),
				hkey.as_mut(),
				disposition.as_mut(),
			))
			.to_sysresult()
			.map(|_| (RegCloseKeyGuard::new(hkey), disposition))
		}
	}

	/// [`RegCreateKeyTransacted`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regcreatekeytransactedw)
	/// function.
	#[must_use]
	pub fn RegCreateKeyTransacted(
		&self,
		sub_key: &str,
		class: Option<&str>,
		options: co::REG_OPTION,
		access_rights: co::KEY,
		security_attributes: Option<&SECURITY_ATTRIBUTES>,
		htransaction: &HTRANSACTION,
	) -> SysResult<(RegCloseKeyGuard, co::REG_DISPOSITION)> {
		let mut hkey = HKEY::NULL;
		let mut disposition = co::REG_DISPOSITION::default();

		unsafe {
			ErrorRet(ffi::RegCreateKeyTransactedW(
				self.ptr(),
				WString::from_str(sub_key).as_ptr(),
				0,
				WString::from_opt_str(class).as_ptr(),
				options.raw(),
				access_rights.raw(),
				pcvoid_or_null(security_attributes),
				hkey.as_mut(),
				disposition.as_mut(),
				htransaction.ptr(),
				std::ptr::null_mut(),
			))
			.to_sysresult()
			.map(|_| (RegCloseKeyGuard::new(hkey), disposition))
		}
	}

	/// [`RegDeleteKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regdeletekeyw)
	/// function.
	pub fn RegDeleteKey(&self, sub_key: &str) -> SysResult<()> {
		ErrorRet(unsafe { ffi::RegDeleteKeyW(self.ptr(), WString::from_str(sub_key).as_ptr()) })
			.to_sysresult()
	}

	/// [`RegDeleteKeyEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regdeletekeyexw)
	/// function.
	///
	/// # Panics
	///
	/// Panics if `platform_view` is different from
	/// [`co::KEY::WOW64_32KEY`](crate::co::KEY::WOW64_32KEY) and
	/// [`co::KEY::WOW64_64KEY`](crate::co::KEY::WOW64_64KEY).
	pub fn RegDeleteKeyEx(&self, sub_key: &str, platform_view: co::KEY) -> SysResult<()> {
		if platform_view != co::KEY::WOW64_32KEY && platform_view != co::KEY::WOW64_64KEY {
			panic!("Platform view must be co::KEY::WOW64_32KEY or co::KEY::WOW64_64KEY");
		}

		ErrorRet(unsafe {
			ffi::RegDeleteKeyExW(
				self.ptr(),
				WString::from_str(sub_key).as_ptr(),
				platform_view.raw(),
				0,
			)
		})
		.to_sysresult()
	}

	/// [`RegDeleteKeyTransacted`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regdeletekeytransactedw)
	/// function.
	pub fn RegDeleteKeyTransacted(
		&self,
		sub_key: &str,
		access_rights: co::KEY,
		htransaction: &HTRANSACTION,
	) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegDeleteKeyTransactedW(
				self.ptr(),
				WString::from_str(sub_key).as_ptr(),
				access_rights.raw(),
				0,
				htransaction.ptr(),
				std::ptr::null_mut(),
			)
		})
		.to_sysresult()
	}

	/// [`RegDeleteTree`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regdeletetreew)
	/// function.
	pub fn RegDeleteTree(&self, sub_key: Option<&str>) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegDeleteTreeW(self.ptr(), WString::from_opt_str(sub_key).as_ptr())
		})
		.to_sysresult()
	}

	/// [`RegDeleteValue`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regdeletevaluew)
	/// function.
	pub fn RegDeleteValue(&self, value_name: Option<&str>) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegDeleteValueW(self.ptr(), WString::from_opt_str(value_name).as_ptr())
		})
		.to_sysresult()
	}

	/// [`RegDisableReflectionKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regdisablereflectionkey)
	/// function.
	pub fn RegDisableReflectionKey(&self) -> SysResult<()> {
		ErrorRet(unsafe { ffi::RegDisableReflectionKey(self.ptr()) }).to_sysresult()
	}

	/// [`RegEnableReflectionKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regenablereflectionkey)
	/// function.
	pub fn RegEnableReflectionKey(&self) -> SysResult<()> {
		ErrorRet(unsafe { ffi::RegEnableReflectionKey(self.ptr()) }).to_sysresult()
	}

	/// Returns an iterator over the names of the keys, which calls
	/// [`RegEnumKeyEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regenumkeyexw)
	/// repeatedly.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let hkey = w::HKEY::CURRENT_USER.RegOpenKeyEx(
	///     Some("Control Panel"),
	///     co::REG_OPTION::default(),
	///     co::KEY::READ,
	/// )?;
	///
	/// for key_name in hkey.RegEnumKeyEx()? {
	///     let key_name = key_name?;
	///     println!("{}", key_name);
	/// }
	///
	/// // Collecting into a Vec
	/// let names: Vec<String> =
	///     hkey.RegEnumKeyEx()?
	///         .collect::<w::SysResult<Vec<_>>>()?;
	/// # w::SysResult::Ok(())
	/// ```
	#[must_use]
	pub fn RegEnumKeyEx(
		&self,
	) -> SysResult<impl DoubleEndedIterator<Item = SysResult<String>> + '_> {
		Ok(HkeyKeyIter::new(self)?)
	}

	/// Returns an iterator of the names and types of the values, which calls
	/// [`RegEnumValue`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regenumvaluew)
	/// repeatedly.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let hkey = w::HKEY::CURRENT_USER.RegOpenKeyEx(
	///     Some("Control Panel\\Appearance"),
	///     co::REG_OPTION::default(),
	///     co::KEY::READ,
	/// )?;
	///
	/// for value_and_type in hkey.RegEnumValue()? {
	///     let (value, reg_type) = value_and_type?;
	///     println!("{}, {}", value, reg_type);
	/// }
	///
	/// // Collecting into a Vec
	/// let values_and_types: Vec<(String, co::REG)> =
	///     hkey.RegEnumValue()?
	///         .collect::<w::SysResult<Vec<_>>>()?;
	/// # w::SysResult::Ok(())
	/// ```
	#[must_use]
	pub fn RegEnumValue(
		&self,
	) -> SysResult<impl DoubleEndedIterator<Item = SysResult<(String, co::REG)>> + '_> {
		Ok(HkeyValueIter::new(self)?)
	}

	/// [`RegFlushKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regflushkey)
	/// function.
	pub fn RegFlushKey(&self) -> SysResult<()> {
		ErrorRet(unsafe { ffi::RegFlushKey(self.ptr()) }).to_sysresult()
	}

	/// [`RegGetValue`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-reggetvaluew)
	/// function.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let val = w::HKEY::CURRENT_USER.RegGetValue(
	///     Some("Control Panel\\Mouse"),
	///     Some("Beep"),
	///     co::RRF::RT_ANY,
	/// )?;
	///
	/// println!("{val}");
	/// # w::SysResult::Ok(())
	/// ```
	#[must_use]
	pub fn RegGetValue(
		&self,
		sub_key: Option<&str>,
		value_name: Option<&str>,
		flags: co::RRF,
	) -> SysResult<RegistryValue> {
		let sub_key_w = WString::from_opt_str(sub_key);
		let value_name_w = WString::from_opt_str(value_name);
		let mut buf = Vec::<u8>::default();

		loop {
			let mut data_len = 0u32; // in bytes

			ErrorRet(unsafe {
				ffi::RegGetValueW(
					self.ptr(),
					sub_key_w.as_ptr(),
					value_name_w.as_ptr(),
					flags.raw(),
					std::ptr::null_mut(),
					std::ptr::null_mut(),
					&mut data_len, // first call to retrieve the size only
				)
			})
			.to_sysresult()?;

			buf.resize(data_len as _, 0x00);
			let mut data_type = 0u32;

			match unsafe {
				co::ERROR::from_raw(ffi::RegGetValueW(
					self.ptr(),
					sub_key_w.as_ptr(),
					value_name_w.as_ptr(),
					flags.raw(),
					&mut data_type,
					buf.as_mut_ptr() as _, // second call to retrieve the data
					&mut data_len,
				) as _)
			} {
				co::ERROR::SUCCESS => {
					buf.resize(data_len as _, 0x00); // data length may have shrunk
					return unsafe { RegistryValue::from_raw(buf, co::REG::from_raw(data_type)) };
				},
				co::ERROR::MORE_DATA => continue, // value changed in a concurrent operation; retry
				e => return Err(e),
			}
		}
	}

	/// [`RegLoadKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regloadkeyw)
	/// function.
	pub fn RegLoadKey(&self, sub_key: Option<&str>, file_path: &str) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegLoadKeyW(
				self.ptr(),
				WString::from_opt_str(sub_key).as_ptr(),
				WString::from_str(file_path).as_ptr(),
			)
		})
		.to_sysresult()
	}

	/// [`RegOpenCurrentUser`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regopencurrentuser)
	/// function.
	#[must_use]
	pub fn RegOpenCurrentUser(access_rights: co::KEY) -> SysResult<RegCloseKeyGuard> {
		let mut hkey = HKEY::NULL;
		unsafe {
			ErrorRet(ffi::RegOpenCurrentUser(access_rights.raw(), hkey.as_mut()))
				.to_sysresult()
				.map(|_| RegCloseKeyGuard::new(hkey))
		}
	}

	/// [`RegOpenKeyEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regopenkeyexw)
	/// function.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let hkey = w::HKEY::CURRENT_USER.RegOpenKeyEx(
	///     Some("Control Panel\\Mouse"),
	///     co::REG_OPTION::default(),
	///     co::KEY::READ,
	/// )?;
	/// # w::SysResult::Ok(())
	/// ```
	#[must_use]
	pub fn RegOpenKeyEx(
		&self,
		sub_key: Option<&str>,
		options: co::REG_OPTION,
		access_rights: co::KEY,
	) -> SysResult<RegCloseKeyGuard> {
		let mut hkey = HKEY::NULL;
		unsafe {
			ErrorRet(ffi::RegOpenKeyExW(
				self.ptr(),
				WString::from_opt_str(sub_key).as_ptr(),
				options.raw(),
				access_rights.raw(),
				hkey.as_mut(),
			))
			.to_sysresult()
			.map(|_| RegCloseKeyGuard::new(hkey))
		}
	}

	/// [`RegOpenKeyTransacted`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regopenkeytransactedw)
	/// function.
	#[must_use]
	pub fn RegOpenKeyTransacted(
		&self,
		sub_key: &str,
		options: co::REG_OPTION,
		access_rights: co::KEY,
		htransaction: &HTRANSACTION,
	) -> SysResult<RegCloseKeyGuard> {
		let mut hkey = HKEY::NULL;
		unsafe {
			ErrorRet(ffi::RegOpenKeyTransactedW(
				self.ptr(),
				WString::from_str(sub_key).as_ptr(),
				options.raw(),
				access_rights.raw(),
				hkey.as_mut(),
				htransaction.ptr(),
				std::ptr::null_mut(),
			))
			.to_sysresult()
			.map(|_| RegCloseKeyGuard::new(hkey))
		}
	}

	/// [`RegQueryInfoKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryinfokeyw)
	/// function.
	pub fn RegQueryInfoKey(
		&self,
		mut class: Option<&mut WString>,
		num_sub_keys: Option<&mut u32>,
		max_sub_key_name_len: Option<&mut u32>,
		max_class_len: Option<&mut u32>,
		num_values: Option<&mut u32>,
		max_value_name_len: Option<&mut u32>,
		max_value_len: Option<&mut u32>,
		security_descr_len: Option<&mut u32>,
		last_write_time: Option<&mut FILETIME>,
	) -> SysResult<()> {
		let (mut class_ptr, mut class_len) = match &mut class {
			Some(class) => {
				if class.buf_len() < WString::SSO_LEN {
					// start with no string heap allocation
					**class = WString::new_alloc_buf(WString::SSO_LEN); // make buffer at least this length
				}
				(unsafe { class.as_mut_ptr() }, class.buf_len() as u32)
			},
			None => (std::ptr::null_mut(), 0),
		};

		let num_sub_keys = num_sub_keys.map_or(std::ptr::null_mut(), |re| re as _);
		let max_sub_key_name_len = max_sub_key_name_len.map_or(std::ptr::null_mut(), |re| re as _);
		let max_class_len = max_class_len.map_or(std::ptr::null_mut(), |re| re as _);
		let num_values = num_values.map_or(std::ptr::null_mut(), |re| re as _);
		let max_value_name_len = max_value_name_len.map_or(std::ptr::null_mut(), |re| re as _);
		let max_value_len = max_value_len.map_or(std::ptr::null_mut(), |re| re as _);
		let security_descr_len = security_descr_len.map_or(std::ptr::null_mut(), |re| re as _);
		let last_write_time = last_write_time.map_or(std::ptr::null_mut(), |re| pvoid(re));

		// Loop until class is large enough.
		loop {
			match unsafe {
				co::ERROR::from_raw(ffi::RegQueryInfoKeyW(
					self.ptr(),
					class_ptr,
					&mut class_len,
					std::ptr::null_mut(),
					num_sub_keys,
					max_sub_key_name_len,
					max_class_len,
					num_values,
					max_value_name_len,
					max_value_len,
					security_descr_len,
					last_write_time,
				) as _)
			} {
				co::ERROR::MORE_DATA => match &mut class {
					Some(class) => {
						**class = WString::new_alloc_buf(class.buf_len() * 2); // double the buffer size to try again
						class_ptr = unsafe { class.as_mut_ptr() };
						class_len = class.buf_len() as _;
					},
					None => return Err(co::ERROR::MORE_DATA),
				},
				co::ERROR::SUCCESS => return Ok(()),
				err => return Err(err),
			}
		}
	}

	/// [`RegQueryMultipleValues`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regquerymultiplevaluesw)
	/// function.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let hkey = w::HKEY::CURRENT_USER.RegOpenKeyEx(
	///     Some("Control Panel\\Desktop"),
	///     co::REG_OPTION::default(),
	///     co::KEY::READ,
	/// )?;
	///
	/// for val in hkey.RegQueryMultipleValues(&["DpiScalingVer", "WallPaper"])? {
	///     println!("{val}");
	/// }
	///
	/// # w::SysResult::Ok(())
	/// ```
	#[must_use]
	pub fn RegQueryMultipleValues(
		&self,
		value_names: &[impl AsRef<str>],
	) -> SysResult<Vec<RegistryValue>> {
		let mut valents = vec![VALENT::default(); value_names.len()];
		let value_names_w = value_names
			.iter()
			.map(|value_name| WString::from_str(value_name.as_ref()))
			.collect::<Vec<_>>();
		valents
			.iter_mut()
			.zip(value_names_w.iter())
			.for_each(|(valent, value_name_w)| valent.ve_valuename = value_name_w.as_ptr() as _);
		let mut buf = Vec::<u8>::default();

		loop {
			let mut data_len = 0u32;

			match unsafe {
				co::ERROR::from_raw(ffi::RegQueryMultipleValuesW(
					self.ptr(),
					valents.as_mut_ptr() as _,
					value_names.len() as _,
					std::ptr::null_mut(),
					&mut data_len, // first call to retrieve size only
				) as _)
			} {
				co::ERROR::MORE_DATA => {},
				e => return Err(e),
			}

			buf.resize(data_len as _, 0x00);

			match unsafe {
				co::ERROR::from_raw(ffi::RegQueryMultipleValuesW(
					self.ptr(),
					valents.as_mut_ptr() as _,
					value_names.len() as _,
					buf.as_mut_ptr() as _,
					&mut data_len,
				) as _)
			} {
				co::ERROR::SUCCESS => {
					buf.resize(data_len as _, 0x00); // data length may have shrunk
					return valents
						.iter()
						.map(|valent| unsafe {
							RegistryValue::from_raw(
								valent.buf_projection(&buf).to_vec(),
								valent.ve_type,
							)
						})
						.collect::<SysResult<Vec<_>>>();
				},
				co::ERROR::MORE_DATA => continue, // value changed in a concurrent operation; retry
				e => return Err(e),
			}
		}
	}

	/// [`RegQueryReflectionKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryreflectionkey)
	/// function.
	#[must_use]
	pub fn RegQueryReflectionKey(&self) -> SysResult<bool> {
		let mut is_disabled = 0;
		ErrorRet(unsafe { ffi::RegQueryReflectionKey(self.ptr(), &mut is_disabled) })
			.to_sysresult()
			.map(|_| is_disabled != 0)
	}

	/// [`RegQueryValueEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexw)
	/// function.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let hkey = w::HKEY::CURRENT_USER.RegOpenKeyEx(
	///     Some("Control Panel\\Mouse"),
	///     co::REG_OPTION::default(),
	///     co::KEY::READ,
	/// )?;
	///
	/// let val = hkey.RegQueryValueEx(Some("Beep"))?;
	/// println!("{val}");
	/// # w::SysResult::Ok(())
	/// ```
	#[must_use]
	pub fn RegQueryValueEx(&self, value_name: Option<&str>) -> SysResult<RegistryValue> {
		let value_name_w = WString::from_opt_str(value_name);
		let mut buf = Vec::<u8>::default();

		loop {
			let mut data_len = 0u32; // in bytes

			ErrorRet(unsafe {
				ffi::RegQueryValueExW(
					self.ptr(),
					value_name_w.as_ptr(),
					std::ptr::null_mut(),
					std::ptr::null_mut(),
					std::ptr::null_mut(),
					&mut data_len, // first call to retrieve size only
				)
			})
			.to_sysresult()?;

			buf.resize(data_len as _, 0x00);
			let mut data_type = 0u32;

			match unsafe {
				co::ERROR::from_raw(ffi::RegQueryValueExW(
					self.ptr(),
					value_name_w.as_ptr(),
					std::ptr::null_mut(),
					&mut data_type,
					buf.as_mut_ptr() as _,
					&mut data_len,
				) as _)
			} {
				co::ERROR::SUCCESS => {
					buf.resize(data_len as _, 0x00); // data length may have shrunk
					return unsafe { RegistryValue::from_raw(buf, co::REG::from_raw(data_type)) };
				},
				co::ERROR::MORE_DATA => continue, // value changed in a concurrent operation; retry
				e => return Err(e),
			}
		}
	}

	/// [`RegRenameKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regrenamekey)
	/// function.
	pub fn RegRenameKey(&self, sub_key_name: &str, new_key_name: &str) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegRenameKey(
				self.ptr(),
				WString::from_str(sub_key_name).as_ptr(),
				WString::from_str(new_key_name).as_ptr(),
			)
		})
		.to_sysresult()
	}

	/// [`RegReplaceKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regreplacekeyw)
	/// function.
	pub fn RegReplaceKey(
		&self,
		sub_key: Option<&str>,
		new_src_file: &str,
		old_file_backup: &str,
	) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegReplaceKeyW(
				self.ptr(),
				WString::from_opt_str(sub_key).as_ptr(),
				WString::from_str(new_src_file).as_ptr(),
				WString::from_str(old_file_backup).as_ptr(),
			)
		})
		.to_sysresult()
	}

	/// [`RegRestoreKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regrestorekeyw)
	/// function.
	pub fn RegRestoreKey(&self, file_path: &str, flags: co::REG_RESTORE) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegRestoreKeyW(self.ptr(), WString::from_str(file_path).as_ptr(), flags.raw())
		})
		.to_sysresult()
	}

	/// [`RegSaveKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsavekeyw)
	/// function.
	pub fn RegSaveKey(
		&self,
		dest_file_path: &str,
		security_attributes: Option<&SECURITY_ATTRIBUTES>,
	) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegSaveKeyW(
				self.ptr(),
				WString::from_str(dest_file_path).as_ptr(),
				pcvoid_or_null(security_attributes),
			)
		})
		.to_sysresult()
	}

	/// [`RegSaveKeyEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsavekeyexw)
	/// function.
	pub fn RegSaveKeyEx(
		&self,
		dest_file_path: &str,
		security_attributes: Option<&SECURITY_ATTRIBUTES>,
		flags: co::REG_SAVE,
	) -> SysResult<()> {
		ErrorRet(unsafe {
			ffi::RegSaveKeyExW(
				self.ptr(),
				WString::from_str(dest_file_path).as_ptr(),
				pcvoid_or_null(security_attributes),
				flags.raw(),
			)
		})
		.to_sysresult()
	}

	/// [`RegSetKeyValue`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsetkeyvaluew)
	/// function.
	///
	/// If the value doesn't exist, if will be created. If new type is different
	/// from current type, new type will take over.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*};
	///
	/// w::HKEY::CURRENT_USER.RegSetKeyValue(
	///     Some("Software\\My Company"),
	///     Some("Color"),
	///     w::RegistryValue::Sz("blue".to_owned()),
	/// )?;
	/// # w::SysResult::Ok(())
	/// ```
	pub fn RegSetKeyValue(
		&self,
		sub_key: Option<&str>,
		value_name: Option<&str>,
		data: RegistryValue,
	) -> SysResult<()> {
		let mut str_buf = WString::new();
		let (data_ptr, data_len) = data.as_ptr_with_len(&mut str_buf);

		ErrorRet(unsafe {
			ffi::RegSetKeyValueW(
				self.ptr(),
				WString::from_opt_str(sub_key).as_ptr(),
				WString::from_opt_str(value_name).as_ptr(),
				data.reg_type().raw(),
				data_ptr,
				data_len,
			)
		})
		.to_sysresult()
	}

	/// [`RegSetValueEx`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsetvalueexw)
	/// function.
	///
	/// If the value doesn't exist, if will be created. If new type is different
	/// from current type, new type will prevail.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let hkey = w::HKEY::CURRENT_USER.RegOpenKeyEx(
	///     Some("Console\\Git Bash"),
	///     co::REG_OPTION::default(),
	///     co::KEY::ALL_ACCESS,
	/// )?;
	///
	/// hkey.RegSetValueEx(
	///     Some("Color"),
	///     w::RegistryValue::Sz("blue".to_owned()),
	/// )?;
	/// # w::SysResult::Ok(())
	/// ```
	pub fn RegSetValueEx(&self, value_name: Option<&str>, data: RegistryValue) -> SysResult<()> {
		let mut str_buf = WString::new();
		let (data_ptr, data_len) = data.as_ptr_with_len(&mut str_buf);

		ErrorRet(unsafe {
			ffi::RegSetValueExW(
				self.ptr(),
				WString::from_opt_str(value_name).as_ptr(),
				0,
				data.reg_type().raw(),
				data_ptr as _,
				data_len,
			)
		})
		.to_sysresult()
	}

	/// [`RegUnLoadKey`](https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regunloadkeyw)
	/// function.
	pub fn RegUnLoadKey(&self, sub_key: Option<&str>) -> SysResult<()> {
		ErrorRet(unsafe { ffi::RegUnLoadKeyW(self.ptr(), WString::from_opt_str(sub_key).as_ptr()) })
			.to_sysresult()
	}
}