Creates or sets a value for the current key. # Arguments `name` - A string containing the name of the value. # Remarks Returns a RegistryValue representing the new value. # Examples ``` # use ntreg::registry_key::*; # use ntreg::registry_value::*; let key = RegistryKey::new("HKCU").unwrap(); key.set_value("TestValue", &RegistryValueData::String("TestValueData".to_string())); key.delete(false);
(&self, name: &str, value: &RegistryValueData)
| 307 | /// key.delete(false); |
| 308 | /// ``` |
| 309 | pub fn set_value(&self, name: &str, value: &RegistryValueData) -> Result<RegistryValue, NtStatusError> { |
| 310 | let key = match open_key(&self.path.clone(), KEY_ALL_ACCESS) { |
| 311 | Ok((key, _)) => key, |
| 312 | Err(err) => return Err(err), |
| 313 | }; |
| 314 | |
| 315 | let mut data: *mut c_void = null_mut(); |
| 316 | let mut string_data: Vec<u16>; |
| 317 | let mut binary_data: Vec<u8>; |
| 318 | let mut len: usize = 0; |
| 319 | match value { |
| 320 | RegistryValueData::None => {} |
| 321 | RegistryValueData::String(ref string) | RegistryValueData::ExpandString(ref string) | RegistryValueData::Link(ref string) => { |
| 322 | string_data = string.encode_utf16().collect(); |
| 323 | string_data.push(0); |
| 324 | data = string_data.as_mut_ptr() as *mut c_void; |
| 325 | len = string_data.len() * size_of::<u16>(); |
| 326 | } |
| 327 | RegistryValueData::MultiString(ref multi_string) => { |
| 328 | string_data = Vec::new(); |
| 329 | for s in multi_string { |
| 330 | let mut s: Vec<u16> = s.encode_utf16().collect(); |
| 331 | s.push(0); |
| 332 | string_data.extend(s) |
| 333 | } |
| 334 | string_data.push(0); |
| 335 | data = string_data.as_mut_ptr() as *mut c_void; |
| 336 | len = string_data.len() * size_of::<u16>(); |
| 337 | } |
| 338 | RegistryValueData::Binary(ref binary) | RegistryValueData::FullResourceDescriptor(ref binary) | |
| 339 | RegistryValueData::ResourceList(ref binary) | RegistryValueData::ResourceRequirementsList(ref binary) => { |
| 340 | binary_data = binary.clone(); |
| 341 | data = binary_data.as_mut_ptr() as *mut c_void; |
| 342 | len = binary_data.len(); |
| 343 | } |
| 344 | RegistryValueData::DWord(ref value) => { |
| 345 | let mut value = *value; |
| 346 | data = &mut value as *mut u32 as *mut c_void; |
| 347 | len = size_of::<u32>(); |
| 348 | } |
| 349 | RegistryValueData::QWord(ref value) => { |
| 350 | let mut value = *value; |
| 351 | data = &mut value as *mut u64 as *mut c_void; |
| 352 | len = size_of::<u64>(); |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | let data_type_value: u32 = match value { |
| 357 | RegistryValueData::None => 0, |
| 358 | RegistryValueData::String(_) => 1, |
| 359 | RegistryValueData::ExpandString(_) => 2, |
| 360 | RegistryValueData::Binary(_) => 3, |
| 361 | RegistryValueData::DWord(_) => 4, |
| 362 | RegistryValueData::Link(_) => 6, |
| 363 | RegistryValueData::MultiString(_) => 7, |
| 364 | RegistryValueData::ResourceList(_) => 8, |
| 365 | RegistryValueData::FullResourceDescriptor(_) => 9, |
| 366 | RegistryValueData::ResourceRequirementsList(_) => 10, |