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
247static inline int __vsnprintf(_Out_z_cap_(capacity) char* str, _In_ size_t capacity, _In_z_ _Printf_format_string_params_(2) const char* format, _In_ va_list arg)
248{
249#pragma warning(suppress: 4996)
250 return _vsnprintf(str, capacity, format, arg);
251}
252
253static inline int __vsnprintf(_Out_z_cap_(capacity) wchar_t* str, _In_ size_t capacity, _In_z_ _Printf_format_string_params_(2) const wchar_t* format, _In_ va_list arg)
254{
255#pragma warning(suppress: 4996)
256 return _vsnwprintf(str, capacity, format, arg);
257}
259
269template<class _Elem, class _Traits, class _Ax>
270static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
271{
272 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
273
274 // Try with stack buffer first.
275 int count = __vsnprintf(buf, _countof(buf), format, arg);
276 if (0 <= count && count < _countof(buf)) {
277 // Copy from stack.
278 str.append(buf, count);
279 return count;
280 }
281 if (count < 0) {
282 switch (errno) {
283 case 0:
284 count = __vsnprintf(NULL, 0, format, arg);
285 assert(count >= 0);
286 break;
287 case EINVAL: throw std::invalid_argument("invalid vsnprintf arguments");
288 case EILSEQ: throw std::runtime_error("encoding error");
289 default: throw std::runtime_error("failed to format string");
290 }
291 }
292 size_t offset = str.size();
293 str.resize(offset + count);
294 if (__vsnprintf(&str[offset], count + 1, format, arg) != count)
295 throw std::runtime_error("failed to format string");
296 return count;
297}
298
307template<class _Elem, class _Traits, class _Ax>
308static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
309{
310 va_list arg;
311 va_start(arg, format);
312 const int res = vsprintf(str, format, arg);
313 va_end(arg);
314 return res;
315}
316
322template<class _Traits, class _Ax>
323static _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
324{
325 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
326
327 // Try to convert to stack buffer first.
328 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
329 if (cch) {
330 // Copy from stack. Be careful not to include zero terminator.
331 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
332 }
333 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
334 // Query the required output size. Allocate buffer. Then convert again.
335 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
336 sMultiByteStr.resize(cch);
337 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
338 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
339 }
340
341 return cch;
342}
343
349template<class _Ax>
350static _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
351{
352 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
353
354 // Try to convert to stack buffer first.
355 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
356 if (cch) {
357 // Copy from stack.
358 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
359 }
360 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
361 // Query the required output size. Allocate buffer. Then convert again.
362 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
363 sMultiByteStr.resize(cch);
364 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
365 }
366
367 return cch;
368}
369
375template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
376static _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
377{
378 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
379
380 // Try to convert to stack buffer first.
381 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
382 if (cch) {
383 // Copy from stack.
384 sMultiByteStr.assign(szStackBuffer, cch);
385 }
386 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
387 // Query the required output size. Allocate buffer. Then convert again.
388 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
389 sMultiByteStr.resize(cch);
390 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
391 }
392
393 return cch;
394}
395
403template<class _Traits, class _Ax>
404static _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
405{
406 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
407
408 // Try to convert to stack buffer first.
409 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
410 if (cch) {
411 // Copy from stack. Be careful not to include zero terminator.
412 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
413 }
414 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
415 // Query the required output size. Allocate buffer. Then convert again.
416 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
417 sMultiByteStr.resize(cch);
418 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
419 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
420 }
421
422 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
423
424 return cch;
425}
426
434template<class _Ax>
435static _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
436{
437 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
438
439 // Try to convert to stack buffer first.
440 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
441 if (cch) {
442 // Copy from stack.
443 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
444 }
445 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
446 // Query the required output size. Allocate buffer. Then convert again.
447 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
448 sMultiByteStr.resize(cch);
449 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
450 }
451
452 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
453
454 return cch;
455}
456
464template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
465static _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
466{
467 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
468
469 // Try to convert to stack buffer first.
470 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
471 if (cch) {
472 // Copy from stack.
473 sMultiByteStr.assign(szStackBuffer, cch);
474 }
475 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
476 // Query the required output size. Allocate buffer. Then convert again.
477 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
478 sMultiByteStr.resize(cch);
479 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
480 }
481
482 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
483
484 return cch;
485}
486
492template<class _Traits, class _Ax>
493static _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
494{
495 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
496
497 // Try to convert to stack buffer first.
498 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
499 if (cch) {
500 // Copy from stack.
501 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
502 }
503 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
504 // Query the required output size. Allocate buffer. Then convert again.
505 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
506 sWideCharStr.resize(cch);
507 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
508 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
509 }
510
511 return cch;
512}
513
519template<class _Ax>
520static _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
521{
522 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
523
524 // Try to convert to stack buffer first.
525 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
526 if (cch) {
527 // Copy from stack.
528 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
529 }
530 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
531 // Query the required output size. Allocate buffer. Then convert again.
532 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
533 sWideCharStr.resize(cch);
534 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
535 }
536
537 return cch;
538}
539
545template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
546static _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
547{
548 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
549
550 // Try to convert to stack buffer first.
551 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
552 if (cch) {
553 // Copy from stack.
554 sWideCharStr.assign(szStackBuffer, cch);
555 }
556 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
557 // Query the required output size. Allocate buffer. Then convert again.
558 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
559 sWideCharStr.resize(cch);
560 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
561 }
562
563 return cch;
564}
565
573template<class _Traits, class _Ax>
574static _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
575{
576 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
577
578 // Try to convert to stack buffer first.
579 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
580 if (cch) {
581 // Copy from stack.
582 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
583 }
584 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
585 // Query the required output size. Allocate buffer. Then convert again.
586 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
587 sWideCharStr.resize(cch);
588 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
589 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
590 }
591
592 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
593
594 return cch;
595}
596
604template<class _Ax>
605static _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
606{
607 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
608
609 // Try to convert to stack buffer first.
610 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
611 if (cch) {
612 // Copy from stack.
613 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
614 }
615 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
616 // Query the required output size. Allocate buffer. Then convert again.
617 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
618 sWideCharStr.resize(cch);
619 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
620 }
621
622 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
623
624 return cch;
625}
626
634template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
635static _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
636{
637 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
638
639 // Try to convert to stack buffer first.
640 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
641 if (cch) {
642 // Copy from stack.
643 sWideCharStr.assign(szStackBuffer, cch);
644 }
645 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
646 // Query the required output size. Allocate buffer. Then convert again.
647 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
648 sWideCharStr.resize(cch);
649 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
650 }
651
652 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
653
654 return cch;
655}
656
662template<class _Traits, class _Ax>
663static 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)
664{
665 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
666 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
667 if (dwResult)
668 str.assign(lpBuffer.get(), dwResult);
669 return dwResult;
670}
671
677template<class _Traits, class _Ax>
678static 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)
679{
680 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
681 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
682 if (dwResult)
683 str.assign(lpBuffer.get(), dwResult);
684 return dwResult;
685}
686
688
689#pragma warning(pop)
690
691namespace winstd
692{
695
699#ifdef _UNICODE
700 typedef std::wstring tstring;
701#else
702 typedef std::string tstring;
703#endif
704
708 template <class _Ty>
710 {
712
717
721 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
722
728 void operator()(_Ty *_Ptr) const
729 {
730 LocalFree(_Ptr);
731 }
732 };
733
737 template <class _Ty>
738 struct LocalFree_delete<_Ty[]>
739 {
741
745 LocalFree_delete() noexcept {}
746
750 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
751 {
752 LocalFree(_Ptr);
753 }
754
760 template<class _Other>
761 void operator()(_Other *) const
762 {
763 LocalFree(_Ptr);
764 }
765 };
766
771 {
776
782 void operator()(HGLOBAL _Ptr) const
783 {
784 GlobalFree(_Ptr);
785 }
786 };
787
791 template <class T>
793 {
796
797 public:
803 globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
804 {
805 m_data = reinterpret_cast<T*>(GlobalLock(hMem));
806 if (!m_data)
807 throw win_runtime_error("GlobalLock failed");
808 }
809
816 {
817 GlobalUnlock(m_h);
818 }
819
823 T* data() const noexcept
824 {
825 return m_data;
826 }
827
828 protected:
829 HGLOBAL m_h;
831 };
832
836 template<class _Ty, class _Dx>
838 {
839 public:
845 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
846 m_own(owner),
847 m_ptr(owner.release())
848 {}
849
856 m_own(other.m_own),
857 m_ptr(other.m_ptr)
858 {
859 other.m_ptr = nullptr;
860 }
861
866 {
867 if (m_ptr != nullptr)
868 m_own.reset(m_ptr);
869 }
870
876 operator typename _Ty**()
877 {
878 return &m_ptr;
879 }
880
886 operator typename _Ty*&()
887 {
888 return m_ptr;
889 }
890
891 protected:
892 std::unique_ptr<_Ty, _Dx> &m_own;
893 _Ty *m_ptr;
894 };
895
903 template<class _Ty, class _Dx>
904 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
905 {
907 }
908
913 template<class _Ty, class _Dx>
915 {
916 public:
922 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
923 m_own(owner),
924 m_ptr(owner.release())
925 {}
926
933 m_own(other.m_own),
934 m_ptr(other.m_ptr)
935 {
936 other.m_ptr = nullptr;
937 }
938
943 {
944 if (m_ptr != nullptr)
945 m_own.reset(m_ptr);
946 }
947
953 operator typename _Ty**() noexcept
954 {
955 return &m_ptr;
956 }
957
963 operator typename _Ty*&()
964 {
965 return m_ptr;
966 }
967
968 protected:
969 std::unique_ptr<_Ty[], _Dx> &m_own;
971 };
972
981 template<class _Ty, class _Dx>
982 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx>& owner) noexcept
983 {
985 }
986
988
991
997 template <class T, const T INVAL>
998 class handle
999 {
1000 public:
1005
1009 static const T invalid;
1010
1016
1024
1031 {
1032 // Transfer handle.
1033 m_h = h.m_h;
1034 h.m_h = invalid;
1035 }
1036
1037 private:
1038 // This class is noncopyable.
1039 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1040 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1041
1042 public:
1049 {
1050 attach(h);
1051 return *this;
1052 }
1053
1059 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1061 {
1062 if (this != std::addressof(h)) {
1063 // Transfer handle.
1064 if (m_h != invalid)
1065 free_internal();
1066 m_h = h.m_h;
1067 h.m_h = invalid;
1068 }
1069 return *this;
1070 }
1071
1077 operator handle_type() const
1078 {
1079 return m_h;
1080 }
1081
1088 {
1089 assert(m_h != invalid);
1090 return *m_h;
1091 }
1092
1098 {
1099 assert(m_h == invalid);
1100 return &m_h;
1101 }
1102
1109 {
1110 assert(m_h != invalid);
1111 return m_h;
1112 }
1113
1124 bool operator!() const
1125 {
1126 return m_h == invalid;
1127 }
1128
1138 {
1139 return m_h < h;
1140 }
1141
1151 {
1152 return !operator>(h);
1153 }
1154
1164 {
1165 return !operator<(h);
1166 }
1167
1177 {
1178 return h < m_h;
1179 }
1180
1190 {
1191 return !operator==(h);
1192 }
1193
1203 {
1204 return m_h == h;
1205 }
1206
1215 {
1216 if (m_h != invalid)
1217 free_internal();
1218 m_h = h;
1219 }
1220
1227 {
1228 handle_type h = m_h;
1229 m_h = invalid;
1230 return h;
1231 }
1232
1236 void free()
1237 {
1238 if (m_h != invalid) {
1239 free_internal();
1240 m_h = invalid;
1241 }
1242 }
1243
1244 protected:
1248 virtual void free_internal() noexcept = 0;
1249
1250 protected:
1252 };
1253
1256
1262 {
1263 public:
1269
1277
1284 {}
1285
1291 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
1292 {}
1293
1304
1311 {
1312 if (this != std::addressof(h)) {
1313 if (h.m_h != invalid) {
1314 handle_type h_new = duplicate_internal(h.m_h);
1315
1316 if (m_h != invalid)
1317 free_internal();
1318
1319 m_h = h_new;
1320 } else {
1321 if (m_h != invalid)
1322 free_internal();
1323
1324 m_h = invalid;
1325 }
1326 }
1327 return *this;
1328 }
1329
1335 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1341
1348 {
1349 return m_h != invalid ? duplicate_internal(m_h) : invalid;
1350 }
1351
1358 {
1359 if (m_h != invalid)
1360 free_internal();
1361
1362 m_h = h != invalid ? duplicate_internal(h) : invalid;
1363 }
1364
1365 protected:
1375 };
1376
1378
1381
1392 {
1393 std::string sResult;
1395 if (hFoundRes) {
1397 if (dwSize) {
1399 if (hLoadedRes) {
1400 LPCWSTR szMessage = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1401 if (szMessage) {
1403 return sResult;
1404 } else
1406 }
1407 }
1408 }
1409 sprintf(sResult, "msg %u", nId);
1410 return sResult;
1411 }
1412
1423 {
1424 std::string sResult;
1426 if (hFoundRes) {
1428 if (dwSize) {
1430 if (hLoadedRes) {
1431 LPCWSTR szFormat = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1432 if (szFormat) {
1433 dwSize /= sizeof(*szFormat);
1434 assert(wcsnlen(szFormat, dwSize) < dwSize); // Resource strings must be zero-terminated to make strings directly usable with sprintf.
1435 va_list arg;
1437 std::wstring sMessage;
1439 va_end(arg);
1441 return sResult;
1442 } else
1444 }
1445 }
1446 }
1447 sprintf(sResult, "msg %u", nId);
1448 return sResult;
1449 }
1450
1454 template <typename _Tn>
1455 class num_runtime_error : public std::runtime_error
1456 {
1457 public:
1458 typedef _Tn error_type;
1459
1460 public:
1468 m_num(num),
1469 runtime_error(msg)
1470 {}
1471
1479 m_num(num),
1480 runtime_error(msg)
1481 {}
1482
1487 {
1488 return m_num;
1489 }
1490
1491 protected:
1493 };
1494
1499 {
1500 public:
1508
1515 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
1516 {}
1517
1525 {}
1526
1532
1538 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
1539 {}
1540
1547 {}
1548
1549 protected:
1557 {
1559 std::wstring wstr;
1561 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1562 wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
1563 } else
1564 sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
1565 std::string str;
1568 return str;
1569 }
1570 };
1571
1573
1576
1580 template<class _Elem, class _Traits, class _Ax>
1581 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1582 {
1583 public:
1586
1593 {
1594 va_list arg;
1596 vsprintf(*this, format, arg);
1597 va_end(arg);
1598 }
1599
1601
1604
1612 {
1613 _Myt format;
1614 ATLENSURE(format.LoadString(hInstance, nFormatID));
1615
1616 va_list arg;
1618 vsprintf(*this, format, arg);
1619 va_end(arg);
1620 }
1621
1639
1641 };
1642
1647
1652
1656#ifdef _UNICODE
1658#else
1660#endif
1661
1665 template<class _Elem, class _Traits, class _Ax>
1666 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1667 {
1668 public:
1671
1684
1686
1689
1706
1724
1726
1736
1746
1756
1766 };
1767
1772
1777
1781#ifdef _UNICODE
1782 typedef wstring_msg tstring_msg;
1783#else
1785#endif
1786
1790 template<class _Elem, class _Traits, class _Ax>
1791 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1792 {
1793 public:
1796
1804 {
1806 guid.Data1,
1807 guid.Data2,
1808 guid.Data3,
1809 guid.Data4[0], guid.Data4[1],
1810 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1811 }
1812
1814 };
1815
1819 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1820 {
1821 public:
1824
1831 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1832 {}
1833
1835 };
1836
1840 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1841 {
1842 public:
1845
1852 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}")
1853 {}
1854
1856 };
1857
1861#ifdef _UNICODE
1862 typedef wstring_guid tstring_guid;
1863#else
1865#endif
1866
1868
1871
1872 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1873 #pragma warning(push)
1874 #pragma warning(disable: 4100)
1875
1883 template<class _Ty>
1884 class sanitizing_allocator : public std::allocator<_Ty>
1885 {
1886 public:
1887 typedef std::allocator<_Ty> _Mybase;
1888
1892 template<class _Other>
1893 struct rebind
1894 {
1896 };
1897
1903
1909
1913 template<class _Other>
1916
1920 void deallocate(_In_ _Ty* const _Ptr, _In_ const std::size_t _Count)
1921 {
1922 // Sanitize then free.
1923 SecureZeroMemory(_Ptr, sizeof(_Ty) * _Count);
1924 _Mybase::deallocate(_Ptr, _Count);
1925 }
1926 };
1927
1928 #pragma warning(pop)
1929
1937 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1938
1946 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1947
1951#ifdef _UNICODE
1953#else
1955#endif
1956
1960 template<size_t N>
1962 {
1963 public:
1968 {
1969 ZeroMemory(m_data, N);
1970 }
1971
1976 {
1977 SecureZeroMemory(m_data, N);
1978 }
1979
1980 public:
1981 unsigned char m_data[N];
1982 };
1983
1985}
Base template class to support converting GUID to string.
Definition Common.h:1792
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:1803
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1667
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:1742
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1752
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:1714
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:1732
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1677
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1696
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1762
Base template class to support string formatting using printf() style templates.
Definition Common.h:1582
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1592
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:1629
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1611
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1262
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:1299
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1347
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1336
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1357
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1275
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1310
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1267
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:793
HGLOBAL m_h
memory handle
Definition Common.h:829
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:815
T * m_data
memory pointer
Definition Common.h:830
T * data() const noexcept
Return data pointer.
Definition Common.h:823
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:803
Base abstract template class to support generic object handle keeping.
Definition Common.h:999
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:1087
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:1014
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1163
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:1108
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:1097
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:1004
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1022
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1137
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1048
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:1124
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:1060
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1189
void free()
Destroys the object.
Definition Common.h:1236
handle_type m_h
Object handle.
Definition Common.h:1251
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1214
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1202
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:1030
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1226
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1176
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1150
Numerical runtime error.
Definition Common.h:1456
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1478
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1467
error_type number() const
Returns the error number.
Definition Common.h:1486
_Tn error_type
Error number type.
Definition Common.h:1458
error_type m_num
Numeric error code.
Definition Common.h:1492
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition Common.h:969
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition Common.h:932
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:942
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition Common.h:922
_Ty * m_ptr
Pointer.
Definition Common.h:970
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:838
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition Common.h:892
_Ty * m_ptr
Pointer.
Definition Common.h:893
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition Common.h:855
~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:865
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition Common.h:845
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1885
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1907
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1914
void deallocate(_Ty *const _Ptr, const std::size_t _Count)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1920
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1887
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1901
Sanitizing BLOB.
Definition Common.h:1962
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1967
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1975
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1820
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1830
Windows runtime error.
Definition Common.h:1499
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1546
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1524
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1506
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1530
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:1556
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1538
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1515
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1841
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1851
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:1391
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:1422
#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:702
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:904
#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:1946
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1954
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1937
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:404
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:1651
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:493
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:678
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:663
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition Common.h:1864
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:1776
static int vsprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format, va_list arg)
Formats string using printf().
Definition Common.h:270
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:1646
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:574
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1659
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:323
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:308
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:1771
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1784
static const T invalid
Invalid handle value.
Definition Common.h:1009
Deleter for unique_ptr using GlobalFree.
Definition Common.h:771
GlobalFree_delete()
Default construct.
Definition Common.h:775
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:782
LocalFree_delete() noexcept
Default construct.
Definition Common.h:745
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:740
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:761
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:750
Deleter for unique_ptr using LocalFree.
Definition Common.h:710
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:711
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:721
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:728
LocalFree_delete()
Default construct.
Definition Common.h:716
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1894
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1895