WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Common.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include <Windows.h>
10#include <assert.h>
11#include <intsafe.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
36
39
43#ifndef __L
44#define __L(x) L ## x
45#endif
46
50#ifndef _L
51#define _L(x) __L(x)
52#endif
53
57#define WINSTD_STRING_IMPL(x) #x
58
62#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
63
67#define WINSTD_NONCOPYABLE(C) \
68private: \
69 C (_In_ const C &h) noexcept; \
70 C& operator=(_In_ const C &h) noexcept;
71
75#define WINSTD_NONMOVABLE(C) \
76private: \
77 C (_Inout_ C &&h) noexcept; \
78 C& operator=(_Inout_ C &&h) noexcept;
79
80#ifndef WINSTD_STACK_BUFFER_BYTES
94#define WINSTD_STACK_BUFFER_BYTES 1024
95#endif
96
98
101
105#ifdef UNICODE
106#define PRINTF_LPTSTR "ls"
107#else
108#define PRINTF_LPTSTR "s"
109#endif
110
114#ifdef OLE2ANSI
115#define PRINTF_LPOLESTR "hs"
116#else
117#define PRINTF_LPOLESTR "ls"
118#endif
119
123#ifdef _UNICODE
124#define _tcin (std::wcin )
125#else
126#define _tcin (std::cin )
127#endif
128
132#ifdef _UNICODE
133#define _tcout (std::wcout)
134#else
135#define _tcout (std::cout)
136#endif
137
141#ifdef _UNICODE
142#define _tcerr (std::wcerr)
143#else
144#define _tcerr (std::cerr)
145#endif
146
150#ifdef _UNICODE
151#define _tclog (std::wclog)
152#else
153#define _tclog (std::clog)
154#endif
155
157
160
164#define WINSTD_HANDLE_IMPL(C, T, INVAL) \
165public: \
166 C ( ) noexcept {} \
167 C (_In_opt_ T h) noexcept : handle<T, INVAL>( h ) {} \
168 C (_Inout_ C &&h) noexcept : handle<T, INVAL>(std::move(h)) {} \
169 C& operator=(_In_opt_ T h) noexcept { handle<T, INVAL>::operator=( h ); return *this; } \
170 C& operator=(_Inout_ C &&h) noexcept { handle<T, INVAL>::operator=(std::move(h)); return *this; } \
171WINSTD_NONCOPYABLE(C)
172
176#define WINSTD_DPLHANDLE_IMPL(C, T, INVAL) \
177public: \
178 C ( ) noexcept {} \
179 C (_In_opt_ T h) noexcept : dplhandle<T, INVAL>( h ) {} \
180 C (_In_ const C &h) noexcept : dplhandle<T, INVAL>(duplicate_internal(h.m_h)) {} \
181 C (_Inout_ C &&h) noexcept : dplhandle<T, INVAL>(std::move (h )) {} \
182 C& operator=(_In_opt_ T h) noexcept { dplhandle<T, INVAL>::operator=( h ); return *this; } \
183 C& operator=(_In_ const C &h) noexcept { dplhandle<T, INVAL>::operator=( h ); return *this; } \
184 C& operator=(_Inout_ C &&h) noexcept { dplhandle<T, INVAL>::operator=(std::move(h)); return *this; } \
185private:
186
188
189#ifndef _FormatMessage_format_string_
190#define _FormatMessage_format_string_ _In_z_
191#endif
192
194#ifndef _LPCBYTE_DEFINED
195#define _LPCBYTE_DEFINED
196typedef const BYTE *LPCBYTE;
197#endif
199
200#pragma warning(push)
201// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
202// Threfore turn off compiler warning instead. ;)
203#pragma warning(disable: 4995)
204#pragma warning(disable: 4996)
205#pragma warning(disable: 4505) // Don't warn on unused code
206
207#ifdef _WIN64
208inline ULONGLONG ULongLongMult(ULONGLONG a, ULONGLONG b)
209{
210 ULONGLONG result;
211 if (SUCCEEDED(ULongLongMult(a, b, &result)))
212 return result;
213 throw std::invalid_argument("multiply overflow");
214}
215#else
216inline SIZE_T SIZETMult(SIZE_T a, SIZE_T b)
217{
218 SIZE_T result;
219 if (SUCCEEDED(SIZETMult(a, b, &result)))
220 return result;
221 throw std::invalid_argument("multiply overflow");
222}
223#endif
224
225#ifdef _WIN64
226inline ULONGLONG ULongLongAdd(ULONGLONG a, ULONGLONG b)
227{
228 ULONGLONG result;
229 if (SUCCEEDED(ULongLongAdd(a, b, &result)))
230 return result;
231 throw std::invalid_argument("add overflow");
232}
233#else
234inline SIZE_T SIZETAdd(SIZE_T a, SIZE_T b)
235{
236 SIZE_T result;
237 if (SUCCEEDED(SIZETAdd(a, b, &result)))
238 return result;
239 throw std::invalid_argument("add overflow");
240}
241#endif
242
245
256static 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
257{
258 return _vsnwprintf(str, capacity, format, arg);
259}
260
270template<class _Elem, class _Traits, class _Ax>
271static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
272{
273 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
274
275 // Try with stack buffer first.
276 int count = vsnprintf(buf, _countof(buf), format, arg);
277 if (0 <= count && count < _countof(buf)) {
278 // Copy from stack.
279 str.append(buf, count);
280 return count;
281 }
282 if (count < 0) {
283 switch (errno) {
284 case 0:
285 count = vsnprintf(NULL, 0, format, arg);
286 assert(count >= 0);
287 break;
288 case EINVAL: throw std::invalid_argument("invalid vsnprintf arguments");
289 case EILSEQ: throw std::runtime_error("encoding error");
290 default: throw std::runtime_error("failed to format string");
291 }
292 }
293 size_t offset = str.size();
294 str.resize(offset + count);
295 if (vsnprintf(&str[offset], count + 1, format, arg) != count)
296 throw std::runtime_error("failed to format string");
297 return count;
298}
299
308template<class _Elem, class _Traits, class _Ax>
309static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
310{
311 va_list arg;
312 va_start(arg, format);
313 const int res = vsprintf(str, format, arg);
314 va_end(arg);
315 return res;
316}
317
323template<class _Traits, class _Ax>
324static _Success_(return != 0) int WideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_ int cchWideChar, _Out_ std::basic_string<char, _Traits, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
325{
326 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
327
328 // Try to convert to stack buffer first.
329 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
330 if (cch) {
331 // Copy from stack. Be careful not to include zero terminator.
332 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
333 }
334 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
335 // Query the required output size. Allocate buffer. Then convert again.
336 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
337 sMultiByteStr.resize(cch);
338 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
339 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
340 }
341
342 return cch;
343}
344
350template<class _Ax>
351static _Success_(return != 0) int WideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_ int cchWideChar, _Out_ std::vector<char, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
352{
353 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
354
355 // Try to convert to stack buffer first.
356 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
357 if (cch) {
358 // Copy from stack.
359 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
360 }
361 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
362 // Query the required output size. Allocate buffer. Then convert again.
363 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
364 sMultiByteStr.resize(cch);
365 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
366 }
367
368 return cch;
369}
370
376template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
377static _Success_(return != 0) int WideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_ std::basic_string<wchar_t, _Traits1, _Ax1> sWideCharStr, _Out_ std::basic_string<char, _Traits2, _Ax2> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
378{
379 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
380
381 // Try to convert to stack buffer first.
382 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
383 if (cch) {
384 // Copy from stack.
385 sMultiByteStr.assign(szStackBuffer, cch);
386 }
387 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
388 // Query the required output size. Allocate buffer. Then convert again.
389 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
390 sMultiByteStr.resize(cch);
391 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
392 }
393
394 return cch;
395}
396
404template<class _Traits, class _Ax>
405static _Success_(return != 0) int SecureWideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_ int cchWideChar, _Out_ std::basic_string<char, _Traits, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
406{
407 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
408
409 // Try to convert to stack buffer first.
410 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
411 if (cch) {
412 // Copy from stack. Be careful not to include zero terminator.
413 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
414 }
415 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
416 // Query the required output size. Allocate buffer. Then convert again.
417 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
418 sMultiByteStr.resize(cch);
419 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
420 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
421 }
422
423 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
424
425 return cch;
426}
427
435template<class _Ax>
436static _Success_(return != 0) int SecureWideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cchWideChar) LPCWSTR lpWideCharStr, _In_ int cchWideChar, _Out_ std::vector<char, _Ax> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
437{
438 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
439
440 // Try to convert to stack buffer first.
441 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
442 if (cch) {
443 // Copy from stack.
444 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
445 }
446 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
447 // Query the required output size. Allocate buffer. Then convert again.
448 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
449 sMultiByteStr.resize(cch);
450 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
451 }
452
453 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
454
455 return cch;
456}
457
465template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
466static _Success_(return != 0) int SecureWideCharToMultiByte(_In_ UINT CodePage, _In_ DWORD dwFlags, _Out_ std::basic_string<wchar_t, _Traits1, _Ax1> sWideCharStr, _Out_ std::basic_string<char, _Traits2, _Ax2> &sMultiByteStr, _In_opt_z_ LPCSTR lpDefaultChar, _Out_opt_ LPBOOL lpUsedDefaultChar) noexcept
467{
468 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
469
470 // Try to convert to stack buffer first.
471 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
472 if (cch) {
473 // Copy from stack.
474 sMultiByteStr.assign(szStackBuffer, cch);
475 }
476 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
477 // Query the required output size. Allocate buffer. Then convert again.
478 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
479 sMultiByteStr.resize(cch);
480 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
481 }
482
483 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
484
485 return cch;
486}
487
493template<class _Traits, class _Ax>
494static _Success_(return != 0) int MultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_ int cbMultiByte, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sWideCharStr) noexcept
495{
496 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
497
498 // Try to convert to stack buffer first.
499 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
500 if (cch) {
501 // Copy from stack.
502 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
503 }
504 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
505 // Query the required output size. Allocate buffer. Then convert again.
506 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
507 sWideCharStr.resize(cch);
508 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
509 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
510 }
511
512 return cch;
513}
514
520template<class _Ax>
521static _Success_(return != 0) int MultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_ int cbMultiByte, _Out_ std::vector<wchar_t, _Ax> &sWideCharStr) noexcept
522{
523 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
524
525 // Try to convert to stack buffer first.
526 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
527 if (cch) {
528 // Copy from stack.
529 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
530 }
531 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
532 // Query the required output size. Allocate buffer. Then convert again.
533 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
534 sWideCharStr.resize(cch);
535 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
536 }
537
538 return cch;
539}
540
546template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
547static _Success_(return != 0) int MultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_ const std::basic_string<char, _Traits1, _Ax1> &sMultiByteStr, _Out_ std::basic_string<wchar_t, _Traits2, _Ax2> &sWideCharStr) noexcept
548{
549 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
550
551 // Try to convert to stack buffer first.
552 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
553 if (cch) {
554 // Copy from stack.
555 sWideCharStr.assign(szStackBuffer, cch);
556 }
557 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
558 // Query the required output size. Allocate buffer. Then convert again.
559 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
560 sWideCharStr.resize(cch);
561 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
562 }
563
564 return cch;
565}
566
574template<class _Traits, class _Ax>
575static _Success_(return != 0) int SecureMultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_ int cbMultiByte, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sWideCharStr) noexcept
576{
577 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
578
579 // Try to convert to stack buffer first.
580 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
581 if (cch) {
582 // Copy from stack.
583 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
584 }
585 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
586 // Query the required output size. Allocate buffer. Then convert again.
587 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
588 sWideCharStr.resize(cch);
589 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
590 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
591 }
592
593 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
594
595 return cch;
596}
597
605template<class _Ax>
606static _Success_(return != 0) int SecureMultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_z_count_(cbMultiByte) LPCSTR lpMultiByteStr, _In_ int cbMultiByte, _Out_ std::vector<wchar_t, _Ax> &sWideCharStr) noexcept
607{
608 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
609
610 // Try to convert to stack buffer first.
611 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
612 if (cch) {
613 // Copy from stack.
614 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
615 }
616 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
617 // Query the required output size. Allocate buffer. Then convert again.
618 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
619 sWideCharStr.resize(cch);
620 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
621 }
622
623 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
624
625 return cch;
626}
627
635template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
636static _Success_(return != 0) int SecureMultiByteToWideChar(_In_ UINT CodePage, _In_ DWORD dwFlags, _In_ const std::basic_string<char, _Traits1, _Ax1> &sMultiByteStr, _Out_ std::basic_string<wchar_t, _Traits2, _Ax2> &sWideCharStr) noexcept
637{
638 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
639
640 // Try to convert to stack buffer first.
641 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
642 if (cch) {
643 // Copy from stack.
644 sWideCharStr.assign(szStackBuffer, cch);
645 }
646 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
647 // Query the required output size. Allocate buffer. Then convert again.
648 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
649 sWideCharStr.resize(cch);
650 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
651 }
652
653 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
654
655 return cch;
656}
657
663template<class _Traits, class _Ax>
664static DWORD FormatMessageA(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
665{
666 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
667 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
668 if (dwResult)
669 str.assign(lpBuffer.get(), dwResult);
670 return dwResult;
671}
672
678template<class _Traits, class _Ax>
679static DWORD FormatMessageW(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
680{
681 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
682 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
683 if (dwResult)
684 str.assign(lpBuffer.get(), dwResult);
685 return dwResult;
686}
687
689
690#pragma warning(pop)
691
692namespace winstd
693{
696
700#ifdef _UNICODE
701 typedef std::wstring tstring;
702#else
703 typedef std::string tstring;
704#endif
705
709 template <class _Ty>
711 {
713
718
722 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
723
729 void operator()(_Ty *_Ptr) const
730 {
731 LocalFree(_Ptr);
732 }
733 };
734
738 template <class _Ty>
739 struct LocalFree_delete<_Ty[]>
740 {
742
746 LocalFree_delete() noexcept {}
747
751 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
752 {
753 LocalFree(_Ptr);
754 }
755
761 template<class _Other>
762 void operator()(_Other *) const
763 {
764 LocalFree(_Ptr);
765 }
766 };
767
772 {
777
783 void operator()(HGLOBAL _Ptr) const
784 {
785 GlobalFree(_Ptr);
786 }
787 };
788
792 template <class T>
794 {
797
798 public:
804 globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
805 {
806 m_data = reinterpret_cast<T*>(GlobalLock(hMem));
807 if (!m_data)
808 throw win_runtime_error("GlobalLock failed");
809 }
810
817 {
818 GlobalUnlock(m_h);
819 }
820
824 T* data() const noexcept
825 {
826 return m_data;
827 }
828
829 protected:
830 HGLOBAL m_h;
832 };
833
837 template<class _Ty, class _Dx>
839 {
840 public:
846 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
847 m_own(owner),
848 m_ptr(owner.release())
849 {}
850
857 m_own(other.m_own),
858 m_ptr(other.m_ptr)
859 {
860 other.m_ptr = nullptr;
861 }
862
867 {
868 if (m_ptr != nullptr)
869 m_own.reset(m_ptr);
870 }
871
877 operator typename _Ty**()
878 {
879 return &m_ptr;
880 }
881
887 operator typename _Ty*&()
888 {
889 return m_ptr;
890 }
891
892 protected:
893 std::unique_ptr<_Ty, _Dx> &m_own;
894 _Ty *m_ptr;
895 };
896
904 template<class _Ty, class _Dx>
905 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
906 {
908 }
909
914 template<class _Ty, class _Dx>
916 {
917 public:
923 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
924 m_own(owner),
925 m_ptr(owner.release())
926 {}
927
934 m_own(other.m_own),
935 m_ptr(other.m_ptr)
936 {
937 other.m_ptr = nullptr;
938 }
939
944 {
945 if (m_ptr != nullptr)
946 m_own.reset(m_ptr);
947 }
948
954 operator typename _Ty**() noexcept
955 {
956 return &m_ptr;
957 }
958
964 operator typename _Ty*&()
965 {
966 return m_ptr;
967 }
968
969 protected:
970 std::unique_ptr<_Ty[], _Dx> &m_own;
972 };
973
982 template<class _Ty, class _Dx>
983 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx>& owner) noexcept
984 {
986 }
987
989
992
998 template <class T, const T INVAL>
999 class handle
1000 {
1001 public:
1006
1010 static const T invalid;
1011
1017
1025
1032 {
1033 // Transfer handle.
1034 m_h = h.m_h;
1035 h.m_h = invalid;
1036 }
1037
1038 private:
1039 // This class is noncopyable.
1040 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1041 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1042
1043 public:
1050 {
1051 attach(h);
1052 return *this;
1053 }
1054
1060 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1062 {
1063 if (this != std::addressof(h)) {
1064 // Transfer handle.
1065 if (m_h != invalid)
1066 free_internal();
1067 m_h = h.m_h;
1068 h.m_h = invalid;
1069 }
1070 return *this;
1071 }
1072
1078 operator handle_type() const
1079 {
1080 return m_h;
1081 }
1082
1089 {
1090 assert(m_h != invalid);
1091 return *m_h;
1092 }
1093
1099 {
1100 assert(m_h == invalid);
1101 return &m_h;
1102 }
1103
1110 {
1111 assert(m_h != invalid);
1112 return m_h;
1113 }
1114
1125 bool operator!() const
1126 {
1127 return m_h == invalid;
1128 }
1129
1139 {
1140 return m_h < h;
1141 }
1142
1152 {
1153 return !operator>(h);
1154 }
1155
1165 {
1166 return !operator<(h);
1167 }
1168
1178 {
1179 return h < m_h;
1180 }
1181
1191 {
1192 return !operator==(h);
1193 }
1194
1204 {
1205 return m_h == h;
1206 }
1207
1216 {
1217 if (m_h != invalid)
1218 free_internal();
1219 m_h = h;
1220 }
1221
1228 {
1229 handle_type h = m_h;
1230 m_h = invalid;
1231 return h;
1232 }
1233
1237 void free()
1238 {
1239 if (m_h != invalid) {
1240 free_internal();
1241 m_h = invalid;
1242 }
1243 }
1244
1245 protected:
1249 virtual void free_internal() noexcept = 0;
1250
1251 protected:
1253 };
1254
1257
1263 {
1264 public:
1270
1278
1285 {}
1286
1292 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
1293 {}
1294
1305
1312 {
1313 if (this != std::addressof(h)) {
1314 if (h.m_h != invalid) {
1315 handle_type h_new = duplicate_internal(h.m_h);
1316
1317 if (m_h != invalid)
1318 free_internal();
1319
1320 m_h = h_new;
1321 } else {
1322 if (m_h != invalid)
1323 free_internal();
1324
1325 m_h = invalid;
1326 }
1327 }
1328 return *this;
1329 }
1330
1336 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1342
1349 {
1350 return m_h != invalid ? duplicate_internal(m_h) : invalid;
1351 }
1352
1359 {
1360 if (m_h != invalid)
1361 free_internal();
1362
1363 m_h = h != invalid ? duplicate_internal(h) : invalid;
1364 }
1365
1366 protected:
1376 };
1377
1379
1382
1393 {
1394 std::string sResult;
1396 if (hFoundRes) {
1398 if (dwSize) {
1400 if (hLoadedRes) {
1401 LPCWSTR szMessage = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1402 if (szMessage) {
1404 return sResult;
1405 } else
1407 }
1408 }
1409 }
1410 sprintf(sResult, "msg %u", nId);
1411 return sResult;
1412 }
1413
1424 {
1425 std::string sResult;
1427 if (hFoundRes) {
1429 if (dwSize) {
1431 if (hLoadedRes) {
1432 LPCWSTR szFormat = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1433 if (szFormat) {
1434 dwSize /= sizeof(*szFormat);
1435 assert(wcsnlen(szFormat, dwSize) < dwSize); // Resource strings must be zero-terminated to make strings directly usable with sprintf.
1436 va_list arg;
1438 std::wstring sMessage;
1440 va_end(arg);
1442 return sResult;
1443 } else
1445 }
1446 }
1447 }
1448 sprintf(sResult, "msg %u", nId);
1449 return sResult;
1450 }
1451
1455 template <typename _Tn>
1456 class num_runtime_error : public std::runtime_error
1457 {
1458 public:
1459 typedef _Tn error_type;
1460
1461 public:
1469 m_num(num),
1470 runtime_error(msg)
1471 {}
1472
1480 m_num(num),
1481 runtime_error(msg)
1482 {}
1483
1488 {
1489 return m_num;
1490 }
1491
1492 protected:
1494 };
1495
1500 {
1501 public:
1509
1516 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
1517 {}
1518
1526 {}
1527
1533
1539 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
1540 {}
1541
1548 {}
1549
1550 protected:
1558 {
1560 std::wstring wstr;
1562 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1563 wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
1564 } else
1565 sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
1566 std::string str;
1569 return str;
1570 }
1571 };
1572
1574
1577
1581 template<class _Elem, class _Traits, class _Ax>
1582 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1583 {
1584 public:
1587
1594 {
1595 va_list arg;
1597 vsprintf(*this, format, arg);
1598 va_end(arg);
1599 }
1600
1602
1605
1613 {
1614 _Myt format;
1615 ATLENSURE(format.LoadString(hInstance, nFormatID));
1616
1617 va_list arg;
1619 vsprintf(*this, format, arg);
1620 va_end(arg);
1621 }
1622
1640
1642 };
1643
1648
1653
1657#ifdef _UNICODE
1659#else
1661#endif
1662
1666 template<class _Elem, class _Traits, class _Ax>
1667 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1668 {
1669 public:
1672
1685
1687
1690
1707
1725
1727
1737
1747
1757
1767 };
1768
1773
1778
1782#ifdef _UNICODE
1783 typedef wstring_msg tstring_msg;
1784#else
1786#endif
1787
1791 template<class _Elem, class _Traits, class _Ax>
1792 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1793 {
1794 public:
1797
1805 {
1807 guid.Data1,
1808 guid.Data2,
1809 guid.Data3,
1810 guid.Data4[0], guid.Data4[1],
1811 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1812 }
1813
1815 };
1816
1820 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1821 {
1822 public:
1825
1832 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1833 {}
1834
1836 };
1837
1841 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1842 {
1843 public:
1846
1853 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}")
1854 {}
1855
1857 };
1858
1862#ifdef _UNICODE
1863 typedef wstring_guid tstring_guid;
1864#else
1866#endif
1867
1869
1872
1873 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1874 #pragma warning(push)
1875 #pragma warning(disable: 4100)
1876
1884 template<class _Ty>
1885 class sanitizing_allocator : public std::allocator<_Ty>
1886 {
1887 public:
1888 typedef std::allocator<_Ty> _Mybase;
1889
1893 template<class _Other>
1894 struct rebind
1895 {
1897 };
1898
1904
1910
1914 template<class _Other>
1917
1921 void deallocate(_In_ _Ty* const _Ptr, _In_ const std::size_t _Count)
1922 {
1923 // Sanitize then free.
1924 SecureZeroMemory(_Ptr, sizeof(_Ty) * _Count);
1925 _Mybase::deallocate(_Ptr, _Count);
1926 }
1927 };
1928
1929 #pragma warning(pop)
1930
1938 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1939
1947 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1948
1952#ifdef _UNICODE
1954#else
1956#endif
1957
1961 template<size_t N>
1963 {
1964 public:
1969 {
1970 ZeroMemory(m_data, N);
1971 }
1972
1977 {
1978 SecureZeroMemory(m_data, N);
1979 }
1980
1981 public:
1982 unsigned char m_data[N];
1983 };
1984
1986}
Base template class to support converting GUID to string.
Definition Common.h:1793
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:1804
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1668
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:1743
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1753
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:1715
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:1733
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1678
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1697
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1763
Base template class to support string formatting using printf() style templates.
Definition Common.h:1583
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1593
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:1630
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1612
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1263
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:1300
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1348
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1337
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1358
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1276
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1311
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1268
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:794
HGLOBAL m_h
memory handle
Definition Common.h:830
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:816
T * m_data
memory pointer
Definition Common.h:831
T * data() const noexcept
Return data pointer.
Definition Common.h:824
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:804
Base abstract template class to support generic object handle keeping.
Definition Common.h:1000
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:1088
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:1015
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1164
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:1109
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:1098
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:1005
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1023
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1138
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1049
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:1125
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:1061
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1190
void free()
Destroys the object.
Definition Common.h:1237
handle_type m_h
Object handle.
Definition Common.h:1252
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1215
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1203
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:1031
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1227
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1177
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1151
Numerical runtime error.
Definition Common.h:1457
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1479
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1468
error_type number() const
Returns the error number.
Definition Common.h:1487
_Tn error_type
Error number type.
Definition Common.h:1459
error_type m_num
Numeric error code.
Definition Common.h:1493
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition Common.h:970
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition Common.h:933
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:943
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition Common.h:923
_Ty * m_ptr
Pointer.
Definition Common.h:971
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:839
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition Common.h:893
_Ty * m_ptr
Pointer.
Definition Common.h:894
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition Common.h:856
~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:866
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition Common.h:846
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1886
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1908
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1915
void deallocate(_Ty *const _Ptr, const std::size_t _Count)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1921
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1888
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1902
Sanitizing BLOB.
Definition Common.h:1963
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1968
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1976
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1821
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1831
Windows runtime error.
Definition Common.h:1500
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1547
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1525
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1507
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1531
static std::string message(error_type num, DWORD dwLanguageId=0)
Returns a user-readable Windows error message. As std::exception messages may only be char*,...
Definition Common.h:1557
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1539
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1516
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1842
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1852
std::string load_msg_from_res(HMODULE hModule, UINT nId, WORD wLanguage)
Loads exception message string from resources and converts it to UTF-8.
Definition Common.h:1392
std::string fmt_msg_from_res(HMODULE hModule, UINT nId, WORD wLanguage,...)
Loads exception message sprintf template from resources, formats it and converts it to UTF-8.
Definition Common.h:1423
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:67
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition Common.h:703
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:905
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition Common.h:75
std::basic_string< wchar_t, std::char_traits< wchar_t >, sanitizing_allocator< wchar_t > > sanitizing_wstring
A sanitizing variant of std::wstring.
Definition Common.h:1947
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1955
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1938
static int SecureWideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, std::basic_string< char, _Traits, _Ax > &sMultiByteStr, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) noexcept
Maps a UTF-16 (wide character) string to a std::string. The new character string is not necessarily f...
Definition Common.h:405
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:1652
static int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, std::basic_string< wchar_t, _Traits, _Ax > &sWideCharStr) noexcept
Maps a character string to a UTF-16 (wide character) std::wstring. The character string is not necess...
Definition Common.h:494
static DWORD FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< wchar_t, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition Common.h:679
static DWORD FormatMessageA(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:664
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition Common.h:1865
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:1777
static int vsprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format, va_list arg)
Formats string using printf().
Definition Common.h:271
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:1647
static int vsnprintf(wchar_t *str, size_t capacity, const wchar_t *format, va_list arg) noexcept
Formats string using printf().
Definition Common.h:256
static int SecureMultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, std::basic_string< wchar_t, _Traits, _Ax > &sWideCharStr) noexcept
Maps a character string to a UTF-16 (wide character) std::wstring. The character string is not necess...
Definition Common.h:575
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1660
static int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, std::basic_string< char, _Traits, _Ax > &sMultiByteStr, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) noexcept
Maps a UTF-16 (wide character) string to a std::string. The new character string is not necessarily f...
Definition Common.h:324
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:309
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:1772
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1785
static const T invalid
Invalid handle value.
Definition Common.h:1010
Deleter for unique_ptr using GlobalFree.
Definition Common.h:772
GlobalFree_delete()
Default construct.
Definition Common.h:776
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:783
LocalFree_delete() noexcept
Default construct.
Definition Common.h:746
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:741
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:762
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:751
Deleter for unique_ptr using LocalFree.
Definition Common.h:711
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:712
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:722
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:729
LocalFree_delete()
Default construct.
Definition Common.h:717
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1895
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1896