简体   繁体   English

如何为 RegGetValueW 分配内存(通过 Windows crate for Rust)

[英]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 to RegGetValueW使用u16我还需要更改传递给RegGetValueW的大小

let mut my_size = (std::mem::size_of::<u16>() * value.len()) as u32;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何分配空间以使用 windows 板条箱调用 GetInterfaceInfo? - How do I allocate space to call GetInterfaceInfo using the windows crate? 如何使用 Windows 的 Rust 板条箱将 CompositionTarget 初始化为 null? - How do I initialize a CompositionTarget to null using the Rust Crate for Windows? 从 Rust Windows 二进制包导出 function - Export function from Rust Windows binary crate 如何在 Windows 上通过 CLI 安装 Rust? - How to install Rust via CLI on Windows? 如何在 Windows 的 Rust 板条箱中为 CoreApp 注册 PointerPressed 事件处理程序? - How do you register a PointerPressed event handler for a CoreApp in the Rust Crate for Windows? 我如何在Windows 8上分配空页内存? - How i can allocate null page memory on Windows 8? 如何在Windows中直接分配视频卡内存? - How to allocate video-card memory directly in Windows? 错误 [E0463]: can&#39;t find crate for std... 如何在 x64 Windows 机器上编译 Rust x32? - Error[E0463]: can't find crate for std... how to compile Rust x32 in a x64 Windows machine? 我应该尝试在 Windows 的 Rust Crate 中的 CoreApp 的 PointerPressed 事件处理程序中访问 self 吗? - Should I try to access self in a PointerPressed event handler for a CoreApp in the Rust Crate for Windows? 如何为Windows中的进程分配超过2GB的内存? - How can I allocate more than 2GB of memory to a process in Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM