Skip to main content

Crate windows_strings

Crate windows_strings 

Source
Expand description

§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

[dependencies]
windows-strings = "0.101"
use windows_strings::*;

let text = HSTRING::from("Hello, Windows!");
let runtime_name: &HSTRING = h!("Windows.Foundation.Uri");
let wide: PCWSTR = w!("Hello");
let narrow: PCSTR = s!("Hello");

assert_eq!(text, "Hello, Windows!");
assert_eq!(unsafe { wide.to_string().unwrap() }, "Hello");
assert_eq!(unsafe { narrow.to_string().unwrap() }, "Hello");

§Choose ownership and encoding

TypeUse
HSTRINGOwned WinRT UTF-16 string
BSTROwned COM Automation string
PCWSTR, PCSTRBorrowed null-terminated UTF-16 or 8-bit input
PWSTR, PSTRMutable 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.

Macros§

h
A literal HSTRING, length-prefixed wide string with a trailing null terminator.
s
A literal UTF-8 string with a trailing null terminator.
w
A literal UTF-16 wide string with a trailing null terminator.

Structs§

BSTR
A length-prefixed wide string.
HSTRING
An (HSTRING) is a reference-counted and immutable UTF-16 string type.
HStringBuilder
Builder for preallocating immutable HSTRING values.
PCSTR
A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters.
PCWSTR
A pointer to a constant null-terminated string of 16-bit Unicode characters.
PSTR
A pointer to a null-terminated string of 8-bit Windows (ANSI) characters.
PWSTR
A pointer to a null-terminated string of 16-bit Unicode characters.