| 809 | |
| 810 | #[pyfunction] |
| 811 | fn SetValue( |
| 812 | key: PyRef<PyHkey>, |
| 813 | sub_key: String, |
| 814 | typ: u32, |
| 815 | value: String, |
| 816 | vm: &VirtualMachine, |
| 817 | ) -> PyResult<()> { |
| 818 | if typ != Registry::REG_SZ { |
| 819 | return Err(vm.new_type_error("type must be winreg.REG_SZ")); |
| 820 | } |
| 821 | |
| 822 | let hkey = key.hkey.load(); |
| 823 | if hkey == Registry::HKEY_PERFORMANCE_DATA { |
| 824 | return Err(os_error_from_windows_code( |
| 825 | vm, |
| 826 | Foundation::ERROR_INVALID_HANDLE as i32, |
| 827 | )); |
| 828 | } |
| 829 | |
| 830 | // Create subkey if sub_key is non-empty |
| 831 | let child_key = if !sub_key.is_empty() { |
| 832 | let wide_sub_key = sub_key.to_wide_with_nul(); |
| 833 | let mut out_key = core::ptr::null_mut(); |
| 834 | let res = unsafe { |
| 835 | Registry::RegCreateKeyExW( |
| 836 | hkey, |
| 837 | wide_sub_key.as_ptr(), |
| 838 | 0, |
| 839 | core::ptr::null(), |
| 840 | 0, |
| 841 | Registry::KEY_SET_VALUE, |
| 842 | core::ptr::null(), |
| 843 | &mut out_key, |
| 844 | core::ptr::null_mut(), |
| 845 | ) |
| 846 | }; |
| 847 | if res != 0 { |
| 848 | return Err(os_error_from_windows_code(vm, res as i32)); |
| 849 | } |
| 850 | Some(out_key) |
| 851 | } else { |
| 852 | None |
| 853 | }; |
| 854 | |
| 855 | let target_key = child_key.unwrap_or(hkey); |
| 856 | // Convert value to UTF-16 for Wide API |
| 857 | let wide_value = value.to_wide_with_nul(); |
| 858 | let res = unsafe { |
| 859 | Registry::RegSetValueExW( |
| 860 | target_key, |
| 861 | core::ptr::null(), // value name is NULL |
| 862 | 0, |
| 863 | typ, |
| 864 | wide_value.as_ptr() as *const u8, |
| 865 | (wide_value.len() * 2) as u32, // byte count |
| 866 | ) |
| 867 | }; |
| 868 | |