From 3ffeb3dc01fc60f0b947ac2a12a3080c97dfea5e Mon Sep 17 00:00:00 2001 From: Howard Kapustein Date: Sat, 9 May 2026 16:28:31 +0200 Subject: [PATCH] Add make_bstr*(std::wstring_view) --- include/wil/stl.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/wil/stl.h b/include/wil/stl.h index 5abd78d2..7b618f3e 100644 --- a/include/wil/stl.h +++ b/include/wil/stl.h @@ -132,6 +132,28 @@ inline PCWSTR str_raw_ptr(const std::wstring& str) return str.c_str(); } +#if defined(__WIL_OLEAUTO_H_) +#if defined(_STRING_VIEW_) || defined(__cpp_lib_string_view) +// Create wil::unique_bstr from std::wstring_view (regardless if not null terminated, or if it contains embedded nulls) +inline wil::unique_bstr make_bstr_nothrow(std::wstring_view source) noexcept +{ + return wil::unique_bstr(::SysAllocStringLen(source.data(), static_cast(source.size()))); +} +inline wil::unique_bstr make_bstr_failfast(std::wstring_view source) noexcept +{ + return wil::unique_bstr(FAIL_FAST_IF_NULL_ALLOC(::SysAllocStringLen(source.data(), static_cast(source.size())))); +} +#ifdef WIL_ENABLE_EXCEPTIONS +inline wil::unique_bstr make_bstr(std::wstring_view source) +{ + wil::unique_bstr result(make_bstr_nothrow(source)); + THROW_IF_NULL_ALLOC(result); + return result; +} +#endif // WIL_ENABLE_EXCEPTIONS +#endif // defined(_STRING_VIEW_) || defined(__cpp_lib_string_view) +#endif // defined(__WIL_OLEAUTO_H_) + #if __cpp_lib_string_view >= 201606L /**