Closed JMS55 closed 2 weeks ago
This is intentionally verbose since you're dealing with a dangling pointer. You should carefully track the lifetime of the HSTRING
in this case:
use windows::core::*;
fn main() {
unsafe {
let s = "Hello";
let h = HSTRING::from(s);
let w = PCWSTR(h.as_ptr());
println!("{}", w.display());
}
}
Thanks for pointing out the dangling pointer. Just to confirm, HSTRING is like Rust String, and PCWSTR is like Rust &str, ownership wise?
Almost. HSTRING
is an owned strings like the Rust Standard Library's String
.
But PCWSTR
is just a pointer without ownership or lifetime. This is unlike &str
which does include lifetime tracking. This is why no implicit or trait-based conversions are provided for PCWSTR
and friends - there's no lifetime to ensure that it will be safe.
Suggestion
Currently I'm using
PCWSTR::from_raw(HSTRING::from(settings.entry_point).as_ptr())