57#define WINSTD_STRING_IMPL(x) #x
62#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
67#define WINSTD_NONCOPYABLE(C) \
69 C (_In_ const C &h) noexcept; \
70 C& operator=(_In_ const C &h) noexcept;
75#define WINSTD_NONMOVABLE(C) \
77 C (_Inout_ C &&h) noexcept; \
78 C& operator=(_Inout_ C &&h) noexcept;
80#ifndef WINSTD_STACK_BUFFER_BYTES
94#define WINSTD_STACK_BUFFER_BYTES 1024
106#define PRINTF_LPTSTR "ls"
108#define PRINTF_LPTSTR "s"
115#define PRINTF_LPOLESTR "hs"
117#define PRINTF_LPOLESTR "ls"
124#define _tcin (std::wcin )
126#define _tcin (std::cin )
133#define _tcout (std::wcout)
135#define _tcout (std::cout)
142#define _tcerr (std::wcerr)
144#define _tcerr (std::cerr)
151#define _tclog (std::wclog)
153#define _tclog (std::clog)
164#define WINSTD_HANDLE_IMPL(C, T, INVAL) \
167 C (_In_opt_ T h) noexcept : handle<T, INVAL>( h ) {} \
168 C (_Inout_ C &&h) noexcept : handle<T, INVAL>(std::move(h)) {} \
169 C& operator=(_In_opt_ T h) noexcept { handle<T, INVAL>::operator=( h ); return *this; } \
170 C& operator=(_Inout_ C &&h) noexcept { handle<T, INVAL>::operator=(std::move(h)); return *this; } \
176#define WINSTD_DPLHANDLE_IMPL(C, T, INVAL) \
179 C (_In_opt_ T h) noexcept : dplhandle<T, INVAL>( h ) {} \
180 C (_In_ const C &h) noexcept : dplhandle<T, INVAL>(duplicate_internal(h.m_h)) {} \
181 C (_Inout_ C &&h) noexcept : dplhandle<T, INVAL>(std::move (h )) {} \
182 C& operator=(_In_opt_ T h) noexcept { dplhandle<T, INVAL>::operator=( h ); return *this; } \
183 C& operator=(_In_ const C &h) noexcept { dplhandle<T, INVAL>::operator=( h ); return *this; } \
184 C& operator=(_Inout_ C &&h) noexcept { dplhandle<T, INVAL>::operator=(std::move(h)); return *this; } \
189#ifndef _FormatMessage_format_string_
190#define _FormatMessage_format_string_ _In_z_
194#ifndef _LPCBYTE_DEFINED
195#define _LPCBYTE_DEFINED
196typedef const BYTE *LPCBYTE;
203#pragma warning(disable: 4995)
204#pragma warning(disable: 4996)
205#pragma warning(disable: 4505)
208inline ULONGLONG ULongLongMult(ULONGLONG a, ULONGLONG b)
211 if (SUCCEEDED(ULongLongMult(a, b, &result)))
213 throw std::invalid_argument(
"multiply overflow");
216inline SIZE_T SIZETMult(SIZE_T a, SIZE_T b)
219 if (SUCCEEDED(SIZETMult(a, b, &result)))
221 throw std::invalid_argument(
"multiply overflow");
226inline ULONGLONG ULongLongAdd(ULONGLONG a, ULONGLONG b)
229 if (SUCCEEDED(ULongLongAdd(a, b, &result)))
231 throw std::invalid_argument(
"add overflow");
234inline SIZE_T SIZETAdd(SIZE_T a, SIZE_T b)
237 if (SUCCEEDED(SIZETAdd(a, b, &result)))
239 throw std::invalid_argument(
"add overflow");
255template<
class _Traits,
class _Ax>
256static int vsprintf(_Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const char *format, _In_ va_list arg)
261 int count = _vsnprintf(buf, _countof(buf), format, arg);
262 if (0 <= count && count < _countof(buf)) {
264 str.append(buf, count);
270 count = _vsnprintf(NULL, 0, format, arg);
273 case EINVAL:
throw std::invalid_argument(
"invalid vsnprintf arguments");
274 case EILSEQ:
throw std::runtime_error(
"encoding error");
275 default:
throw std::runtime_error(
"failed to format string");
278 size_t offset = str.size();
279 str.resize(offset + count);
280 if (_vsnprintf(&str[offset], count + 1, format, arg) != count)
281 throw std::runtime_error(
"failed to format string");
294template<
class _Traits,
class _Ax>
295static int vsprintf(_Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const wchar_t *format, _In_ va_list arg)
300 int count = _vsnwprintf(buf, _countof(buf), format, arg);
301 if (0 <= count && count < _countof(buf)) {
303 str.append(buf, count);
309 count = _vsnwprintf(NULL, 0, format, arg);
312 case EINVAL:
throw std::invalid_argument(
"invalid vsnprintf arguments");
313 case EILSEQ:
throw std::runtime_error(
"encoding error");
314 default:
throw std::runtime_error(
"failed to format string");
317 size_t offset = str.size();
318 str.resize(offset + count);
319 if (_vsnwprintf(&str[offset], count + 1, format, arg) != count)
320 throw std::runtime_error(
"failed to format string");
332template<
class _Elem,
class _Traits,
class _Ax>
333static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, ...)
336 va_start(arg, format);
337 const int res =
vsprintf(str, format, arg);
347template<
class _Traits,
class _Ax>
348static _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
353 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
356 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (
size_t)cch - 1);
358 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
360 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
361 sMultiByteStr.resize(cch);
362 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
363 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (
size_t)cch - 1);
375static _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
380 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
383 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
385 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
387 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
388 sMultiByteStr.resize(cch);
389 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
400template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
401static _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
406 int cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
409 sMultiByteStr.assign(szStackBuffer, cch);
411 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
413 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
414 sMultiByteStr.resize(cch);
415 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
428template<
class _Traits,
class _Ax>
429static _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
434 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
437 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (
size_t)cch - 1);
439 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
441 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
442 sMultiByteStr.resize(cch);
443 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
444 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (
size_t)cch - 1);
447 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
460static _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
465 int cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
468 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
470 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
472 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
473 sMultiByteStr.resize(cch);
474 cch =
::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
477 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
489template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
490static _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
495 int cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
498 sMultiByteStr.assign(szStackBuffer, cch);
500 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
502 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
503 sMultiByteStr.resize(cch);
504 cch =
::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
507 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
517template<
class _Traits,
class _Ax>
518static _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
523 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
526 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
528 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
531 sWideCharStr.resize(cch);
533 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (
size_t)cch - 1);
545static _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
550 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
553 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
555 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
558 sWideCharStr.resize(cch);
559 cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
570template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
571static _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
576 int cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
579 sWideCharStr.assign(szStackBuffer, cch);
581 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
583 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
584 sWideCharStr.resize(cch);
585 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), &sWideCharStr[0], cch);
598template<
class _Traits,
class _Ax>
599static _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
604 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
607 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
609 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
612 sWideCharStr.resize(cch);
614 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (
size_t)cch - 1);
617 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
630static _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
635 int cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
638 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
640 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
643 sWideCharStr.resize(cch);
644 cch =
::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
647 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
659template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
660static _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
665 int cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
668 sWideCharStr.assign(szStackBuffer, cch);
670 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
672 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
673 sWideCharStr.resize(cch);
674 cch =
::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), &sWideCharStr[0], cch);
677 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
687template<
class _Traits,
class _Ax>
688static DWORD
FormatMessageA(_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)
691 DWORD dwResult =
FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPSTR
>(&lpBuffer), 0, Arguments);
693 str.assign(lpBuffer, dwResult);
704template<
class _Traits,
class _Ax>
705static DWORD
FormatMessageW(_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)
708 DWORD dwResult =
FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPWSTR
>(&lpBuffer), 0, Arguments);
710 str.assign(lpBuffer, dwResult);
789 template<
class _Other>
834 m_data =
reinterpret_cast<T*
>(GlobalLock(hMem));
872 template <
class T, const T INVAL>
934 #pragma warning(suppress: 26432)
937 if (
this != std::addressof(h)) {
1129 template <class T, const T INVAL>
1135 template <class T, T INVAL>
1187 if (
this != std::addressof(h)) {
1210 #pragma warning(disable: 26432)
1268 std::string sResult;
1269 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1271 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1273 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1275 LPCWSTR szMessage =
reinterpret_cast<LPCWSTR
>(LockResource(hLoadedRes));
1277 WideCharToMultiByte(CP_UTF8, 0, szMessage, dwSize /
sizeof(*szMessage), sResult, NULL, NULL);
1280 SetLastError(ERROR_LOCK_FAILED);
1284 sprintf(sResult,
"msg %u", nId);
1297 inline std::string
fmt_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage, ...)
1299 std::string sResult;
1300 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1302 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1304 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1306 LPCWSTR szFormat =
reinterpret_cast<LPCWSTR
>(LockResource(hLoadedRes));
1308 dwSize /=
sizeof(*szFormat);
1309 assert(wcsnlen(szFormat, dwSize) < dwSize);
1311 va_start(arg, wLanguage);
1312 std::wstring sMessage;
1318 SetLastError(ERROR_LOCK_FAILED);
1322 sprintf(sResult,
"msg %u", nId);
1329 template <
typename _Tn>
1392 SetLastError(m_error);
1464 if (
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
1466 wstr.erase(wstr.find_last_not_of(L
" \t\n\r\f\v") + 1);
1468 sprintf(wstr, num >= 0x10000 ? L
"Error 0x%X" : L
"Error %u", num);
1483 template<
class _Elem,
class _Traits,
class _Ax>
1498 va_start(arg, format);
1517 ATLENSURE(format.LoadString(hInstance, nFormatID));
1520 va_start(arg, nFormatID);
1535 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1538 va_start(arg, nFormatID);
1568 template<
class _Elem,
class _Traits,
class _Ax>
1583 va_start(arg, format);
1584 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1601 _Myt format(GetManager());
1602 ATLENSURE(format.LoadString(hInstance, nFormatID));
1605 va_start(arg, nFormatID);
1606 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1619 _Myt format(GetManager());
1620 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1623 va_start(arg, nFormatID);
1624 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1635 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1637 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, Arguments);
1645 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1647 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, (va_list*)Arguments);
1657 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, Arguments);
1667 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, (va_list*)Arguments);
1693 template<
class _Elem,
class _Traits,
class _Ax>
1712 guid.Data4[0], guid.Data4[1],
1713 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1734 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1755 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}")
1776 #pragma warning(push)
1777 #pragma warning(disable: 4100)
1795 template<
class _Other>
1816 template<
class _Other>
1823 void deallocate(_In_ _Ty*
const _Ptr, _In_
const std::size_t _Count)
1826 SecureZeroMemory(_Ptr,
sizeof(_Ty) * _Count);
1827 _Mybase::deallocate(_Ptr, _Count);
1831 #pragma warning(pop)
1872 ZeroMemory(m_data, N);
1880 SecureZeroMemory(m_data, N);
1884 unsigned char m_data[N];
Base template class to support converting GUID to string.
Definition Common.h:1695
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:1706
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1570
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:1645
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1655
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:1617
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:1635
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1580
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1599
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1665
Base template class to support string formatting using printf() style templates.
Definition Common.h:1485
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1495
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:1532
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1514
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1137
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:1174
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1222
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1211
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1232
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1150
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1185
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1142
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:822
HGLOBAL m_h
memory handle
Definition Common.h:858
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:844
T * m_data
memory pointer
Definition Common.h:859
T * data() const noexcept
Return data pointer.
Definition Common.h:852
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:832
Base abstract template class to support generic object handle keeping.
Definition Common.h:874
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:962
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:889
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1038
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:983
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:972
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:897
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1012
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:923
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:999
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:935
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1064
void free()
Destroys the object.
Definition Common.h:1111
handle_type m_h
Object handle.
Definition Common.h:1126
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1089
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1077
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:905
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1101
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:879
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1051
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1025
Saves GetLastError and restores SetLastError when going out of scope.
Definition Common.h:1374
~last_error_saver()
Sets the last-error code for the calling thread.
Definition Common.h:1390
last_error_saver(DWORD error=GetLastError())
Saves the calling thread's last-error code value.
Definition Common.h:1381
Numerical runtime error.
Definition Common.h:1331
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1353
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1342
error_type number() const
Returns the error number.
Definition Common.h:1361
error_type m_num
Numeric error code.
Definition Common.h:1367
_Tn error_type
Error number type.
Definition Common.h:1333
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1788
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1810
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1790
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1817
void deallocate(_Ty *const _Ptr, const std::size_t _Count)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1823
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1804
Sanitizing BLOB.
Definition Common.h:1865
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1870
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1878
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1723
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1733
Windows runtime error.
Definition Common.h:1403
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1450
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1428
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1410
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1434
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:1460
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1442
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1419
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1744
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1754
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:1266
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:1297
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:67
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition Common.h:731
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition Common.h:75
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:1849
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1857
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1840
static const T invalid
Invalid handle value.
Definition Common.h:884
Deleter for unique_ptr using GlobalFree.
Definition Common.h:800
GlobalFree_delete()
Default construct.
Definition Common.h:804
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:811
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:769
LocalFree_delete() noexcept
Default construct.
Definition Common.h:774
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:790
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:779
Deleter for unique_ptr using LocalFree.
Definition Common.h:739
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:740
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:750
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:757
LocalFree_delete()
Default construct.
Definition Common.h:745
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1797
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1798