56#define WINSTD_STRING_IMPL(x) #x
61#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
66#define WINSTD_NONCOPYABLE(C) \
68 C (_In_ const C &h) noexcept; \
69 C& operator=(_In_ const C &h) noexcept;
74#define WINSTD_NONMOVABLE(C) \
76 C (_Inout_ C &&h) noexcept; \
77 C& operator=(_Inout_ C &&h) noexcept;
79#ifndef WINSTD_STACK_BUFFER_BYTES
93#define WINSTD_STACK_BUFFER_BYTES 1024
105#define PRINTF_LPTSTR "ls"
107#define PRINTF_LPTSTR "s"
114#define PRINTF_LPOLESTR "hs"
116#define PRINTF_LPOLESTR "ls"
123#define _tcin (std::wcin )
125#define _tcin (std::cin )
132#define _tcout (std::wcout)
134#define _tcout (std::cout)
141#define _tcerr (std::wcerr)
143#define _tcerr (std::cerr)
150#define _tclog (std::wclog)
152#define _tclog (std::clog)
163#define WINSTD_HANDLE_IMPL(C, INVAL) \
166 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
167 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
168 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
169 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
175#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
178 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
179 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
180 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
181 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
182 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
183 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
188#ifndef _FormatMessage_format_string_
189#define _FormatMessage_format_string_ _In_z_
193#ifndef _LPCBYTE_DEFINED
194#define _LPCBYTE_DEFINED
195typedef const BYTE *LPCBYTE;
202#pragma warning(disable: 4995)
203#pragma warning(disable: 4996)
204#pragma warning(disable: 4505)
220static int vsnprintf(_Out_z_cap_(capacity)
char *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const char *format, _In_ va_list arg)
222 return _vsnprintf(str, capacity, format, arg);
236static int vsnprintf(_Out_z_cap_(capacity)
wchar_t *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const wchar_t *format, _In_ va_list arg)
noexcept
238 return _vsnwprintf(str, capacity, format, arg);
250template<
class _Elem,
class _Traits,
class _Ax>
251static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_ va_list arg)
256 int count =
vsnprintf(buf, _countof(buf) - 1, format, arg);
259 str.assign(buf, count);
263 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
264 count =
vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
266 str.assign(buf_dyn.get(), count);
283template<
class _Elem,
class _Traits,
class _Ax>
284static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, ...)
287 va_start(arg, format);
288 const int res =
vsprintf(str, format, arg);
298template<
class _Traits,
class _Ax>
299static _Success_(
return != 0) int
WideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_
int cchWideChar, _Out_ std::basic_string<
char, _Traits, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
304 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
307 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
309 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
311 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
312 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
313 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
314 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
326static _Success_(
return != 0) int
WideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_
int cchWideChar, _Out_ std::vector<
char, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
331 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
334 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
336 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
338 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
339 sMultiByteStr.resize(cch);
340 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
351template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
352static _Success_(
return != 0) int
WideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_ std::basic_string<
wchar_t, _Traits1, _Ax1> sWideCharStr, _Out_ std::basic_string<
char, _Traits2, _Ax2> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
357 int cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
360 sMultiByteStr.assign(szStackBuffer, cch);
362 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
364 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
365 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
366 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
367 sMultiByteStr.assign(szBuffer.get(), cch);
380template<
class _Traits,
class _Ax>
381static _Success_(
return != 0) int
SecureWideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_
int cchWideChar, _Out_ std::basic_string<
char, _Traits, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
386 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
389 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
391 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
393 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
394 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
395 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
396 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
397 SecureZeroMemory(szBuffer.get(),
sizeof(CHAR) * cch);
400 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
413static _Success_(
return != 0) int
SecureWideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_
int cchWideChar, _Out_ std::vector<
char, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
418 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
421 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
423 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
425 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
426 sMultiByteStr.resize(cch);
427 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
430 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
442template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
443static _Success_(
return != 0) int
SecureWideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _Out_ std::basic_string<
wchar_t, _Traits1, _Ax1> sWideCharStr, _Out_ std::basic_string<
char, _Traits2, _Ax2> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
448 int cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
451 sMultiByteStr.assign(szStackBuffer, cch);
453 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
455 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
456 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
457 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
458 sMultiByteStr.assign(szBuffer.get(), cch);
459 SecureZeroMemory(szBuffer.get(),
sizeof(CHAR) * cch);
462 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
472template<
class _Traits,
class _Ax>
473static _Success_(
return != 0) int
MultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_
int cbMultiByte, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sWideCharStr) noexcept
478 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
481 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
483 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
486 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
488 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
500static _Success_(
return != 0) int
MultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_
int cbMultiByte, _Out_ std::vector<
wchar_t, _Ax> &sWideCharStr) noexcept
505 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
508 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
510 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
513 sWideCharStr.resize(cch);
514 cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
525template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
526static _Success_(
return != 0) int
MultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_ const std::basic_string<
char, _Traits1, _Ax1> &sMultiByteStr, _Out_ std::basic_string<
wchar_t, _Traits2, _Ax2> &sWideCharStr) noexcept
531 int cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
534 sWideCharStr.assign(szStackBuffer, cch);
536 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
538 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
539 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
540 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szBuffer.get(), cch);
541 sWideCharStr.assign(szBuffer.get(), cch);
554template<
class _Traits,
class _Ax>
555static _Success_(
return != 0) int
SecureMultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_
int cbMultiByte, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sWideCharStr) noexcept
560 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
563 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
565 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
568 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
570 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
571 SecureZeroMemory(szBuffer.get(),
sizeof(WCHAR) * cch);
574 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
587static _Success_(
return != 0) int
SecureMultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_
int cbMultiByte, _Out_ std::vector<
wchar_t, _Ax> &sWideCharStr) noexcept
592 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
595 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
597 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
600 sWideCharStr.resize(cch);
601 cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
604 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
616template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
617static _Success_(
return != 0) int
SecureMultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_ const std::basic_string<
char, _Traits1, _Ax1> &sMultiByteStr, _Out_ std::basic_string<
wchar_t, _Traits2, _Ax2> &sWideCharStr) noexcept
622 int cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
625 sWideCharStr.assign(szStackBuffer, cch);
627 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
629 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
630 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
631 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szBuffer.get(), cch);
632 sWideCharStr.assign(szBuffer.get(), cch);
633 SecureZeroMemory(szBuffer.get(),
sizeof(WCHAR) * cch);
636 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
646template<
class _Traits,
class _Ax>
647static DWORD
FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
649 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
650 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPSTR
>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
652 str.assign(lpBuffer.get(), dwResult);
661template<
class _Traits,
class _Ax>
662static DWORD
FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
664 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
665 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPWSTR
>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
667 str.assign(lpBuffer.get(), dwResult);
744 template<
class _Other>
789 m_data =
reinterpret_cast<T*
>(GlobalLock(hMem));
820 template<
class _Ty,
class _Dx>
831 m_ptr(owner.release())
843 other.m_ptr =
nullptr;
851 if (
m_ptr !=
nullptr)
860 operator typename _Ty**()
870 operator typename _Ty*&()
887 template<
class _Ty,
class _Dx>
897 template<
class _Ty,
class _Dx>
908 m_ptr(owner.release())
920 other.m_ptr =
nullptr;
928 if (
m_ptr !=
nullptr)
937 operator typename _Ty**()
noexcept
947 operator typename _Ty*&()
965 template<
class _Ty,
class _Dx>
981 template <
class T, const T INVAL>
1026 handle<handle_type, INVAL>& operator=(_In_
const handle<handle_type, INVAL> &h)
noexcept {};
1045 #pragma warning(suppress: 26432)
1048 if (
this != std::addressof(h)) {
1240 template <class T, const T INVAL>
1246 template <class T, T INVAL>
1280 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
1302 if (
this != std::addressof(h)) {
1325 #pragma warning(disable: 26432)
1383 std::string sResult;
1384 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1386 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1388 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1390 LPCWSTR szMessage =
reinterpret_cast<LPCWSTR
>(LockResource(hLoadedRes));
1392 WideCharToMultiByte(CP_UTF8, 0, szMessage, dwSize /
sizeof(*szMessage), sResult, NULL, NULL);
1395 SetLastError(ERROR_LOCK_FAILED);
1399 sprintf(sResult,
"msg %u", nId);
1412 inline std::string
fmt_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage, ...)
1414 std::string sResult;
1415 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1417 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1419 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1421 LPCWSTR szFormat =
reinterpret_cast<LPCWSTR
>(LockResource(hLoadedRes));
1423 dwSize /=
sizeof(*szFormat);
1424 assert(wcsnlen(szFormat, dwSize) < dwSize);
1426 va_start(arg, wLanguage);
1427 std::wstring sMessage;
1433 SetLastError(ERROR_LOCK_FAILED);
1437 sprintf(sResult,
"msg %u", nId);
1444 template <
typename _Tn>
1552 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
1554 wstr.erase(wstr.find_last_not_of(L
" \t\n\r\f\v") + 1);
1556 sprintf(wstr, num >= 0x10000 ? L
"Error 0x%X" : L
"Error %u", num);
1559 SetLastError(runtime_num);
1572 template<
class _Elem,
class _Traits,
class _Ax>
1587 va_start(arg, format);
1606 ATLENSURE(format.LoadString(hInstance, nFormatID));
1609 va_start(arg, nFormatID);
1624 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1627 va_start(arg, nFormatID);
1657 template<
class _Elem,
class _Traits,
class _Ax>
1672 va_start(arg, format);
1673 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1690 _Myt format(GetManager());
1691 ATLENSURE(format.LoadString(hInstance, nFormatID));
1694 va_start(arg, nFormatID);
1695 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1708 _Myt format(GetManager());
1709 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1712 va_start(arg, nFormatID);
1713 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1724 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1726 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, Arguments);
1734 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1736 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, (va_list*)Arguments);
1746 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, Arguments);
1756 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, (va_list*)Arguments);
1782 template<
class _Elem,
class _Traits,
class _Ax>
1797 sprintf<_Elem, _Traits, _Ax>(*
this, format,
1801 guid.Data4[0], guid.Data4[1],
1802 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1823 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1845 basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >(guid, L
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1867 #pragma warning(push)
1868 #pragma warning(disable: 4100)
1886 template<
class _Other>
1909 template<
class _Other>
1920 SecureZeroMemory(_Ptr, _Size);
1921 _Mybase::deallocate(_Ptr, _Size);
1925 #pragma warning(pop)
1966 ZeroMemory(m_data, N);
1974 SecureZeroMemory(m_data, N);
1978 unsigned char m_data[N];
Base template class to support converting GUID to string.
Definition Common.h:1784
basic_string_guid(const GUID &guid, const _Elem *format)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1795
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1659
basic_string_msg(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1734
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1744
basic_string_msg(HINSTANCE hInstance, WORD wLanguageID, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1706
basic_string_msg(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1724
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1669
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1688
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1754
Base template class to support string formatting using printf() style templates.
Definition Common.h:1574
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1584
basic_string_printf(HINSTANCE hInstance, WORD wLanguageID, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1621
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1603
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1248
virtual handle_type duplicate_internal(handle_type h) const =0
Abstract member function that must be implemented by child classes to do the actual object handle dup...
dplhandle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1289
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1337
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1326
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1347
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1262
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1300
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1253
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:777
HGLOBAL m_h
memory handle
Definition Common.h:813
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:799
T * m_data
memory pointer
Definition Common.h:814
T * data() const noexcept
Return data pointer.
Definition Common.h:807
globalmem_accessor(HGLOBAL hMem)
Locks a global memory object and returns a pointer to the first byte of the object's memory block.
Definition Common.h:787
Base abstract template class to support generic object handle keeping.
Definition Common.h:983
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:1073
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:998
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1149
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:1094
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:1083
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:988
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1007
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1123
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1034
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:1110
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:1046
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1175
void free()
Destroys the object.
Definition Common.h:1222
handle_type m_h
Object handle.
Definition Common.h:1237
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1200
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1188
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:1016
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1212
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1162
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1136
Numerical runtime error.
Definition Common.h:1446
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1469
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1457
error_type number() const
Returns the error number.
Definition Common.h:1478
_Tn error_type
Error number type.
Definition Common.h:1448
error_type m_num
Numeric error code.
Definition Common.h:1484
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition Common.h:953
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition Common.h:916
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:926
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition Common.h:906
_Ty * m_ptr
Pointer.
Definition Common.h:954
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:822
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition Common.h:876
_Ty * m_ptr
Pointer.
Definition Common.h:877
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition Common.h:839
~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:849
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition Common.h:829
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1879
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1902
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1917
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1910
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1881
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1895
Sanitizing BLOB.
Definition Common.h:1959
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1964
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1972
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1812
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1822
Windows runtime error.
Definition Common.h:1491
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1538
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1516
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1498
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1522
static std::string message(error_type num, DWORD dwLanguageId=0)
Returns a user-readable Windows error message. As std::exception messages may only be char*,...
Definition Common.h:1548
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1530
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1507
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1834
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1844
std::string load_msg_from_res(HMODULE hModule, UINT nId, WORD wLanguage)
Loads exception message string from resources and converts it to UTF-8.
Definition Common.h:1381
std::string fmt_msg_from_res(HMODULE hModule, UINT nId, WORD wLanguage,...)
Loads exception message sprintf template from resources, formats it and converts it to UTF-8.
Definition Common.h:1412
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:66
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:93
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition Common.h:686
ref_unique_ptr< _Ty, _Dx > get_ptr(std::unique_ptr< _Ty, _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr.
Definition Common.h:888
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition Common.h:74
std::basic_string< wchar_t, std::char_traits< wchar_t >, sanitizing_allocator< wchar_t > > sanitizing_wstring
A sanitizing variant of std::wstring.
Definition Common.h:1943
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1951
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1934
static const T invalid
Invalid handle value.
Definition Common.h:993
Deleter for unique_ptr using GlobalFree.
Definition Common.h:755
GlobalFree_delete()
Default construct.
Definition Common.h:759
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:766
LocalFree_delete() noexcept
Default construct.
Definition Common.h:729
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:724
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:745
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:734
Deleter for unique_ptr using LocalFree.
Definition Common.h:694
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:695
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:705
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:712
LocalFree_delete()
Default construct.
Definition Common.h:700
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1888
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1889