Proper handling of LPWSTR output in windows-rs

1k Views Asked by At

I noticed there are two ways of handling LPWSTR output types in the windows-rs crate; Both start with marshalling the string into a slice:

let len = (0..).take_while(|&i| *ptr.offset(i) != 0).count();
let slice = std::slice::from_raw_parts(ptr, len);

Then it seems we can do one of:

let output = OsString::from_wide(slice).to_string_lossy().into_owned()

or

let output = String::from_utf16_lossy(slice)

The first option I'm guessing is more correct, but in order to use it you have to bring std::os::windows::prelude::OsStringExt into scope. The problem for me right now is that prevents rust-analyzer from linting due to a bug: https://github.com/rust-analyzer/rust-analyzer/issues/6063

Using the later method sidesteps this to improve my development workflow. Is the later method likely cause me any problems?

0

There are 0 best solutions below