windows-strings
Windows string types and literal macros. Choose the representation required by the API rather than converting every string to one preferred type.
Get started
[]
= "0.101"
use *;
let text = HSTRINGfrom;
let runtime_name: &HSTRING = h!;
let wide: PCWSTR = w!;
let narrow: PCSTR = s!;
assert_eq!;
assert_eq!;
assert_eq!;
Choose ownership and encoding
| Type | Use |
|---|---|
HSTRING |
Owned WinRT UTF-16 string |
BSTR |
Owned COM Automation string |
PCWSTR, PCSTR |
Borrowed null-terminated UTF-16 or 8-bit input |
PWSTR, PSTR |
Mutable pointers to caller- or API-owned storage |
HSTRING and BSTR carry a length. Pointer wrappers do not record a length, lifetime, or
ownership. Prefer wide Windows APIs: s! encodes UTF-8, but many narrow Win32 APIs expect a
Windows code page.
For dynamic pointer strings, encode into a buffer, append a zero, and retain the buffer throughout the call. Validate interior nulls when the API must receive the whole string. Output parameters need writable storage with the documented size.
Reading a returned pointer is unsafe. Establish its validity and termination, then follow the API's allocation and release rules. A null pointer is not an empty string.
HSTRING::from_wide accepts existing UTF-16 data. BSTR and Windows OsStr/Path conversions
are Windows-only; standard-library conversions require the default std feature.