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
255template<class _Traits, class _Ax>
256static int vsprintf(_Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
257{
258 char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
259
260 // Try with stack buffer first.
261 int count = _vsnprintf(buf, _countof(buf), format, arg);
262 if (0 <= count && count < _countof(buf)) {
263 // Copy from stack.
264 str.append(buf, count);
265 return count;
266 }
267 if (count < 0) {
268 switch (errno) {
269 case 0:
270 count = _vsnprintf(NULL, 0, format, arg);
271 assert(count >= 0);
272 break;
273 case EINVAL: throw std::invalid_argument("invalid vsnprintf arguments");
274 case EILSEQ: throw std::runtime_error("encoding error");
275 default: throw std::runtime_error("failed to format string");
276 }
277 }
278 size_t offset = str.size();
279 str.resize(offset + count);
280 if (_vsnprintf(&str[offset], count + 1, format, arg) != count)
281 throw std::runtime_error("failed to format string");
282 return count;
283}
284
294template<class _Traits, class _Ax>
295static int vsprintf(_Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const wchar_t *format, _In_ va_list arg)
296{
297 wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
298
299 // Try with stack buffer first.
300 int count = _vsnwprintf(buf, _countof(buf), format, arg);
301 if (0 <= count && count < _countof(buf)) {
302 // Copy from stack.
303 str.append(buf, count);
304 return count;
305 }
306 if (count < 0) {
307 switch (errno) {
308 case 0:
309 count = _vsnwprintf(NULL, 0, format, arg);
310 assert(count >= 0);
311 break;
312 case EINVAL: throw std::invalid_argument("invalid vsnprintf arguments");
313 case EILSEQ: throw std::runtime_error("encoding error");
314 default: throw std::runtime_error("failed to format string");
315 }
316 }
317 size_t offset = str.size();
318 str.resize(offset + count);
319 if (_vsnwprintf(&str[offset], count + 1, format, arg) != count)
320 throw std::runtime_error("failed to format string");
321 return count;
322}
323
332template<class _Elem, class _Traits, class _Ax>
333static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
334{
335 va_list arg;
336 va_start(arg, format);
337 const int res = vsprintf(str, format, arg);
338 va_end(arg);
339 return res;
340}
341
347template<class _Traits, class _Ax>
348static _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
349{
350 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
351
352 // Try to convert to stack buffer first.
353 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
354 if (cch) {
355 // Copy from stack. Be careful not to include zero terminator.
356 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
357 }
358 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
359 // Query the required output size. Allocate buffer. Then convert again.
360 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
361 sMultiByteStr.resize(cch);
362 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
363 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
364 }
365
366 return cch;
367}
368
374template<class _Ax>
375static _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
376{
377 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
378
379 // Try to convert to stack buffer first.
380 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
381 if (cch) {
382 // Copy from stack.
383 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
384 }
385 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
386 // Query the required output size. Allocate buffer. Then convert again.
387 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
388 sMultiByteStr.resize(cch);
389 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
390 }
391
392 return cch;
393}
394
400template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
401static _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
402{
403 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
404
405 // Try to convert to stack buffer first.
406 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
407 if (cch) {
408 // Copy from stack.
409 sMultiByteStr.assign(szStackBuffer, cch);
410 }
411 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
412 // Query the required output size. Allocate buffer. Then convert again.
413 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
414 sMultiByteStr.resize(cch);
415 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
416 }
417
418 return cch;
419}
420
428template<class _Traits, class _Ax>
429static _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
430{
431 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
432
433 // Try to convert to stack buffer first.
434 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
435 if (cch) {
436 // Copy from stack. Be careful not to include zero terminator.
437 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
438 }
439 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
440 // Query the required output size. Allocate buffer. Then convert again.
441 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
442 sMultiByteStr.resize(cch);
443 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
444 sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
445 }
446
447 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
448
449 return cch;
450}
451
459template<class _Ax>
460static _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
461{
462 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
463
464 // Try to convert to stack buffer first.
465 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
466 if (cch) {
467 // Copy from stack.
468 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
469 }
470 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
471 // Query the required output size. Allocate buffer. Then convert again.
472 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
473 sMultiByteStr.resize(cch);
474 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
475 }
476
477 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
478
479 return cch;
480}
481
489template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
490static _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
491{
492 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
493
494 // Try to convert to stack buffer first.
495 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
496 if (cch) {
497 // Copy from stack.
498 sMultiByteStr.assign(szStackBuffer, cch);
499 }
500 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
501 // Query the required output size. Allocate buffer. Then convert again.
502 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
503 sMultiByteStr.resize(cch);
504 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
505 }
506
507 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
508
509 return cch;
510}
511
517template<class _Traits, class _Ax>
518static _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
519{
520 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
521
522 // Try to convert to stack buffer first.
523 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
524 if (cch) {
525 // Copy from stack.
526 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
527 }
528 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
529 // Query the required output size. Allocate buffer. Then convert again.
530 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
531 sWideCharStr.resize(cch);
532 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
533 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
534 }
535
536 return cch;
537}
538
544template<class _Ax>
545static _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
546{
547 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
548
549 // Try to convert to stack buffer first.
550 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
551 if (cch) {
552 // Copy from stack.
553 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
554 }
555 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
556 // Query the required output size. Allocate buffer. Then convert again.
557 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
558 sWideCharStr.resize(cch);
559 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
560 }
561
562 return cch;
563}
564
570template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
571static _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
572{
573 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
574
575 // Try to convert to stack buffer first.
576 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
577 if (cch) {
578 // Copy from stack.
579 sWideCharStr.assign(szStackBuffer, cch);
580 }
581 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
582 // Query the required output size. Allocate buffer. Then convert again.
583 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
584 sWideCharStr.resize(cch);
585 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
586 }
587
588 return cch;
589}
590
598template<class _Traits, class _Ax>
599static _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
600{
601 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
602
603 // Try to convert to stack buffer first.
604 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
605 if (cch) {
606 // Copy from stack.
607 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
608 }
609 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
610 // Query the required output size. Allocate buffer. Then convert again.
611 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
612 sWideCharStr.resize(cch);
613 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
614 sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
615 }
616
617 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
618
619 return cch;
620}
621
629template<class _Ax>
630static _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
631{
632 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
633
634 // Try to convert to stack buffer first.
635 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
636 if (cch) {
637 // Copy from stack.
638 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
639 }
640 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
641 // Query the required output size. Allocate buffer. Then convert again.
642 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
643 sWideCharStr.resize(cch);
644 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
645 }
646
647 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
648
649 return cch;
650}
651
659template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
660static _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
661{
662 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
663
664 // Try to convert to stack buffer first.
665 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
666 if (cch) {
667 // Copy from stack.
668 sWideCharStr.assign(szStackBuffer, cch);
669 }
670 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
671 // Query the required output size. Allocate buffer. Then convert again.
672 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
673 sWideCharStr.resize(cch);
674 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
675 }
676
677 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
678
679 return cch;
680}
681
687template<class _Traits, class _Ax>
688static 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)
689{
690 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
691 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
692 if (dwResult)
693 str.assign(lpBuffer.get(), dwResult);
694 return dwResult;
695}
696
702template<class _Traits, class _Ax>
703static 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)
704{
705 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
706 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
707 if (dwResult)
708 str.assign(lpBuffer.get(), dwResult);
709 return dwResult;
710}
711
713
714#pragma warning(pop)
715
716namespace winstd
717{
720
724#ifdef _UNICODE
725 typedef std::wstring tstring;
726#else
727 typedef std::string tstring;
728#endif
729
733 template <class _Ty>
735 {
737
742
746 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
747
753 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const
754 {
755 LocalFree(_Ptr);
756 }
757 };
758
762 template <class _Ty>
763 struct LocalFree_delete<_Ty[]>
764 {
766
770 LocalFree_delete() noexcept {}
771
775 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
776 {
777 LocalFree(_Ptr);
778 }
779
785 template<class _Other>
786 void operator()(_Other *) const
787 {
788 LocalFree(_Ptr);
789 }
790 };
791
796 {
801
807 void operator()(HGLOBAL _Ptr) const
808 {
809 GlobalFree(_Ptr);
810 }
811 };
812
816 template <class T>
818 {
821
822 public:
828 globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
829 {
830 m_data = reinterpret_cast<T*>(GlobalLock(hMem));
831 if (!m_data)
832 throw win_runtime_error("GlobalLock failed");
833 }
834
841 {
842 GlobalUnlock(m_h);
843 }
844
848 T* data() const noexcept
849 {
850 return m_data;
851 }
852
853 protected:
854 HGLOBAL m_h;
856 };
857
859
862
868 template <class T, const T INVAL>
869 class handle
870 {
871 public:
875 typedef T handle_type;
876
880 static const T invalid;
881
885 handle() noexcept : m_h(invalid)
886 {}
887
893 handle(_In_opt_ handle_type h) noexcept : m_h(h)
894 {}
895
901 handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
902 {
903 // Transfer handle.
904 m_h = h.m_h;
905 h.m_h = invalid;
906 }
907
908 private:
909 // This class is noncopyable.
910 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
911 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
912
913 public:
920 {
921 attach(h);
922 return *this;
923 }
924
930 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
932 {
933 if (this != std::addressof(h)) {
934 // Transfer handle.
935 if (m_h != invalid)
937 m_h = h.m_h;
938 h.m_h = invalid;
939 }
940 return *this;
941 }
942
948 operator handle_type() const
949 {
950 return m_h;
951 }
952
959 {
960 assert(m_h != invalid);
961 return *m_h;
962 }
963
969 {
970 assert(m_h == invalid);
971 return &m_h;
972 }
973
980 {
981 assert(m_h != invalid);
982 return m_h;
983 }
984
995 bool operator!() const
996 {
997 return m_h == invalid;
998 }
999
1008 bool operator<(_In_opt_ handle_type h) const
1009 {
1010 return m_h < h;
1011 }
1012
1021 bool operator<=(_In_opt_ handle_type h) const
1022 {
1023 return !operator>(h);
1024 }
1025
1034 bool operator>=(_In_opt_ handle_type h) const
1035 {
1036 return !operator<(h);
1037 }
1038
1047 bool operator>(_In_opt_ handle_type h) const
1048 {
1049 return h < m_h;
1050 }
1051
1060 bool operator!=(_In_opt_ handle_type h) const
1061 {
1062 return !operator==(h);
1063 }
1064
1073 bool operator==(_In_opt_ handle_type h) const
1074 {
1075 return m_h == h;
1076 }
1077
1085 void attach(_In_opt_ handle_type h) noexcept
1086 {
1087 if (m_h != invalid)
1088 free_internal();
1089 m_h = h;
1090 }
1091
1098 {
1099 handle_type h = m_h;
1100 m_h = invalid;
1101 return h;
1102 }
1103
1107 void free()
1108 {
1109 if (m_h != invalid) {
1110 free_internal();
1111 m_h = invalid;
1112 }
1113 }
1114
1115 protected:
1119 virtual void free_internal() noexcept = 0;
1120
1121 protected:
1123 };
1124
1125 template <class T, const T INVAL>
1126 const T handle<T, INVAL>::invalid = INVAL;
1127
1131 template <class T, T INVAL>
1132 class dplhandle : public handle<T, INVAL>
1133 {
1134 public:
1138 dplhandle() noexcept
1139 {}
1140
1147 {}
1148
1154 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
1155 {}
1156
1162 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
1163 {}
1164
1171 {
1173 return *this;
1174 }
1175
1182 {
1183 if (this != std::addressof(h)) {
1184 if (h.m_h != invalid) {
1185 handle_type h_new = duplicate_internal(h.m_h);
1186
1187 if (m_h != invalid)
1188 free_internal();
1189
1190 m_h = h_new;
1191 } else {
1192 if (m_h != invalid)
1193 free_internal();
1194
1195 m_h = invalid;
1196 }
1197 }
1198 return *this;
1199 }
1200
1206 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1208 {
1210 return *this;
1211 }
1212
1219 {
1220 return m_h != invalid ? duplicate_internal(m_h) : invalid;
1221 }
1222
1229 {
1230 if (m_h != invalid)
1231 free_internal();
1232
1233 m_h = h != invalid ? duplicate_internal(h) : invalid;
1234 }
1235
1236 protected:
1245 virtual handle_type duplicate_internal(_In_ handle_type h) const = 0;
1246 };
1247
1249
1252
1262 inline std::string load_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage)
1263 {
1264 std::string sResult;
1265 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1266 if (hFoundRes) {
1267 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1268 if (dwSize) {
1269 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1270 if (hLoadedRes) {
1271 LPCWSTR szMessage = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1272 if (szMessage) {
1273 WideCharToMultiByte(CP_UTF8, 0, szMessage, dwSize / sizeof(*szMessage), sResult, NULL, NULL);
1274 return sResult;
1275 } else
1276 SetLastError(ERROR_LOCK_FAILED);
1277 }
1278 }
1279 }
1280 sprintf(sResult, "msg %u", nId);
1281 return sResult;
1282 }
1283
1293 inline std::string fmt_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage, ...)
1294 {
1295 std::string sResult;
1296 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1297 if (hFoundRes) {
1298 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1299 if (dwSize) {
1300 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1301 if (hLoadedRes) {
1302 LPCWSTR szFormat = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1303 if (szFormat) {
1304 dwSize /= sizeof(*szFormat);
1305 assert(wcsnlen(szFormat, dwSize) < dwSize); // Resource strings must be zero-terminated to make strings directly usable with sprintf.
1306 va_list arg;
1307 va_start(arg, wLanguage);
1308 std::wstring sMessage;
1309 vsprintf(sMessage, szFormat, arg);
1310 va_end(arg);
1311 WideCharToMultiByte(CP_UTF8, 0, sMessage, sResult, NULL, NULL);
1312 return sResult;
1313 } else
1314 SetLastError(ERROR_LOCK_FAILED);
1315 }
1316 }
1317 }
1318 sprintf(sResult, "msg %u", nId);
1319 return sResult;
1320 }
1321
1325 template <typename _Tn>
1326 class num_runtime_error : public std::runtime_error
1327 {
1328 public:
1329 typedef _Tn error_type;
1330
1331 public:
1338 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1339 m_num(num),
1340 runtime_error(msg)
1341 {}
1342
1349 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1350 m_num(num),
1351 runtime_error(msg)
1352 {}
1353
1358 {
1359 return m_num;
1360 }
1361
1362 protected:
1364 };
1365
1370 {
1371 public:
1377 last_error_saver(_In_ DWORD error = GetLastError()) :
1378 m_error(error)
1379 {}
1380
1387 {
1388 SetLastError(m_error);
1389 }
1390
1391 protected:
1392 DWORD m_error;
1393 };
1394
1399 {
1400 public:
1406 win_runtime_error(_In_ error_type num) : num_runtime_error<DWORD>(num, message(num))
1407 {}
1408
1415 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
1416 {}
1417
1424 win_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<DWORD>(num, std::string(msg) + ": " + message(num))
1425 {}
1426
1430 win_runtime_error() : num_runtime_error<DWORD>(GetLastError(), message(GetLastError()))
1431 {}
1432
1438 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
1439 {}
1440
1446 win_runtime_error(_In_z_ const char *msg) : num_runtime_error<DWORD>(GetLastError(), std::string(msg) + ": " + message(GetLastError()))
1447 {}
1448
1449 protected:
1456 static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
1457 {
1458 last_error_saver last_error_save;
1459 std::wstring wstr;
1460 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
1461 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1462 wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
1463 } else
1464 sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
1465 std::string str;
1466 WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
1467 return str;
1468 }
1469 };
1470
1472
1475
1479 template<class _Elem, class _Traits, class _Ax>
1480 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1481 {
1482 public:
1485
1491 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1492 {
1493 va_list arg;
1494 va_start(arg, format);
1495 vsprintf(*this, format, arg);
1496 va_end(arg);
1497 }
1498
1500
1503
1510 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1511 {
1512 _Myt format;
1513 ATLENSURE(format.LoadString(hInstance, nFormatID));
1514
1515 va_list arg;
1516 va_start(arg, nFormatID);
1517 vsprintf(*this, format, arg);
1518 va_end(arg);
1519 }
1520
1528 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1529 {
1530 _Myt format;
1531 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1532
1533 va_list arg;
1534 va_start(arg, nFormatID);
1535 vsprintf(*this, format, arg);
1536 va_end(arg);
1537 }
1538
1540 };
1541
1546
1551
1555#ifdef _UNICODE
1557#else
1559#endif
1560
1564 template<class _Elem, class _Traits, class _Ax>
1565 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1566 {
1567 public:
1570
1576 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1577 {
1578 va_list arg;
1579 va_start(arg, format);
1580 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1581 va_end(arg);
1582 }
1583
1585
1588
1595 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1596 {
1597 _Myt format(GetManager());
1598 ATLENSURE(format.LoadString(hInstance, nFormatID));
1599
1600 va_list arg;
1601 va_start(arg, nFormatID);
1602 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1603 va_end(arg);
1604 }
1605
1613 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1614 {
1615 _Myt format(GetManager());
1616 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1617
1618 va_list arg;
1619 va_start(arg, nFormatID);
1620 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1621 va_end(arg);
1622 }
1623
1625
1631 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1632 {
1633 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1634 }
1635
1641 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1642 {
1643 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1644 }
1645
1651 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1652 {
1653 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1654 }
1655
1661 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1662 {
1663 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1664 }
1665 };
1666
1671
1676
1680#ifdef _UNICODE
1681 typedef wstring_msg tstring_msg;
1682#else
1684#endif
1685
1689 template<class _Elem, class _Traits, class _Ax>
1690 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1691 {
1692 public:
1695
1702 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1703 {
1704 sprintf<_Elem, _Traits, _Ax>(*this, format,
1705 guid.Data1,
1706 guid.Data2,
1707 guid.Data3,
1708 guid.Data4[0], guid.Data4[1],
1709 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1710 }
1711
1713 };
1714
1718 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1719 {
1720 public:
1723
1729 string_guid(_In_ const GUID &guid) :
1730 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1731 {}
1732
1734 };
1735
1739 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1740 {
1741 public:
1744
1750 wstring_guid(_In_ const GUID &guid) :
1751 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}")
1752 {}
1753
1755 };
1756
1760#ifdef _UNICODE
1761 typedef wstring_guid tstring_guid;
1762#else
1764#endif
1765
1767
1770
1771 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1772 #pragma warning(push)
1773 #pragma warning(disable: 4100)
1774
1782 template<class _Ty>
1783 class sanitizing_allocator : public std::allocator<_Ty>
1784 {
1785 public:
1786 typedef std::allocator<_Ty> _Mybase;
1787
1791 template<class _Other>
1792 struct rebind
1793 {
1795 };
1796
1801 {}
1802
1807 {}
1808
1812 template<class _Other>
1813 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1814 {}
1815
1819 void deallocate(_In_ _Ty* const _Ptr, _In_ const std::size_t _Count)
1820 {
1821 // Sanitize then free.
1822 SecureZeroMemory(_Ptr, sizeof(_Ty) * _Count);
1823 _Mybase::deallocate(_Ptr, _Count);
1824 }
1825 };
1826
1827 #pragma warning(pop)
1828
1836 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1837
1845 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1846
1850#ifdef _UNICODE
1852#else
1854#endif
1855
1859 template<size_t N>
1861 {
1862 public:
1867 {
1868 ZeroMemory(m_data, N);
1869 }
1870
1875 {
1876 SecureZeroMemory(m_data, N);
1877 }
1878
1879 public:
1880 unsigned char m_data[N];
1881 };
1882
1884}
Base template class to support converting GUID to string.
Definition Common.h:1691
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:1702
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1566
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:1641
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1651
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:1613
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:1631
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1576
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1595
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1661
Base template class to support string formatting using printf() style templates.
Definition Common.h:1481
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1491
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:1528
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1510
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1133
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:1170
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1218
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1207
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1228
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1146
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1181
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1138
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:818
HGLOBAL m_h
memory handle
Definition Common.h:854
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:840
T * m_data
memory pointer
Definition Common.h:855
T * data() const noexcept
Return data pointer.
Definition Common.h:848
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:828
Base abstract template class to support generic object handle keeping.
Definition Common.h:870
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:958
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:885
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1034
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:979
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:968
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:893
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1008
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:919
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:995
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:931
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1060
void free()
Destroys the object.
Definition Common.h:1107
handle_type m_h
Object handle.
Definition Common.h:1122
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1085
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1073
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:901
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1097
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:875
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1047
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1021
Saves GetLastError and restores SetLastError when going out of scope.
Definition Common.h:1370
~last_error_saver()
Sets the last-error code for the calling thread.
Definition Common.h:1386
last_error_saver(DWORD error=GetLastError())
Saves the calling thread's last-error code value.
Definition Common.h:1377
Numerical runtime error.
Definition Common.h:1327
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1349
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1338
error_type number() const
Returns the error number.
Definition Common.h:1357
error_type m_num
Numeric error code.
Definition Common.h:1363
_Tn error_type
Error number type.
Definition Common.h:1329
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1784
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1806
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1786
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1813
void deallocate(_Ty *const _Ptr, const std::size_t _Count)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1819
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1800
Sanitizing BLOB.
Definition Common.h:1861
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1866
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1874
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1719
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1729
Windows runtime error.
Definition Common.h:1399
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1446
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1424
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1406
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1430
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:1456
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1438
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1415
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1740
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1750
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:1262
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:1293
#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:727
#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:1845
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1853
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1836
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:429
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:1550
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:518
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:703
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:688
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition Common.h:1763
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:1675
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:1545
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:599
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1558
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:348
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:333
static int vsprintf(std::basic_string< char, _Traits, _Ax > &str, const char *format, va_list arg)
Formats string using printf().
Definition Common.h:256
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:1670
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1683
static const T invalid
Invalid handle value.
Definition Common.h:880
Deleter for unique_ptr using GlobalFree.
Definition Common.h:796
GlobalFree_delete()
Default construct.
Definition Common.h:800
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:807
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:765
LocalFree_delete() noexcept
Default construct.
Definition Common.h:770
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:786
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:775
Deleter for unique_ptr using LocalFree.
Definition Common.h:735
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:736
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:746
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:753
LocalFree_delete()
Default construct.
Definition Common.h:741
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1793
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1794