43#define WINSTD_STRING_IMPL(x) #x
48#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
53#define WINSTD_NONCOPYABLE(C) \
55 C (_In_ const C &h) noexcept; \
56 C& operator=(_In_ const C &h) noexcept;
61#define WINSTD_NONMOVABLE(C) \
63 C (_Inout_ C &&h) noexcept; \
64 C& operator=(_Inout_ C &&h) noexcept;
66#ifndef WINSTD_STACK_BUFFER_BYTES
80#define WINSTD_STACK_BUFFER_BYTES 1024
101#define PRINTF_LPTSTR "ls"
103#define PRINTF_LPTSTR "s"
110#define PRINTF_LPOLESTR "hs"
112#define PRINTF_LPOLESTR "ls"
116#define _tcin (std::wcin )
117#define _tcout (std::wcout)
118#define _tcerr (std::wcerr)
119#define _tclog (std::wclog)
121#define _tcin (std::cin )
122#define _tcout (std::cout)
123#define _tcerr (std::cerr)
124#define _tclog (std::clog)
138#define WINSTD_HANDLE_IMPL(C, INVAL) \
141 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
142 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
143 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
144 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
150#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
153 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
154 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
155 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
156 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
157 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
158 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
163#ifndef _FormatMessage_format_string_
164#define _FormatMessage_format_string_ _In_z_
167#ifndef _LPCBYTE_DEFINED
168#define _LPCBYTE_DEFINED
169typedef const BYTE *LPCBYTE;
175#pragma warning(disable: 4995)
176#pragma warning(disable: 4996)
177#pragma warning(disable: 4505)
193static int vsnprintf(_Out_z_cap_(capacity)
char *str, _In_
size_t capacity, _In_z_ _Printf_format_string_
const char *format, _In_ va_list arg)
195 return _vsnprintf(str, capacity, format, arg);
209static 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
211 return _vsnwprintf(str, capacity, format, arg);
223template<
class _Elem,
class _Traits,
class _Ax>
224static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, _In_ va_list arg)
229 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
232 str.assign(buf, count);
236 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
237 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
239 str.assign(buf_dyn.get(), count);
256template<
class _Elem,
class _Traits,
class _Ax>
257static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_
const _Elem *format, ...)
260 va_start(arg, format);
261 const int res = vsprintf(str, format, arg);
271template<
class _Traits,
class _Ax>
272static 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)
274 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
275 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPSTR
>((LPSTR*)
get_ptr(lpBuffer)), 0, Arguments);
277 str.assign(lpBuffer.get(), dwResult);
286template<
class _Traits,
class _Ax>
287static 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)
289 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
290 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId,
reinterpret_cast<LPWSTR
>((LPWSTR*)
get_ptr(lpBuffer)), 0, Arguments);
292 str.assign(lpBuffer.get(), dwResult);
369 template<
class _Other>
379 template<
class _Ty,
class _Dx>
390 m_ptr(owner.release())
402 other.m_ptr =
nullptr;
410 if (
m_ptr !=
nullptr)
419 operator typename _Ty**()
429 operator typename _Ty*&()
446 template<
class _Ty,
class _Dx>
460 template<
class _Ty,
class _Dx>
470 #pragma warning(push)
471 #pragma warning(disable: 26432)
472 template<
class _Ty,
class _Dx>
483 m_ptr(owner.release())
495 if (
this != &other) {
497 m_ptr = owner.release();
512 other.m_ptr =
nullptr;
524 if (
this != &other) {
527 other.m_ptr =
nullptr;
538 if (
m_ptr !=
nullptr)
547 operator typename _Ty**()
noexcept
557 operator typename _Ty*&()
578 template <
class T, const T INVAL>
623 handle<handle_type, INVAL>& operator=(_In_
const handle<handle_type, INVAL> &h)
noexcept {};
642 #pragma warning(suppress: 26432)
645 if (
this != std::addressof(h)) {
836 template <class T, const T INVAL>
842 template <class T, T INVAL>
876 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
898 if (
this != std::addressof(h)) {
923 #pragma warning(disable: 26432)
979 template <
typename _Tn>
1074 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1076 str.erase(str.find_last_not_of(_T(
" \t\n\r\f\v")) + 1);
1078 sprintf(str, m_num >= 0x10000 ? _T(
"Error 0x%X") : _T(
"Error %u"), m_num);
1091 template<
class _Elem,
class _Traits,
class _Ax>
1106 va_start(arg, format);
1107 vsprintf(*
this, format, arg);
1125 ATLENSURE(format.LoadString(hInstance, nFormatID));
1128 va_start(arg, nFormatID);
1129 vsprintf(*
this, format, arg);
1143 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1146 va_start(arg, nFormatID);
1147 vsprintf(*
this, format, arg);
1176 template<
class _Elem,
class _Traits,
class _Ax>
1191 va_start(arg, format);
1192 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1209 _Myt format(GetManager());
1210 ATLENSURE(format.LoadString(hInstance, nFormatID));
1213 va_start(arg, nFormatID);
1214 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1227 _Myt format(GetManager());
1228 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1231 va_start(arg, nFormatID);
1232 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *
this, &arg);
1243 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1245 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, Arguments);
1253 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1255 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *
this, (va_list*)Arguments);
1265 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, Arguments);
1275 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *
this, (va_list*)Arguments);
1301 template<
class _Elem,
class _Traits,
class _Ax>
1316 sprintf<_Elem, _Traits, _Ax>(*
this, format,
1320 guid.Data4[0], guid.Data4[1],
1321 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1342 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1364 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}")
1389 #pragma warning(push)
1390 #pragma warning(disable: 4100)
1408 template<
class _Other>
1431 template<
class _Other>
1442 SecureZeroMemory(_Ptr, _Size);
1443 _Mybase::deallocate(_Ptr, _Size);
1447 #pragma warning(pop)
1488 ZeroMemory(m_data, N);
1496 SecureZeroMemory(m_data, N);
1500 unsigned char m_data[N];
Base template class to support converting GUID to string.
Definition: Common.h:1303
basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1314
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1178
basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1243
basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1225
basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1273
basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1263
basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1253
basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1207
basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1188
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1093
basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1103
basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1122
basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1140
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:844
bool attach_duplicated(_In_opt_ handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:949
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:935
dplhandle< handle_type, INVAL > & operator=(_In_opt_ handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:885
dplhandle< handle_type, INVAL > & operator=(_In_ const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:896
virtual handle_type duplicate_internal(_In_ handle_type h) const noexcept=0
Abstract member function that must be implemented by child classes to do the actual object handle dup...
dplhandle(_In_opt_ handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:858
dplhandle< handle_type, INVAL > & operator=(_Inout_ dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:924
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:849
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:670
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
bool operator<=(_In_opt_ handle_type h) const
Is handle less than or equal to?
Definition: Common.h:730
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:595
handle(_In_opt_ handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:604
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:691
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:680
bool operator!=(_In_opt_ handle_type h) const
Is handle not equal to?
Definition: Common.h:769
bool operator==(_In_opt_ handle_type h) const
Is handle equal to?
Definition: Common.h:782
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
handle(_Inout_ handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:613
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:704
void free()
Destroys the object.
Definition: Common.h:816
bool operator>=(_In_opt_ handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:743
bool operator<(_In_opt_ handle_type h) const
Is handle less than?
Definition: Common.h:717
handle< handle_type, INVAL > & operator=(_In_opt_ handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:631
handle< handle_type, INVAL > & operator=(_Inout_ handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:643
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
bool operator>(_In_opt_ handle_type h) const
Is handle greater than?
Definition: Common.h:756
handle_type m_h
Object handle.
Definition: Common.h:833
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:806
Numerical runtime error.
Definition: Common.h:981
num_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: Common.h:992
error_type number() const
Returns the Windows error number.
Definition: Common.h:1013
_Tn error_type
Error number type.
Definition: Common.h:983
error_type m_num
Numeric error code.
Definition: Common.h:1019
num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1004
Helper class for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:474
ref_unique_ptr(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:481
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:563
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:536
ref_unique_ptr(_Inout_ ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:508
_Ty * m_ptr
Pointer.
Definition: Common.h:564
ref_unique_ptr & operator=(_Inout_ ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:522
ref_unique_ptr & operator=(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:493
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:381
ref_unique_ptr(_Inout_ std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:388
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:435
ref_unique_ptr(_Inout_ ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:398
_Ty * m_ptr
Pointer.
Definition: Common.h:436
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:408
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1401
void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1439
sanitizing_allocator(_In_ const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1424
sanitizing_allocator(_In_ const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1432
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1403
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1417
Sanitizing BLOB.
Definition: Common.h:1481
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1486
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1494
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1331
string_guid(_In_ const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1341
Windows runtime error.
Definition: Common.h:1026
tstring msg(_In_opt_ DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1071
win_runtime_error(_In_ const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1053
win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1044
win_runtime_error(_In_opt_z_ const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1062
win_runtime_error(_In_ error_type num, _In_ const std::string &msg)
Constructs an exception.
Definition: Common.h:1034
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1353
wstring_guid(_In_ const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1363
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:311
ref_unique_ptr< _Ty[], _Dx > get_ptr(_Inout_ std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:461
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:1465
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1473
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1456
static const T invalid
Invalid handle value.
Definition: Common.h:590
void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:359
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:354
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:349
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:370
Deleter for unique_ptr using LocalFree.
Definition: Common.h:319
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:320
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:330
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:337
LocalFree_delete()
Default construct.
Definition: Common.h:325
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1410
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1411