WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
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#define _WINSOCKAPI_ // Prevent inclusion of winsock.h in windows.h.
10#include <Windows.h>
11#include <assert.h>
12#include <stdarg.h>
13#include <tchar.h>
14#include <iostream>
15#include <memory>
16#include <stdexcept>
17#include <string>
18#include <vector>
19
25
29#ifndef __L
30#define __L(x) L ## x
31#endif
32
36#ifndef _L
37#define _L(x) __L(x)
38#endif
39
43#define WINSTD_STRING_IMPL(x) #x
44
48#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
49
53#define WINSTD_NONCOPYABLE(C) \
54private: \
55 C (_In_ const C &h) noexcept; \
56 C& operator=(_In_ const C &h) noexcept;
57
61#define WINSTD_NONMOVABLE(C) \
62private: \
63 C (_Inout_ C &&h) noexcept; \
64 C& operator=(_Inout_ C &&h) noexcept;
65
66#ifndef WINSTD_STACK_BUFFER_BYTES
80#define WINSTD_STACK_BUFFER_BYTES 1024
81#endif
82
84
96
100#ifdef UNICODE
101#define PRINTF_LPTSTR "ls"
102#else
103#define PRINTF_LPTSTR "s"
104#endif
105
109#ifdef OLE2ANSI
110#define PRINTF_LPOLESTR "hs"
111#else
112#define PRINTF_LPOLESTR "ls"
113#endif
114
118#ifdef _UNICODE
119#define _tcin (std::wcin )
120#else
121#define _tcin (std::cin )
122#endif
123
127#ifdef _UNICODE
128#define _tcout (std::wcout)
129#else
130#define _tcout (std::cout)
131#endif
132
136#ifdef _UNICODE
137#define _tcerr (std::wcerr)
138#else
139#define _tcerr (std::cerr)
140#endif
141
145#ifdef _UNICODE
146#define _tclog (std::wclog)
147#else
148#define _tclog (std::clog)
149#endif
150
152
158
162#define WINSTD_HANDLE_IMPL(C, INVAL) \
163public: \
164 C ( ) noexcept { } \
165 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
166 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
167 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
168 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
169WINSTD_NONCOPYABLE(C)
170
174#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
175public: \
176 C ( ) noexcept { } \
177 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
178 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
179 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
180 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
181 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
182 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
183private:
184
186
187#ifndef _FormatMessage_format_string_
188#define _FormatMessage_format_string_ _In_z_
189#endif
190
191#ifndef _LPCBYTE_DEFINED
192#define _LPCBYTE_DEFINED
193typedef const BYTE *LPCBYTE;
194#endif
195
196#pragma warning(push)
197// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
198// Threfore turn off compiler warning instead. ;)
199#pragma warning(disable: 4995)
200#pragma warning(disable: 4996)
201#pragma warning(disable: 4505) // Don't warn on unused code
202
205
216#if _MSC_VER <= 1600
217static int vsnprintf(_Out_z_cap_(capacity) char *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
218{
219 return _vsnprintf(str, capacity, format, arg);
220}
221#endif
222
233static 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
234{
235 return _vsnwprintf(str, capacity, format, arg);
236}
237
247template<class _Elem, class _Traits, class _Ax>
248static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
249{
250 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
251
252 // Try with stack buffer first.
253 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
254 if (count >= 0) {
255 // Copy from stack.
256 str.assign(buf, count);
257 } else {
258 for (size_t capacity = 2*WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem);; capacity *= 2) {
259 // Allocate on heap and retry.
260 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
261 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
262 if (count >= 0) {
263 str.assign(buf_dyn.get(), count);
264 break;
265 }
266 }
267 }
268
269 return count;
270}
271
280template<class _Elem, class _Traits, class _Ax>
281static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
282{
283 va_list arg;
284 va_start(arg, format);
285 const int res = vsprintf(str, format, arg);
286 va_end(arg);
287 return res;
288}
289
295template<class _Traits, class _Ax>
296static 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)
297{
298 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
299 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
300 if (dwResult)
301 str.assign(lpBuffer.get(), dwResult);
302 return dwResult;
303}
304
310template<class _Traits, class _Ax>
311static 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)
312{
313 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
314 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
315 if (dwResult)
316 str.assign(lpBuffer.get(), dwResult);
317 return dwResult;
318}
319
321
322#pragma warning(pop)
323
324namespace winstd
325{
328
332#ifdef _UNICODE
333 typedef std::wstring tstring;
334#else
335 typedef std::string tstring;
336#endif
337
341 template <class _Ty>
343 {
345
350
354 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
355
361 void operator()(_Ty *_Ptr) const
362 {
363 LocalFree(_Ptr);
364 }
365 };
366
370 template <class _Ty>
371 struct LocalFree_delete<_Ty[]>
372 {
374
378 LocalFree_delete() noexcept {}
379
383 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
384 {
385 LocalFree(_Ptr);
386 }
387
393 template<class _Other>
394 void operator()(_Other *) const
395 {
396 LocalFree(_Ptr);
397 }
398 };
399
403 template<class _Ty, class _Dx>
405 {
406 public:
412 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
413 m_own(owner),
414 m_ptr(owner.release())
415 {}
416
423 m_own(other.m_own),
424 m_ptr(other.m_ptr)
425 {
426 other.m_ptr = nullptr;
427 }
428
433 {
434 if (m_ptr != nullptr)
435 m_own.reset(m_ptr);
436 }
437
443 operator typename _Ty**()
444 {
445 return &m_ptr;
446 }
447
453 operator typename _Ty*&()
454 {
455 return m_ptr;
456 }
457
458 protected:
459 std::unique_ptr<_Ty, _Dx> &m_own;
460 _Ty *m_ptr;
461 };
462
470 template<class _Ty, class _Dx>
471 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
472 {
473 return ref_unique_ptr<_Ty, _Dx>(owner);
474 }
475
484 template<class _Ty, class _Dx>
485 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
486 {
487 return ref_unique_ptr<_Ty[], _Dx>(owner);
488 }
489
494 #pragma warning(push)
495 #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.
496 template<class _Ty, class _Dx>
497 class ref_unique_ptr<_Ty[], _Dx>
498 {
499 public:
505 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
506 m_own(owner),
507 m_ptr(owner.release())
508 {}
509
517 ref_unique_ptr& operator=(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept
518 {
519 if (this != &other) {
520 m_own = owner;
521 m_ptr = owner.release();
522 }
523
524 return *this;
525 }
526
532 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
533 m_own(other.m_own),
534 m_ptr(other.m_ptr)
535 {
536 other.m_ptr = nullptr;
537 }
538
546 ref_unique_ptr& operator=(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other)
547 {
548 if (this != &other) {
549 m_own = other.m_own;
550 m_ptr = other.m_ptr;
551 other.m_ptr = nullptr;
552 }
553
554 return *this;
555 }
556
561 {
562 if (m_ptr != nullptr)
563 m_own.reset(m_ptr);
564 }
565
571 operator typename _Ty**() noexcept
572 {
573 return &m_ptr;
574 }
575
581 operator typename _Ty*&()
582 {
583 return m_ptr;
584 }
585
586 protected:
587 std::unique_ptr<_Ty[], _Dx> &m_own;
588 _Ty *m_ptr;
589 };
590 #pragma warning(pop)
591
593
596
602 template <class T, const T INVAL>
603 class handle
604 {
605 public:
609 typedef T handle_type;
610
614 static const T invalid;
615
619 handle() noexcept : m_h(invalid)
620 {
621 }
622
628 handle(_In_opt_ handle_type h) noexcept : m_h(h)
629 {
630 }
631
637 handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
638 {
639 // Transfer handle.
640 m_h = h.m_h;
641 h.m_h = invalid;
642 }
643
644 private:
645 // This class is noncopyable.
646 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
647 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
648
649 public:
656 {
657 attach(h);
658 return *this;
659 }
660
666 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
668 {
669 if (this != std::addressof(h)) {
670 // Transfer handle.
671 if (m_h != invalid)
673 m_h = h.m_h;
674 h.m_h = invalid;
675 }
676 return *this;
677 }
678
684 operator handle_type() const
685 {
686 return m_h;
687 }
688
695 {
696 assert(m_h != invalid);
697 return *m_h;
698 }
699
705 {
706 assert(m_h == invalid);
707 return &m_h;
708 }
709
716 {
717 assert(m_h != invalid);
718 return m_h;
719 }
720
728 bool operator!() const
729 {
730 return m_h == invalid;
731 }
732
741 bool operator<(_In_opt_ handle_type h) const
742 {
743 return m_h < h;
744 }
745
754 bool operator<=(_In_opt_ handle_type h) const
755 {
756 return !operator>(h);
757 }
758
767 bool operator>=(_In_opt_ handle_type h) const
768 {
769 return !operator<(h);
770 }
771
780 bool operator>(_In_opt_ handle_type h) const
781 {
782 return h < m_h;
783 }
784
793 bool operator!=(_In_opt_ handle_type h) const
794 {
795 return !operator==(h);
796 }
797
806 bool operator==(_In_opt_ handle_type h) const
807 {
808 return m_h == h;
809 }
810
818 void attach(_In_opt_ handle_type h) noexcept
819 {
820 if (m_h != invalid)
822 m_h = h;
823 }
824
831 {
832 handle_type h = m_h;
833 m_h = invalid;
834 return h;
835 }
836
840 void free()
841 {
842 if (m_h != invalid) {
844 m_h = invalid;
845 }
846 }
847
848 protected:
852 virtual void free_internal() noexcept = 0;
853
854 protected:
856 };
857
858 template <class T, const T INVAL>
859 const T handle<T, INVAL>::invalid = INVAL;
860
864 template <class T, T INVAL>
865 class dplhandle : public handle<T, INVAL>
866 {
867 public:
871 dplhandle() noexcept
872 {
873 }
874
881 {
882 }
883
889 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) noexcept : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
890 {
891 }
892
898 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
899 {
900 }
901
908 {
910 return *this;
911 }
912
919 {
920 if (this != std::addressof(h)) {
921 if (h.m_h != invalid) {
922 handle_type h_new = duplicate_internal(h.m_h);
923 if (h_new != invalid) {
924 if (m_h != invalid)
926
927 m_h = h_new;
928 } else
929 assert(0); // Could not duplicate the handle
930 } else {
931 if (m_h != invalid)
933
934 m_h = invalid;
935 }
936 }
937 return *this;
938 }
939
945 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
947 {
949 return *this;
950 }
951
958 {
959 return m_h != invalid ? duplicate_internal(m_h) : invalid;
960 }
961
972 {
973 if (m_h != invalid)
975
976 return h != invalid ? (m_h = duplicate_internal(h)) != invalid : (m_h = invalid, true);
977 }
978
979 protected:
987 virtual handle_type duplicate_internal(_In_ handle_type h) const noexcept = 0;
988 };
989
991
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
1409
1410 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1411 #pragma warning(push)
1412 #pragma warning(disable: 4100)
1413
1421 template<class _Ty>
1422 class sanitizing_allocator : public std::allocator<_Ty>
1423 {
1424 public:
1425 typedef std::allocator<_Ty> _Mybase;
1426
1430 template<class _Other>
1431 struct rebind
1432 {
1434 };
1435
1440 {
1441 }
1442
1447 {
1448 }
1449
1453 template<class _Other>
1454 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1455 {
1456 }
1457
1461 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1462 {
1463 // Sanitize then free.
1464 SecureZeroMemory(_Ptr, _Size);
1465 _Mybase::deallocate(_Ptr, _Size);
1466 }
1467 };
1468
1469 #pragma warning(pop)
1470
1478 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1479
1487 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1488
1492#ifdef _UNICODE
1494#else
1496#endif
1497
1501 template<size_t N>
1503 {
1504 public:
1509 {
1510 ZeroMemory(m_data, N);
1511 }
1512
1517 {
1518 SecureZeroMemory(m_data, N);
1519 }
1520
1521 public:
1522 unsigned char m_data[N];
1523 };
1524
1526}
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 handle duplica...
Definition: Common.h:866
dplhandle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:907
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:957
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:946
bool attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:971
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:880
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:918
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:871
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:694
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:619
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:767
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:715
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:704
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:628
bool operator<(handle_type h) const
Is handle less than?
Definition: Common.h:741
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:655
bool operator!() const
Tests if the object handle is INVAL.
Definition: Common.h:728
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:667
bool operator!=(handle_type h) const
Is handle not equal to?
Definition: Common.h:793
void free()
Destroys the object.
Definition: Common.h:840
handle_type m_h
Object handle.
Definition: Common.h:855
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
bool operator==(handle_type h) const
Is handle equal to?
Definition: Common.h:806
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:637
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:830
bool operator>(handle_type h) const
Is handle greater than?
Definition: Common.h:780
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition: Common.h:754
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:498
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:587
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:532
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:560
ref_unique_ptr & operator=(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:517
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:505
_Ty * m_ptr
Pointer.
Definition: Common.h:588
ref_unique_ptr & operator=(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:546
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:405
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:459
_Ty * m_ptr
Pointer.
Definition: Common.h:460
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:422
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:432
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:412
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1423
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1446
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1461
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1454
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1425
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1439
Sanitizing BLOB.
Definition: Common.h:1503
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1508
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1516
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:80
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:485
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:335
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:1487
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1495
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1478
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
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
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1192
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:614
LocalFree_delete() noexcept
Default construct.
Definition: Common.h:378
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:373
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Common.h:394
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition: Common.h:383
Deleter for unique_ptr using LocalFree.
Definition: Common.h:343
LocalFree_delete< _Ty > _Myt
This type.
Definition: Common.h:344
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition: Common.h:354
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Common.h:361
LocalFree_delete()
Default construct.
Definition: Common.h:349
Convert this type to sanitizing_allocator<_Other>
Definition: Common.h:1432
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1433