Convert an &str to utf16 on no_std

So basically, I need to use winapi with no_std, and many of the function parameters require a type of &[u16]
I initially tried to use the utf16 crate, and it seemed to work fine although when I tried to set the lpSubKey field in the RegOpenKeyExW function and then tried running the program it exited with ERROR_FILE_NOT_FOUND. I went looking through the source code of the winreg crate, and found this function:

fn to_utf16<P: AsRef<OsStr>>(s: P) -> Vec<u16> {
    s.as_ref()
        .encode_wide()
        .chain(Some(0).into_iter())
        .collect()
}

I made a new project that wasn't no_std and used that function for converting to utf16, and the error disappeared and I was able to open the registry key. Is this a problem with the way I am using the utf16 crate? or is there some other way I can convert to utf16 without the standard library that will give the same results as the function in the winreg crate?

If you can't use std (or the alloc built-in crate), then you can't make a Vec.

There's ArrayVec that has a fixed pre-allocated capacity on the stack. If your string is relatively short, then you could use it instead:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.