WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Common.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2023 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
407 {
412
418 void operator()(HGLOBAL _Ptr) const
419 {
420 GlobalFree(_Ptr);
421 }
422 };
423
427 template <class T>
429 {
432
433 public:
439 globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
440 {
441 m_data = (T*)GlobalLock(hMem);
442 if (!m_data)
443 throw win_runtime_error("GlobalLock failed");
444 }
445
452 {
453 GlobalUnlock(m_h);
454 }
455
459 T* data() const noexcept
460 {
461 return m_data;
462 }
463
464 protected:
465 HGLOBAL m_h;
467 };
468
472 template<class _Ty, class _Dx>
474 {
475 public:
481 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
482 m_own(owner),
483 m_ptr(owner.release())
484 {}
485
492 m_own(other.m_own),
493 m_ptr(other.m_ptr)
494 {
495 other.m_ptr = nullptr;
496 }
497
502 {
503 if (m_ptr != nullptr)
504 m_own.reset(m_ptr);
505 }
506
512 operator typename _Ty**()
513 {
514 return &m_ptr;
515 }
516
522 operator typename _Ty*&()
523 {
524 return m_ptr;
525 }
526
527 protected:
528 std::unique_ptr<_Ty, _Dx> &m_own;
529 _Ty *m_ptr;
530 };
531
539 template<class _Ty, class _Dx>
540 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
541 {
542 return ref_unique_ptr<_Ty, _Dx>(owner);
543 }
544
549 template<class _Ty, class _Dx>
550 class ref_unique_ptr<_Ty[], _Dx>
551 {
552 public:
558 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
559 m_own(owner),
560 m_ptr(owner.release())
561 {}
562
568 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
569 m_own(other.m_own),
570 m_ptr(other.m_ptr)
571 {
572 other.m_ptr = nullptr;
573 }
574
579 {
580 if (m_ptr != nullptr)
581 m_own.reset(m_ptr);
582 }
583
589 operator typename _Ty**() noexcept
590 {
591 return &m_ptr;
592 }
593
599 operator typename _Ty*&()
600 {
601 return m_ptr;
602 }
603
604 protected:
605 std::unique_ptr<_Ty[], _Dx> &m_own;
606 _Ty *m_ptr;
607 };
608
617 template<class _Ty, class _Dx>
618 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx>& owner) noexcept
619 {
620 return ref_unique_ptr<_Ty[], _Dx>(owner);
621 }
622
624
627
633 template <class T, const T INVAL>
634 class handle
635 {
636 public:
640 typedef T handle_type;
641
645 static const T invalid;
646
650 handle() noexcept : m_h(invalid)
651 {
652 }
653
659 handle(_In_opt_ handle_type h) noexcept : m_h(h)
660 {
661 }
662
668 handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
669 {
670 // Transfer handle.
671 m_h = h.m_h;
672 h.m_h = invalid;
673 }
674
675 private:
676 // This class is noncopyable.
677 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
678 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
679
680 public:
687 {
688 attach(h);
689 return *this;
690 }
691
697 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
699 {
700 if (this != std::addressof(h)) {
701 // Transfer handle.
702 if (m_h != invalid)
704 m_h = h.m_h;
705 h.m_h = invalid;
706 }
707 return *this;
708 }
709
715 operator handle_type() const
716 {
717 return m_h;
718 }
719
726 {
727 assert(m_h != invalid);
728 return *m_h;
729 }
730
736 {
737 assert(m_h == invalid);
738 return &m_h;
739 }
740
747 {
748 assert(m_h != invalid);
749 return m_h;
750 }
751
762 bool operator!() const
763 {
764 return m_h == invalid;
765 }
766
775 bool operator<(_In_opt_ handle_type h) const
776 {
777 return m_h < h;
778 }
779
788 bool operator<=(_In_opt_ handle_type h) const
789 {
790 return !operator>(h);
791 }
792
801 bool operator>=(_In_opt_ handle_type h) const
802 {
803 return !operator<(h);
804 }
805
814 bool operator>(_In_opt_ handle_type h) const
815 {
816 return h < m_h;
817 }
818
827 bool operator!=(_In_opt_ handle_type h) const
828 {
829 return !operator==(h);
830 }
831
840 bool operator==(_In_opt_ handle_type h) const
841 {
842 return m_h == h;
843 }
844
852 void attach(_In_opt_ handle_type h) noexcept
853 {
854 if (m_h != invalid)
856 m_h = h;
857 }
858
865 {
866 handle_type h = m_h;
867 m_h = invalid;
868 return h;
869 }
870
874 void free()
875 {
876 if (m_h != invalid) {
878 m_h = invalid;
879 }
880 }
881
882 protected:
886 virtual void free_internal() noexcept = 0;
887
888 protected:
890 };
891
892 template <class T, const T INVAL>
893 const T handle<T, INVAL>::invalid = INVAL;
894
898 template <class T, T INVAL>
899 class dplhandle : public handle<T, INVAL>
900 {
901 public:
905 dplhandle() noexcept
906 {
907 }
908
915 {
916 }
917
923 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
924 {
925 }
926
932 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
933 {
934 }
935
942 {
944 return *this;
945 }
946
953 {
954 if (this != std::addressof(h)) {
955 if (h.m_h != invalid) {
956 handle_type h_new = duplicate_internal(h.m_h);
957
958 if (m_h != invalid)
960
961 m_h = h_new;
962 } else {
963 if (m_h != invalid)
965
966 m_h = invalid;
967 }
968 }
969 return *this;
970 }
971
977 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
979 {
981 return *this;
982 }
983
990 {
991 return m_h != invalid ? duplicate_internal(m_h) : invalid;
992 }
993
1000 {
1001 if (m_h != invalid)
1002 free_internal();
1003
1004 m_h = h != invalid ? duplicate_internal(h) : invalid;
1005 }
1006
1007 protected:
1016 virtual handle_type duplicate_internal(_In_ handle_type h) const = 0;
1017 };
1018
1020
1023
1027 template <typename _Tn>
1028 class num_runtime_error : public std::runtime_error
1029 {
1030 public:
1031 typedef _Tn error_type;
1032
1033 public:
1040 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1041 m_num(num),
1042 runtime_error(msg)
1043 {
1044 }
1045
1052 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1053 m_num(num),
1054 runtime_error(msg)
1055 {
1056 }
1057
1062 {
1063 return m_num;
1064 }
1065
1066 protected:
1068 };
1069
1074 {
1075 public:
1082 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg)
1083 {
1084 }
1085
1092 win_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(num, msg)
1093 {
1094 }
1095
1101 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg)
1102 {
1103 }
1104
1110 win_runtime_error(_In_opt_z_ const char *msg = nullptr) : num_runtime_error<DWORD>(GetLastError(), msg)
1111 {
1112 }
1113
1119 tstring msg(_In_opt_ DWORD dwLanguageId = 0) const
1120 {
1121 tstring str;
1122 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, m_num, dwLanguageId, str, NULL)) {
1123 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1124 str.erase(str.find_last_not_of(_T(" \t\n\r\f\v")) + 1);
1125 } else
1126 sprintf(str, m_num >= 0x10000 ? _T("Error 0x%X") : _T("Error %u"), m_num);
1127 return str;
1128 }
1129 };
1130
1132
1135
1139 template<class _Elem, class _Traits, class _Ax>
1140 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1141 {
1142 public:
1145
1151 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1152 {
1153 va_list arg;
1154 va_start(arg, format);
1155 vsprintf(*this, format, arg);
1156 va_end(arg);
1157 }
1158
1160
1163
1170 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1171 {
1172 _Myt format;
1173 ATLENSURE(format.LoadString(hInstance, nFormatID));
1174
1175 va_list arg;
1176 va_start(arg, nFormatID);
1177 vsprintf(*this, format, arg);
1178 va_end(arg);
1179 }
1180
1188 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1189 {
1190 _Myt format;
1191 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1192
1193 va_list arg;
1194 va_start(arg, nFormatID);
1195 vsprintf(*this, format, arg);
1196 va_end(arg);
1197 }
1198
1200 };
1201
1206
1211
1215#ifdef _UNICODE
1217#else
1219#endif
1220
1224 template<class _Elem, class _Traits, class _Ax>
1225 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1226 {
1227 public:
1230
1236 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1237 {
1238 va_list arg;
1239 va_start(arg, format);
1240 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1241 va_end(arg);
1242 }
1243
1245
1248
1255 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1256 {
1257 _Myt format(GetManager());
1258 ATLENSURE(format.LoadString(hInstance, nFormatID));
1259
1260 va_list arg;
1261 va_start(arg, nFormatID);
1262 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1263 va_end(arg);
1264 }
1265
1273 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1274 {
1275 _Myt format(GetManager());
1276 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1277
1278 va_list arg;
1279 va_start(arg, nFormatID);
1280 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1281 va_end(arg);
1282 }
1283
1285
1291 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1292 {
1293 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1294 }
1295
1301 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1302 {
1303 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1304 }
1305
1311 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1312 {
1313 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1314 }
1315
1321 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1322 {
1323 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1324 }
1325 };
1326
1331
1336
1340#ifdef _UNICODE
1341 typedef wstring_msg tstring_msg;
1342#else
1344#endif
1345
1349 template<class _Elem, class _Traits, class _Ax>
1350 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1351 {
1352 public:
1355
1362 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1363 {
1364 sprintf<_Elem, _Traits, _Ax>(*this, format,
1365 guid.Data1,
1366 guid.Data2,
1367 guid.Data3,
1368 guid.Data4[0], guid.Data4[1],
1369 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1370 }
1371
1373 };
1374
1378 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1379 {
1380 public:
1383
1389 string_guid(_In_ const GUID &guid) :
1390 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1391 {
1392 }
1393
1395 };
1396
1400 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1401 {
1402 public:
1405
1411 wstring_guid(_In_ const GUID &guid) :
1412 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}")
1413 {
1414 }
1415
1417 };
1418
1422#ifdef _UNICODE
1423 typedef wstring_guid tstring_guid;
1424#else
1426#endif
1427
1429
1432
1433 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1434 #pragma warning(push)
1435 #pragma warning(disable: 4100)
1436
1444 template<class _Ty>
1445 class sanitizing_allocator : public std::allocator<_Ty>
1446 {
1447 public:
1448 typedef std::allocator<_Ty> _Mybase;
1449
1453 template<class _Other>
1454 struct rebind
1455 {
1457 };
1458
1463 {
1464 }
1465
1470 {
1471 }
1472
1476 template<class _Other>
1477 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1478 {
1479 }
1480
1484 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1485 {
1486 // Sanitize then free.
1487 SecureZeroMemory(_Ptr, _Size);
1488 _Mybase::deallocate(_Ptr, _Size);
1489 }
1490 };
1491
1492 #pragma warning(pop)
1493
1501 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1502
1510 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1511
1515#ifdef _UNICODE
1517#else
1519#endif
1520
1524 template<size_t N>
1526 {
1527 public:
1532 {
1533 ZeroMemory(m_data, N);
1534 }
1535
1540 {
1541 SecureZeroMemory(m_data, N);
1542 }
1543
1544 public:
1545 unsigned char m_data[N];
1546 };
1547
1549}
Base template class to support converting GUID to string.
Definition: Common.h:1351
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:1362
Base template class to support string formatting using FormatMessage() style templates.
Definition: Common.h:1226
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:1301
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1311
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:1273
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:1291
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition: Common.h:1236
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition: Common.h:1255
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition: Common.h:1321
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1141
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition: Common.h:1151
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:1188
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition: Common.h:1170
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition: Common.h:900
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:941
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition: Common.h:989
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition: Common.h:978
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition: Common.h:999
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:914
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition: Common.h:952
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition: Common.h:905
Context scope automatic GlobalAlloc (un)access.
Definition: Common.h:429
HGLOBAL m_h
memory handle
Definition: Common.h:465
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition: Common.h:451
T * m_data
memory pointer
Definition: Common.h:466
T * data() const noexcept
Return data pointer.
Definition: Common.h:459
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:439
Base abstract template class to support generic object handle keeping.
Definition: Common.h:635
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition: Common.h:725
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:650
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition: Common.h:801
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition: Common.h:746
handle_type * operator&()
Returns the object handle reference.
Definition: Common.h:735
T handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:640
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition: Common.h:659
bool operator<(handle_type h) const
Is handle less than?
Definition: Common.h:775
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition: Common.h:686
bool operator!() const
Tests if the object handle is invalid.
Definition: Common.h:762
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition: Common.h:698
bool operator!=(handle_type h) const
Is handle not equal to?
Definition: Common.h:827
void free()
Destroys the object.
Definition: Common.h:874
handle_type m_h
Object handle.
Definition: Common.h:889
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:852
bool operator==(handle_type h) const
Is handle equal to?
Definition: Common.h:840
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition: Common.h:668
handle_type detach()
Dismisses the object handle from this class.
Definition: Common.h:864
bool operator>(handle_type h) const
Is handle greater than?
Definition: Common.h:814
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition: Common.h:788
Numerical runtime error.
Definition: Common.h:1029
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1052
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1040
error_type number() const
Returns the Windows error number.
Definition: Common.h:1061
_Tn error_type
Error number type.
Definition: Common.h:1031
error_type m_num
Numeric error code.
Definition: Common.h:1067
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:605
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition: Common.h:568
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:578
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition: Common.h:558
_Ty * m_ptr
Pointer.
Definition: Common.h:606
Helper class for returning pointers to std::unique_ptr.
Definition: Common.h:474
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition: Common.h:528
_Ty * m_ptr
Pointer.
Definition: Common.h:529
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition: Common.h:491
~ref_unique_ptr()
Returns ownership of the pointer.
Definition: Common.h:501
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition: Common.h:481
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition: Common.h:1446
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition: Common.h:1469
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition: Common.h:1484
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition: Common.h:1477
std::allocator< _Ty > _Mybase
Base type.
Definition: Common.h:1448
sanitizing_allocator() noexcept
Construct default allocator.
Definition: Common.h:1462
Sanitizing BLOB.
Definition: Common.h:1526
sanitizing_blob()
Constructs uninitialized BLOB.
Definition: Common.h:1531
~sanitizing_blob()
Sanitizes BLOB.
Definition: Common.h:1539
Single-byte character implementation of a class to support converting GUID to string.
Definition: Common.h:1379
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1389
Windows runtime error.
Definition: Common.h:1074
tstring msg(DWORD dwLanguageId=0) const
Returns a user-readable Windows error message.
Definition: Common.h:1119
win_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition: Common.h:1092
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition: Common.h:1101
win_runtime_error(const char *msg=nullptr)
Constructs an exception using GetLastError()
Definition: Common.h:1110
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition: Common.h:1082
Wide character implementation of a class to support converting GUID to string.
Definition: Common.h:1401
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition: Common.h:1411
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:66
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:93
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition: Common.h:338
ref_unique_ptr< _Ty, _Dx > get_ptr(std::unique_ptr< _Ty, _Dx > &owner) noexcept
Helper function template for returning pointers to std::unique_ptr.
Definition: Common.h:540
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:74
std::basic_string< wchar_t, std::char_traits< wchar_t >, sanitizing_allocator< wchar_t > > sanitizing_wstring
A sanitizing variant of std::wstring.
Definition: Common.h:1510
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition: Common.h:1518
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition: Common.h:1501
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:1210
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition: Common.h:1425
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:1335
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:1205
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:1218
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:1330
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition: Common.h:1343
static const T invalid
Invalid handle value.
Definition: Common.h:645
Deleter for unique_ptr using GlobalFree.
Definition: Common.h:407
GlobalFree_delete()
Default construct.
Definition: Common.h:411
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition: Common.h:418
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:1455
sanitizing_allocator< _Other > other
Other type.
Definition: Common.h:1456