[英]How to allocate memory for RegGetValueW (via the Windows crate for Rust)
I'm attempting to read a value from the windows registry using the official windows rust bindings .我正在尝试使用官方windows rust bindings从 windows 注册表中读取一个值。
I've tried a lot of different things without success, so I created a C++ version as a sanity test:我尝试了很多不同的东西都没有成功,所以我创建了一个 C++ 版本作为健全性测试:
WCHAR value[64];
PVOID pvData = value;
DWORD size = sizeof(value);
LSTATUS result = RegGetValue(
HKEY_CURRENT_USER,
L"SOFTWARE\\MyTest\\foo",
NULL,
RRF_RT_REG_SZ,
NULL,
pvData,
&size);
if (result == ERROR_SUCCESS) {
wprintf(L"Value data: %s (%d)\n", (PWSTR)pvData, size);
}
This works as expected and prints Value data: hello (12)
(It's not perfect but the end result shows at least a working happy path with no cleanup.)这按预期工作并打印Value data: hello (12)
(这并不完美,但最终结果至少显示了一条没有清理的工作快乐路径。)
My latest Rust attempt is:我最近的 Rust 尝试是:
unsafe {
let mut value: Vec<u16> = vec![0; 64];
let mut my_size: u32 = 0;
let size = &mut my_size as *mut u32;
let result = RegGetValueW(
HKEY_CURRENT_USER,
"SOFTWARE\\MyTest\\foo",
None,
RRF_RT_REG_SZ,
std::ptr::null_mut(), // Doesn't allow None
value.as_mut_ptr() as *mut c_void,
size
);
println!("Result: {:?}", result);
println!("Value data: {:?} ({:?})", value, *size);
// Result: LSTATUS(234) // 234 = ERROR_MORE_DATA, so pvData is not large enough
// Value data: [0, 0, ...] (14) // 14 is peculiar given the C++ size reports 12
}
Would greatly appreciate some guidance or nudge in the right direction!非常感谢一些指导或朝着正确方向轻推!
I had two major issues that needed to be resolved:我有两个主要问题需要解决:
WCHAR would be a u16 in rust WCHAR 将是一个 u16 in rust
I was using u8
我用的是u8
let mut value: Vec<u16> = vec![0; 64];
When using
u16
I also needed to change the size passed toRegGetValueW
使用u16
我还需要更改传递给RegGetValueW
的大小
let mut my_size = (std::mem::size_of::<u16>() * value.len()) as u32;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.