WinStd
Windows Win32 API using Standard C++
Common.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include <Windows.h>
10#include <assert.h>
11#include <stdarg.h>
12#include <tchar.h>
13#include <iostream>
14#include <memory>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
35
38
42#ifndef __L
43#define __L(x) L ## x
44#endif
45
49#ifndef _L
50#define _L(x) __L(x)
51#endif
52
56#define WINSTD_STRING_IMPL(x) #x
57
61#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
62
66#define WINSTD_NONCOPYABLE(C) \
67private: \
68 C (_In_ const C &h) noexcept; \
69 C& operator=(_In_ const C &h) noexcept;
70
74#define WINSTD_NONMOVABLE(C) \
75private: \
76 C (_Inout_ C &&h) noexcept; \
77 C& operator=(_Inout_ C &&h) noexcept;
78
79#ifndef WINSTD_STACK_BUFFER_BYTES
93#define WINSTD_STACK_BUFFER_BYTES 1024
94#endif
95
97
100
104#ifdef UNICODE
105#define PRINTF_LPTSTR "ls"
106#else
107#define PRINTF_LPTSTR "s"
108#endif
109
113#ifdef OLE2ANSI
114#define PRINTF_LPOLESTR "hs"
115#else
116#define PRINTF_LPOLESTR "ls"
117#endif
118
122#ifdef _UNICODE
123#define _tcin (std::wcin )
124#else
125#define _tcin (std::cin )
126#endif
127
131#ifdef _UNICODE
132#define _tcout (std::wcout)
133#else
134#define _tcout (std::cout)
135#endif
136
140#ifdef _UNICODE
141#define _tcerr (std::wcerr)
142#else
143#define _tcerr (std::cerr)
144#endif
145
149#ifdef _UNICODE
150#define _tclog (std::wclog)
151#else
152#define _tclog (std::clog)
153#endif
154
156
159
163#define WINSTD_HANDLE_IMPL(C, INVAL) \
164public: \
165 C ( ) noexcept { } \
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; } \
170WINSTD_NONCOPYABLE(C)
171
175#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
176public: \
177 C ( ) noexcept { } \
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; } \
184private:
185
187
188#ifndef _FormatMessage_format_string_
189#define _FormatMessage_format_string_ _In_z_
190#endif
191
193#ifndef _LPCBYTE_DEFINED
194#define _LPCBYTE_DEFINED
195typedef const BYTE *LPCBYTE;
196#endif
198
199#pragma warning(push)
200// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
201// Threfore turn off compiler warning instead. ;)
202#pragma warning(disable: 4995)
203#pragma warning(disable: 4996)
204#pragma warning(disable: 4505) // Don't warn on unused code
205
208
219#if _MSC_VER <= 1600
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)
221{
222 return _vsnprintf(str, capacity, format, arg);
223}
224#endif
225
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
237{
238 return _vsnwprintf(str, capacity, format, arg);
239}
240
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)
252{
253 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
254
255 // Try with stack buffer first.
256 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
257 if (count >= 0) {
258 // Copy from stack.
259 str.assign(buf, count);
260 } else {
261 for (size_t capacity = 2*WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem);; capacity *= 2) {
262 // Allocate on heap and retry.
263 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
264 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
265 if (count >= 0) {
266 str.assign(buf_dyn.get(), count);
267 break;
268 }
269 }
270 }
271
272 return count;
273}
274
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, ...)
285{
286 va_list arg;
287 va_start(arg, format);
288 const int res = vsprintf(str, format, arg);
289 va_end(arg);
290 return res;
291}
292
298template<class _Traits, class _Ax>
299static 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)
300{
301 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
302 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
303 if (dwResult)
304 str.assign(lpBuffer.get(), dwResult);
305 return dwResult;
306}
307
313template<class _Traits, class _Ax>
314static 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)
315{
316 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
317 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
318 if (dwResult)
319 str.assign(lpBuffer.get(), dwResult);
320 return dwResult;
321}
322
324
325#pragma warning(pop)
326
327namespace winstd
328{
331
335#ifdef _UNICODE
336 typedef std::wstring tstring;
337#else
338 typedef std::string tstring;
339#endif
340
344 template <class _Ty>
346 {
348
353
357 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
358
364 void operator()(_Ty *_Ptr) const
365 {
366 LocalFree(_Ptr);
367 }
368 };
369
373 template <class _Ty>
374 struct LocalFree_delete<_Ty[]>
375 {
377
381 LocalFree_delete() noexcept {}
382
386 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
387 {
388 LocalFree(_Ptr);
389 }
390
396 template<class _Other>
397 void operator()(_Other *) const
398 {
399 LocalFree(_Ptr);
400 }
401 };
402
406 template<class _Ty, class _Dx>
408 {
409 public:
415 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
416 m_own(owner),
417 m_ptr(owner.release())
418 {}
419
426 m_own(other.m_own),
427 m_ptr(other.m_ptr)
428 {
429 other.m_ptr = nullptr;
430 }
431
436 {
437 if (m_ptr != nullptr)
438 m_own.reset(m_ptr);
439 }
440
446 operator typename _Ty**()
447 {
448 return &m_ptr;
449 }
450
456 operator typename _Ty*&()
457 {
458 return m_ptr;
459 }
460
461 protected:
462 std::unique_ptr<_Ty, _Dx> &m_own;
463 _Ty *m_ptr;
464 };
465
473 template<class _Ty, class _Dx>
474 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
475 {
476 return ref_unique_ptr<_Ty, _Dx>(owner);
477 }
478
487 template<class _Ty, class _Dx>
488 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
489 {
490 return ref_unique_ptr<_Ty[], _Dx>(owner);
491 }
492
497 #pragma warning(push)
498 #pragma warning(disable: 26432) // Copy constructor and assignment operator are also present, but not detected by code analysis as they are using base type source object reference.
499 template<class _Ty, class _Dx>
500 class ref_unique_ptr<_Ty[], _Dx>
501 {
502 public:
508 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
509 m_own(owner),
510 m_ptr(owner.release())
511 {}
512
520 ref_unique_ptr& operator=(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
521 {
522 if (this != &other) {
523 m_own = owner;
524 m_ptr = owner.release();
525 }
526
527 return *this;
528 }
529
535 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
536 m_own(other.m_own),
537 m_ptr(other.m_ptr)
538 {
539 other.m_ptr = nullptr;
540 }
541
549 ref_unique_ptr& operator=(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other)
550 {
551 if (this != &other) {
552 m_own = other.m_own;
553 m_ptr = other.m_ptr;
554 other.m_ptr = nullptr;
555 }
556
557 return *this;
558 }
559
564 {
565 if (m_ptr != nullptr)
566 m_own.reset(m_ptr);
567 }
568
574 operator typename _Ty**() noexcept
575 {
576 return &m_ptr;
577 }
578
584 operator typename _Ty*&()
585 {
586 return m_ptr;
587 }
588
589 protected:
590 std::unique_ptr<_Ty[], _Dx> &m_own;
591 _Ty *m_ptr;
592 };
593 #pragma warning(pop)
594
596
599
605 template <class T, const T INVAL>
606 class handle
607 {
608 public:
612 typedef T handle_type;
613
617 static const T invalid;
618
622 handle() noexcept : m_h(invalid)
623 {
624 }
625
631 handle(_In_opt_ handle_type h) noexcept : m_h(h)
632 {
633 }
634
640 handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
641 {
642 // Transfer handle.
643 m_h = h.m_h;
644 h.m_h = invalid;
645 }
646
647 private:
648 // This class is noncopyable.
649 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
650 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
651
652 public:
659 {
660 attach(h);
661 return *this;
662 }
663
669 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
671 {
672 if (this != std::addressof(h)) {
673 // Transfer handle.
674 if (m_h != invalid)
676 m_h = h.m_h;
677 h.m_h = invalid;
678 }
679 return *this;
680 }
681
687 operator handle_type() const
688 {
689 return m_h;
690 }
691
698 {
699 assert(m_h != invalid);
700 return *m_h;
701 }
702
708 {
709 assert(m_h == invalid);
710 return &m_h;
711 }
712
719 {
720 assert(m_h != invalid);
721 return m_h;
722 }
723
731 bool operator!() const
732 {
733 return m_h == invalid;
734 }
735
744 bool operator<(_In_opt_ handle_type h) const
745 {
746 return m_h < h;
747 }
748
757 bool operator<=(_In_opt_ handle_type h) const
758 {
759 return !operator>(h);
760 }
761
770 bool operator>=(_In_opt_ handle_type h) const
771 {
772 return !operator<(h);
773 }
774
783 bool operator>(_In_opt_ handle_type h) const
784 {
785 return h < m_h;
786 }
787
796 bool operator!=(_In_opt_ handle_type h) const
797 {
798 return !operator==(h);
799 }
800
809 bool operator==(_In_opt_ handle_type h) const
810 {
811 return m_h == h;
812 }
813
821 void attach(_In_opt_ handle_type h) noexcept
822 {
823 if (m_h != invalid)
825 m_h = h;
826 }
827
834 {
835 handle_type h = m_h;
836 m_h = invalid;
837 return h;
838 }
839
843 void free()
844 {
845 if (m_h != invalid) {
847 m_h = invalid;
848 }
849 }
850
851 protected:
855 virtual void free_internal() noexcept = 0;
856
857 protected:
859 };
860
861 template <class T, const T INVAL>
862 const T handle<T, INVAL>::invalid = INVAL;
863
867 template <class T, T INVAL>
868 class dplhandle : public handle<T, INVAL>
869 {
870 public:
874 dplhandle() noexcept
875 {
876 }
877
884 {
885 }
886
892 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) noexcept : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
893 {
894 }
895
901 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
902 {
903 }
904
911 {
913 return *this;
914 }
915
922 {
923 if (this != std::addressof(h)) {
924 if (h.m_h != invalid) {
925 handle_type h_new = duplicate_internal(h.m_h);
926 if (h_new != invalid) {
927 if (m_h != invalid)
929
930 m_h = h_new;
931 } else
932 assert(0); // Could not duplicate the handle
933 } else {
934 if (m_h != invalid)
936
937 m_h = invalid;
938 }
939 }
940 return *this;
941 }
942
948 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
950 {
952 return *this;
953 }
954
961 {
962 return m_h != invalid ? duplicate_internal(m_h) : invalid;
963 }
964
975 {
976 if (m_h != invalid)
978
979 return h != invalid ? (m_h = duplicate_internal(h)) != invalid : (m_h = invalid, true);
980 }
981
982 protected:
990 virtual handle_type duplicate_internal(_In_ handle_type h) const noexcept = 0;
991 };
992
994
997
1001 template <typename _Tn>
1002 class num_runtime_error : public std::runtime_error
1003 {
1004 public:
1005 typedef _Tn error_type;
1006
1007 public:
1014 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1015 m_num(num),
1016 runtime_error(msg)
1017 {
1018 }
1019
1026 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1027 m_num(num),
1028 runtime_error(msg)
1029 {
1030 }
1031
1036 {
1037 return m_num;
1038 }
1039
1040 protected:
1042 };
1043
1048 {
1049 public:
1056 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg)
1057 {
1058 }
1059
1066 win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(num, msg)
1067 {
1068 }
1069
1075 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg)
1076 {
1077 }
1078
1084 win_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(GetLastError(), msg)
1085 {
1086 }
1087
1093 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
1094 {
1095 tstring str;
1096 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1097 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1098 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
1099 } else
1100 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
1101 return str;
1102 }
1103 };
1104
1106
1109
1113 template<class _Elem, class _Traits, class _Ax>
1114 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1115 {
1116 public:
1119
1125 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1126 {
1127 va_list arg;
1128 va_start(arg, format);
1129 vsprintf(*this, format, arg);
1130 va_end(arg);
1131 }
1132
1134
1137
1144 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1145 {
1146 _Myt format;
1147 ATLENSURE(format.LoadString(hInstance, nFormatID));
1148
1149 va_list arg;
1150 va_start(arg, nFormatID);
1151 vsprintf(*this, format, arg);
1152 va_end(arg);
1153 }
1154
1162 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1163 {
1164 _Myt format;
1165 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1166
1167 va_list arg;
1168 va_start(arg, nFormatID);
1169 vsprintf(*this, format, arg);
1170 va_end(arg);
1171 }
1172
1174 };
1175
1180
1185
1189#ifdef _UNICODE
1191#else
1193#endif
1194
1198 template<class _Elem, class _Traits, class _Ax>
1199 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1200 {
1201 public:
1204
1210 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1211 {
1212 va_list arg;
1213 va_start(arg, format);
1214 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1215 va_end(arg);
1216 }
1217
1219
1222
1229 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1230 {
1231 _Myt format(GetManager());
1232 ATLENSURE(format.LoadString(hInstance, nFormatID));
1233
1234 va_list arg;
1235 va_start(arg, nFormatID);
1236 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1237 va_end(arg);
1238 }
1239
1247 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1248 {
1249 _Myt format(GetManager());
1250 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1251
1252 va_list arg;
1253 va_start(arg, nFormatID);
1254 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1255 va_end(arg);
1256 }
1257
1259
1265 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1266 {
1267 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1268 }
1269
1275 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1276 {
1277 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1278 }
1279
1285 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1286 {
1287 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1288 }
1289
1295 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1296 {
1297 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1298 }
1299 };
1300
1305
1310
1314#ifdef _UNICODE
1315 typedef wstring_msg tstring_msg;
1316#else
1318#endif
1319
1323 template<class _Elem, class _Traits, class _Ax>
1324 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1325 {
1326 public:
1329
1336 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1337 {
1338 sprintf<_Elem, _Traits, _Ax>(*this, format,
1339 guid.Data1,
1340 guid.Data2,
1341 guid.Data3,
1342 guid.Data4[0], guid.Data4[1],
1343 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1344 }
1345
1347 };
1348
1352 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1353 {
1354 public:
1357
1363 string_guid(_In_ const GUID &guid) :
1364 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1365 {
1366 }
1367
1369 };
1370
1374 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1375 {
1376 public:
1379
1385 wstring_guid(_In_ const GUID &guid) :
1386 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}")
1387 {
1388 }
1389
1391 };
1392
1396#ifdef _UNICODE
1397 typedef wstring_guid tstring_guid;
1398#else
1400#endif
1401
1403
1406
1407 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1408 #pragma warning(push)
1409 #pragma warning(disable: 4100)
1410
1418 template<class _Ty>
1419 class sanitizing_allocator : public std::allocator<_Ty>
1420 {
1421 public:
1422 typedef std::allocator<_Ty> _Mybase;
1423
1427 template<class _Other>
1428 struct rebind
1429 {
1431 };
1432
1437 {
1438 }
1439
1444 {
1445 }
1446
1450 template<class _Other>
1451 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1452 {
1453 }
1454
1458 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1459 {
1460 // Sanitize then free.
1461 SecureZeroMemory(_Ptr, _Size);
1462 _Mybase::deallocate(_Ptr, _Size);
1463 }
1464 };
1465
1466 #pragma warning(pop)
1467
1475 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1476
1484 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1485
1489#ifdef _UNICODE
1491#else
1493#endif
1494
1498 template<size_t N>
1500 {
1501 public:
1506 {
1507 ZeroMemory(m_data, N);
1508 }
1509
1514 {
1515 SecureZeroMemory(m_data, N);
1516 }
1517
1518 public:
1519 unsigned char m_data[N];
1520 };
1521
1523}
Base template class to support converting GUID to string.
Definition: Common.h:1325
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:1336
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1200
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:1275
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1285
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:1247
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:1265
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1210
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1229
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1295
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1115
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1125
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:1162
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1144
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition: Common.h:869
dplhandle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:910
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:960
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:949
bool attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:974
virtual handle_type duplicate_internal(handle_type h) const noexcept=0
Abstract member function that must be implemented by child classes to do the actual object handle dup...
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:883
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:921
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:874
Base abstract template class to support generic object handle keeping.
Definition: Common.h:607
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:697
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:622
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:770
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:718
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:707
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:612
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:631
bool operator<(handle_type h) const
Is handle less than?
Definition: Common.h:744
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:658
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:731
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:670
bool operator!=(handle_type h) const
Is handle not equal to?
Definition: Common.h:796
void free()
Destroys the object.
Definition: Common.h:843
handle_type m_h
Object handle.
Definition: Common.h:858
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:821
bool operator==(handle_type h) const
Is handle equal to?
Definition: Common.h:809
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:640
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:833
bool operator>(handle_type h) const
Is handle greater than?
Definition: Common.h:783
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition: Common.h:757
Numerical runtime error.
Definition: Common.h:1003
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1026
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1014
error_type number() const
Returns the Windows error number.
Definition: Common.h:1035
_Tn error_type
Error number type.
Definition: Common.h:1005
error_type m_num
Numeric error code.
Definition: Common.h:1041
Helper class for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:501
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:590
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:535
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:563
ref_unique_ptr & operator=(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:520
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:508
_Ty * m_ptr
Pointer.
Definition: Common.h:591
ref_unique_ptr & operator=(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:549
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:408
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:462
_Ty * m_ptr
Pointer.
Definition: Common.h:463
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:425
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:435
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:415
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1420
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1443
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1458
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1451
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1422
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1436
Sanitizing BLOB.
Definition: Common.h:1500
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1505
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1513
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1353
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1363
Windows runtime error.
Definition: Common.h:1048
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1093
win_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1066
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1075
win_runtime_error(const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1084
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1056
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1375
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1385
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:93
ref_unique_ptr< _Ty[], _Dx > get_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr (specialization for arrays)
Definition: Common.h:488
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:338
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:1484
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1492
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1475
basic_string_printf< wchar_t, std::char_traits< wchar_t >, std::allocator< wchar_t > > wstring_printf
Wide character implementation of a class to support string formatting using printf() style templates.
Definition: Common.h:1184
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition: Common.h:1399
basic_string_msg< wchar_t, std::char_traits< wchar_t >, std::allocator< wchar_t > > wstring_msg
Wide character implementation of a class to support string formatting using FormatMessage() style tem...
Definition: Common.h:1309
static int vsprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format, va_list arg)
Formats string using printf().
Definition: Common.h:251
static DWORD FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< char, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition: Common.h:299
basic_string_printf< char, std::char_traits< char >, std::allocator< char > > string_printf
Single-byte character implementation of a class to support string formatting using printf() style tem...
Definition: Common.h:1179
static int vsnprintf(char *str, size_t capacity, const char *format, va_list arg)
Formats string using printf().
Definition: Common.h:220
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1192
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition: Common.h:284
basic_string_msg< char, std::char_traits< char >, std::allocator< char > > string_msg
Single-byte character implementation of a class to support string formatting using FormatMessage() st...
Definition: Common.h:1304
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1317
static const T invalid
Invalid handle value.
Definition: Common.h:617
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:381
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:376
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:397
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:386
Deleter for unique_ptr using LocalFree.
Definition: Common.h:346
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:347
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:357
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:364
LocalFree_delete()
Default construct.
Definition: Common.h:352
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1429
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1430