When accessing DLL functions on Windows I need to be able to read Windows UTF-16 strings they return and to pass in the same kind of strings.
For passing a string into a DLL I'm using this (which is something I got off the internet -- possibly this forum):
pub type Wchar = u16;
pub fn win16_for_str(s: &str) -> Vec<Wchar> {
OsStr::new(s).encode_wide().chain(iter::once(0)).collect()
}
To use this I do let arg = win16_for_str("text"); let reply = win_func(arg.as_ptr());
But when I get a string returned from Windows (i.e., as a *const u16
) I made my own (although based on ideas I found online). Is this a sane and reasonable function? BTW I want to get a String back no matter what.
pub fn str_for_pwin16(p: *const Wchar) -> String {
if p.is_null() {
return String::new();
}
unsafe {
let mut size = 0;
while size < MAX_TEXT_LEN { // e.g., 65_000, 100_000, ...
if *p.offset(size) == 0 {
break;
}
size += 1;
}
size += 1;
let slice = slice::from_raw_parts(p, size as usize);
let result = OsString::from_wide(slice).into_string();
if result.is_ok() {
return result.unwrap();
}
return result.unwrap_err().to_string_lossy().to_string();
}
}
Thanks.