WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Common.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2023 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include <Windows.h>
10#include <assert.h>
11#include <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
256#if _MSC_VER <= 1600
257static int vsnprintf(_Out_z_cap_(capacity) char *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
258{
259 return _vsnprintf(str, capacity, format, arg);
260}
261#endif
262
273static int vsnprintf(_Out_z_cap_(capacity) wchar_t *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const wchar_t *format, _In_ va_list arg) noexcept
274{
275 return _vsnwprintf(str, capacity, format, arg);
276}
277
287template<class _Elem, class _Traits, class _Ax>
288static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
289{
290 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
291
292 // Try with stack buffer first.
293 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
294 if (count >= 0) {
295 // Copy from stack.
296 str.assign(buf, count);
297 } else {
298 for (size_t capacity = 2*WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem);; capacity *= 2) {
299 // Allocate on heap and retry.
300 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
301 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
302 if (count >= 0) {
303 str.assign(buf_dyn.get(), count);
304 break;
305 }
306 }
307 }
308
309 return count;
310}
311
320template<class _Elem, class _Traits, class _Ax>
321static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
322{
323 va_list arg;
324 va_start(arg, format);
325 const int res = vsprintf(str, format, arg);
326 va_end(arg);
327 return res;
328}
329
335template<class _Traits, class _Ax>
336static _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
337{
338 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
339
340 // Try to convert to stack buffer first.
341 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
342 if (cch) {
343 // Copy from stack. Be careful not to include zero terminator.
344 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
345 }
346 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
347 // Query the required output size. Allocate buffer. Then convert again.
348 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
349 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
350 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
351 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
352 }
353
354 return cch;
355}
356
362template<class _Ax>
363static _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
364{
365 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
366
367 // Try to convert to stack buffer first.
368 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
369 if (cch) {
370 // Copy from stack.
371 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
372 }
373 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
374 // Query the required output size. Allocate buffer. Then convert again.
375 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
376 sMultiByteStr.resize(cch);
377 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
378 }
379
380 return cch;
381}
382
388template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
389static _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
390{
391 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
392
393 // Try to convert to stack buffer first.
394 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
395 if (cch) {
396 // Copy from stack.
397 sMultiByteStr.assign(szStackBuffer, cch);
398 }
399 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
400 // Query the required output size. Allocate buffer. Then convert again.
401 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
402 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
403 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
404 sMultiByteStr.assign(szBuffer.get(), cch);
405 }
406
407 return cch;
408}
409
417template<class _Traits, class _Ax>
418static _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
419{
420 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
421
422 // Try to convert to stack buffer first.
423 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
424 if (cch) {
425 // Copy from stack. Be careful not to include zero terminator.
426 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
427 }
428 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
429 // Query the required output size. Allocate buffer. Then convert again.
430 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
431 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
432 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
433 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
434 SecureZeroMemory(szBuffer.get(), sizeof(CHAR) * cch);
435 }
436
437 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
438
439 return cch;
440}
441
449template<class _Ax>
450static _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
451{
452 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
453
454 // Try to convert to stack buffer first.
455 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
456 if (cch) {
457 // Copy from stack.
458 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
459 }
460 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
461 // Query the required output size. Allocate buffer. Then convert again.
462 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
463 sMultiByteStr.resize(cch);
464 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
465 }
466
467 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
468
469 return cch;
470}
471
479template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
480static _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
481{
482 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
483
484 // Try to convert to stack buffer first.
485 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
486 if (cch) {
487 // Copy from stack.
488 sMultiByteStr.assign(szStackBuffer, cch);
489 }
490 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
491 // Query the required output size. Allocate buffer. Then convert again.
492 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
493 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
494 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
495 sMultiByteStr.assign(szBuffer.get(), cch);
496 SecureZeroMemory(szBuffer.get(), sizeof(CHAR) * cch);
497 }
498
499 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
500
501 return cch;
502}
503
509template<class _Traits, class _Ax>
510static _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
511{
512 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
513
514 // Try to convert to stack buffer first.
515 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
516 if (cch) {
517 // Copy from stack.
518 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
519 }
520 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
521 // Query the required output size. Allocate buffer. Then convert again.
522 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
523 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
524 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
525 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
526 }
527
528 return cch;
529}
530
536template<class _Ax>
537static _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
538{
539 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
540
541 // Try to convert to stack buffer first.
542 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
543 if (cch) {
544 // Copy from stack.
545 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
546 }
547 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
548 // Query the required output size. Allocate buffer. Then convert again.
549 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
550 sWideCharStr.resize(cch);
551 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
552 }
553
554 return cch;
555}
556
562template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
563static _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
564{
565 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
566
567 // Try to convert to stack buffer first.
568 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
569 if (cch) {
570 // Copy from stack.
571 sWideCharStr.assign(szStackBuffer, cch);
572 }
573 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
574 // Query the required output size. Allocate buffer. Then convert again.
575 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
576 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
577 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szBuffer.get(), cch);
578 sWideCharStr.assign(szBuffer.get(), cch);
579 }
580
581 return cch;
582}
583
591template<class _Traits, class _Ax>
592static _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
593{
594 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
595
596 // Try to convert to stack buffer first.
597 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
598 if (cch) {
599 // Copy from stack.
600 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
601 }
602 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
603 // Query the required output size. Allocate buffer. Then convert again.
604 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
605 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
606 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
607 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
608 SecureZeroMemory(szBuffer.get(), sizeof(WCHAR) * cch);
609 }
610
611 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
612
613 return cch;
614}
615
623template<class _Ax>
624static _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
625{
626 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
627
628 // Try to convert to stack buffer first.
629 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
630 if (cch) {
631 // Copy from stack.
632 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
633 }
634 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
635 // Query the required output size. Allocate buffer. Then convert again.
636 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
637 sWideCharStr.resize(cch);
638 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
639 }
640
641 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
642
643 return cch;
644}
645
653template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
654static _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
655{
656 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
657
658 // Try to convert to stack buffer first.
659 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
660 if (cch) {
661 // Copy from stack.
662 sWideCharStr.assign(szStackBuffer, cch);
663 }
664 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
665 // Query the required output size. Allocate buffer. Then convert again.
666 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
667 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
668 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szBuffer.get(), cch);
669 sWideCharStr.assign(szBuffer.get(), cch);
670 SecureZeroMemory(szBuffer.get(), sizeof(WCHAR) * cch);
671 }
672
673 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
674
675 return cch;
676}
677
683template<class _Traits, class _Ax>
684static 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)
685{
686 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
687 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
688 if (dwResult)
689 str.assign(lpBuffer.get(), dwResult);
690 return dwResult;
691}
692
698template<class _Traits, class _Ax>
699static 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)
700{
701 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
702 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
703 if (dwResult)
704 str.assign(lpBuffer.get(), dwResult);
705 return dwResult;
706}
707
709
710#pragma warning(pop)
711
712namespace winstd
713{
716
720#ifdef _UNICODE
721 typedef std::wstring tstring;
722#else
723 typedef std::string tstring;
724#endif
725
729 template <class _Ty>
731 {
733
738
742 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
743
749 void operator()(_Ty *_Ptr) const
750 {
751 LocalFree(_Ptr);
752 }
753 };
754
758 template <class _Ty>
759 struct LocalFree_delete<_Ty[]>
760 {
762
766 LocalFree_delete() noexcept {}
767
771 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
772 {
773 LocalFree(_Ptr);
774 }
775
781 template<class _Other>
782 void operator()(_Other *) const
783 {
784 LocalFree(_Ptr);
785 }
786 };
787
792 {
797
803 void operator()(HGLOBAL _Ptr) const
804 {
805 GlobalFree(_Ptr);
806 }
807 };
808
812 template <class T>
814 {
817
818 public:
824 globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
825 {
826 m_data = reinterpret_cast<T*>(GlobalLock(hMem));
827 if (!m_data)
828 throw win_runtime_error("GlobalLock failed");
829 }
830
837 {
838 GlobalUnlock(m_h);
839 }
840
844 T* data() const noexcept
845 {
846 return m_data;
847 }
848
849 protected:
850 HGLOBAL m_h;
852 };
853
857 template<class _Ty, class _Dx>
859 {
860 public:
866 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
867 m_own(owner),
868 m_ptr(owner.release())
869 {}
870
877 m_own(other.m_own),
878 m_ptr(other.m_ptr)
879 {
880 other.m_ptr = nullptr;
881 }
882
887 {
888 if (m_ptr != nullptr)
889 m_own.reset(m_ptr);
890 }
891
897 operator typename _Ty**()
898 {
899 return &m_ptr;
900 }
901
907 operator typename _Ty*&()
908 {
909 return m_ptr;
910 }
911
912 protected:
913 std::unique_ptr<_Ty, _Dx> &m_own;
914 _Ty *m_ptr;
915 };
916
924 template<class _Ty, class _Dx>
925 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
926 {
927 return ref_unique_ptr<_Ty, _Dx>(owner);
928 }
929
934 template<class _Ty, class _Dx>
935 class ref_unique_ptr<_Ty[], _Dx>
936 {
937 public:
943 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
944 m_own(owner),
945 m_ptr(owner.release())
946 {}
947
953 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
954 m_own(other.m_own),
955 m_ptr(other.m_ptr)
956 {
957 other.m_ptr = nullptr;
958 }
959
964 {
965 if (m_ptr != nullptr)
966 m_own.reset(m_ptr);
967 }
968
974 operator typename _Ty**() noexcept
975 {
976 return &m_ptr;
977 }
978
984 operator typename _Ty*&()
985 {
986 return m_ptr;
987 }
988
989 protected:
990 std::unique_ptr<_Ty[], _Dx> &m_own;
991 _Ty *m_ptr;
992 };
993
1002 template<class _Ty, class _Dx>
1003 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx>& owner) noexcept
1004 {
1005 return ref_unique_ptr<_Ty[], _Dx>(owner);
1006 }
1007
1009
1012
1018 template <class T, const T INVAL>
1020 {
1021 public:
1025 typedef T handle_type;
1026
1030 static const T invalid;
1031
1035 handle() noexcept : m_h(invalid)
1036 {
1037 }
1038
1044 handle(_In_opt_ handle_type h) noexcept : m_h(h)
1045 {
1046 }
1047
1054 {
1055 // Transfer handle.
1056 m_h = h.m_h;
1057 h.m_h = invalid;
1058 }
1059
1060 private:
1061 // This class is noncopyable.
1062 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1063 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1064
1065 public:
1072 {
1073 attach(h);
1074 return *this;
1075 }
1076
1082 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1084 {
1085 if (this != std::addressof(h)) {
1086 // Transfer handle.
1087 if (m_h != invalid)
1088 free_internal();
1089 m_h = h.m_h;
1090 h.m_h = invalid;
1091 }
1092 return *this;
1093 }
1094
1100 operator handle_type() const
1101 {
1102 return m_h;
1103 }
1104
1111 {
1112 assert(m_h != invalid);
1113 return *m_h;
1114 }
1115
1121 {
1122 assert(m_h == invalid);
1123 return &m_h;
1124 }
1125
1132 {
1133 assert(m_h != invalid);
1134 return m_h;
1135 }
1136
1147 bool operator!() const
1148 {
1149 return m_h == invalid;
1150 }
1151
1160 bool operator<(_In_opt_ handle_type h) const
1161 {
1162 return m_h < h;
1163 }
1164
1173 bool operator<=(_In_opt_ handle_type h) const
1174 {
1175 return !operator>(h);
1176 }
1177
1186 bool operator>=(_In_opt_ handle_type h) const
1187 {
1188 return !operator<(h);
1189 }
1190
1199 bool operator>(_In_opt_ handle_type h) const
1200 {
1201 return h < m_h;
1202 }
1203
1212 bool operator!=(_In_opt_ handle_type h) const
1213 {
1214 return !operator==(h);
1215 }
1216
1225 bool operator==(_In_opt_ handle_type h) const
1226 {
1227 return m_h == h;
1228 }
1229
1237 void attach(_In_opt_ handle_type h) noexcept
1238 {
1239 if (m_h != invalid)
1240 free_internal();
1241 m_h = h;
1242 }
1243
1250 {
1251 handle_type h = m_h;
1252 m_h = invalid;
1253 return h;
1254 }
1255
1259 void free()
1260 {
1261 if (m_h != invalid) {
1262 free_internal();
1263 m_h = invalid;
1264 }
1265 }
1266
1267 protected:
1271 virtual void free_internal() noexcept = 0;
1272
1273 protected:
1275 };
1276
1277 template <class T, const T INVAL>
1278 const T handle<T, INVAL>::invalid = INVAL;
1279
1283 template <class T, T INVAL>
1284 class dplhandle : public handle<T, INVAL>
1285 {
1286 public:
1290 dplhandle() noexcept
1291 {
1292 }
1293
1300 {
1301 }
1302
1308 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
1309 {
1310 }
1311
1317 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
1318 {
1319 }
1320
1327 {
1329 return *this;
1330 }
1331
1338 {
1339 if (this != std::addressof(h)) {
1340 if (h.m_h != invalid) {
1341 handle_type h_new = duplicate_internal(h.m_h);
1342
1343 if (m_h != invalid)
1344 free_internal();
1345
1346 m_h = h_new;
1347 } else {
1348 if (m_h != invalid)
1349 free_internal();
1350
1351 m_h = invalid;
1352 }
1353 }
1354 return *this;
1355 }
1356
1362 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1364 {
1366 return *this;
1367 }
1368
1375 {
1376 return m_h != invalid ? duplicate_internal(m_h) : invalid;
1377 }
1378
1385 {
1386 if (m_h != invalid)
1387 free_internal();
1388
1389 m_h = h != invalid ? duplicate_internal(h) : invalid;
1390 }
1391
1392 protected:
1401 virtual handle_type duplicate_internal(_In_ handle_type h) const = 0;
1402 };
1403
1405
1408
1418 inline std::string load_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage)
1419 {
1420 std::string sResult;
1421 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1422 if (hFoundRes) {
1423 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1424 if (dwSize) {
1425 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1426 if (hLoadedRes) {
1427 LPCWSTR szMessage = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1428 if (szMessage) {
1429 WideCharToMultiByte(CP_UTF8, 0, szMessage, dwSize / sizeof(*szMessage), sResult, NULL, NULL);
1430 return sResult;
1431 } else
1432 SetLastError(ERROR_LOCK_FAILED);
1433 }
1434 }
1435 }
1436 sprintf(sResult, "msg %u", nId);
1437 return sResult;
1438 }
1439
1449 inline std::string fmt_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage, ...)
1450 {
1451 std::string sResult;
1452 HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
1453 if (hFoundRes) {
1454 DWORD dwSize = SizeofResource(hModule, hFoundRes);
1455 if (dwSize) {
1456 HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
1457 if (hLoadedRes) {
1458 LPCWSTR szFormat = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
1459 if (szFormat) {
1460 dwSize /= sizeof(*szFormat);
1461 assert(wcsnlen(szFormat, dwSize) < dwSize); // Resource strings must be zero-terminated to make strings directly usable with sprintf.
1462 va_list arg;
1463 va_start(arg, wLanguage);
1464 std::wstring sMessage;
1465 vsprintf(sMessage, szFormat, arg);
1466 va_end(arg);
1467 WideCharToMultiByte(CP_UTF8, 0, sMessage, sResult, NULL, NULL);
1468 return sResult;
1469 } else
1470 SetLastError(ERROR_LOCK_FAILED);
1471 }
1472 }
1473 }
1474 sprintf(sResult, "msg %u", nId);
1475 return sResult;
1476 }
1477
1481 template <typename _Tn>
1482 class num_runtime_error : public std::runtime_error
1483 {
1484 public:
1485 typedef _Tn error_type;
1486
1487 public:
1494 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1495 m_num(num),
1496 runtime_error(msg)
1497 {
1498 }
1499
1506 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1507 m_num(num),
1508 runtime_error(msg)
1509 {
1510 }
1511
1516 {
1517 return m_num;
1518 }
1519
1520 protected:
1522 };
1523
1528 {
1529 public:
1535 win_runtime_error(_In_ error_type num) : num_runtime_error<DWORD>(num, message(num))
1536 {}
1537
1544 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
1545 {}
1546
1553 win_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<DWORD>(num, std::string(msg) + ": " + message(num))
1554 {}
1555
1559 win_runtime_error() : num_runtime_error<DWORD>(GetLastError(), message(GetLastError()))
1560 {}
1561
1567 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
1568 {}
1569
1575 win_runtime_error(_In_z_ const char *msg) : num_runtime_error<DWORD>(GetLastError(), std::string(msg) + ": " + message(GetLastError()))
1576 {}
1577
1578 protected:
1585 static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
1586 {
1587 error_type runtime_num = GetLastError();
1588 std::wstring wstr;
1589 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
1590 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1591 wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
1592 } else
1593 sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
1594 std::string str;
1595 WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
1596 SetLastError(runtime_num);
1597 return str;
1598 }
1599 };
1600
1602
1605
1609 template<class _Elem, class _Traits, class _Ax>
1610 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1611 {
1612 public:
1615
1621 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1622 {
1623 va_list arg;
1624 va_start(arg, format);
1625 vsprintf(*this, format, arg);
1626 va_end(arg);
1627 }
1628
1630
1633
1640 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1641 {
1642 _Myt format;
1643 ATLENSURE(format.LoadString(hInstance, nFormatID));
1644
1645 va_list arg;
1646 va_start(arg, nFormatID);
1647 vsprintf(*this, format, arg);
1648 va_end(arg);
1649 }
1650
1658 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1659 {
1660 _Myt format;
1661 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1662
1663 va_list arg;
1664 va_start(arg, nFormatID);
1665 vsprintf(*this, format, arg);
1666 va_end(arg);
1667 }
1668
1670 };
1671
1676
1681
1685#ifdef _UNICODE
1687#else
1689#endif
1690
1694 template<class _Elem, class _Traits, class _Ax>
1695 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1696 {
1697 public:
1700
1706 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1707 {
1708 va_list arg;
1709 va_start(arg, format);
1710 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1711 va_end(arg);
1712 }
1713
1715
1718
1725 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1726 {
1727 _Myt format(GetManager());
1728 ATLENSURE(format.LoadString(hInstance, nFormatID));
1729
1730 va_list arg;
1731 va_start(arg, nFormatID);
1732 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1733 va_end(arg);
1734 }
1735
1743 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1744 {
1745 _Myt format(GetManager());
1746 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1747
1748 va_list arg;
1749 va_start(arg, nFormatID);
1750 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1751 va_end(arg);
1752 }
1753
1755
1761 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1762 {
1763 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1764 }
1765
1771 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1772 {
1773 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1774 }
1775
1781 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1782 {
1783 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1784 }
1785
1791 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1792 {
1793 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1794 }
1795 };
1796
1801
1806
1810#ifdef _UNICODE
1811 typedef wstring_msg tstring_msg;
1812#else
1814#endif
1815
1819 template<class _Elem, class _Traits, class _Ax>
1820 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1821 {
1822 public:
1825
1832 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1833 {
1834 sprintf<_Elem, _Traits, _Ax>(*this, format,
1835 guid.Data1,
1836 guid.Data2,
1837 guid.Data3,
1838 guid.Data4[0], guid.Data4[1],
1839 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1840 }
1841
1843 };
1844
1848 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1849 {
1850 public:
1853
1859 string_guid(_In_ const GUID &guid) :
1860 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1861 {
1862 }
1863
1865 };
1866
1870 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1871 {
1872 public:
1875
1881 wstring_guid(_In_ const GUID &guid) :
1882 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}")
1883 {
1884 }
1885
1887 };
1888
1892#ifdef _UNICODE
1893 typedef wstring_guid tstring_guid;
1894#else
1896#endif
1897
1899
1902
1903 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1904 #pragma warning(push)
1905 #pragma warning(disable: 4100)
1906
1914 template<class _Ty>
1915 class sanitizing_allocator : public std::allocator<_Ty>
1916 {
1917 public:
1918 typedef std::allocator<_Ty> _Mybase;
1919
1923 template<class _Other>
1924 struct rebind
1925 {
1927 };
1928
1933 {
1934 }
1935
1940 {
1941 }
1942
1946 template<class _Other>
1947 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1948 {
1949 }
1950
1954 void deallocate(_In_ _Ty* const _Ptr, _In_ const std::size_t _Count)
1955 {
1956 // Sanitize then free.
1957 SecureZeroMemory(_Ptr, sizeof(_Ty) * _Count);
1958 _Mybase::deallocate(_Ptr, _Count);
1959 }
1960 };
1961
1962 #pragma warning(pop)
1963
1971 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1972
1980 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1981
1985#ifdef _UNICODE
1987#else
1989#endif
1990
1994 template<size_t N>
1996 {
1997 public:
2002 {
2003 ZeroMemory(m_data, N);
2004 }
2005
2010 {
2011 SecureZeroMemory(m_data, N);
2012 }
2013
2014 public:
2015 unsigned char m_data[N];
2016 };
2017
2019}
Base template class to support converting GUID to string.
Definition Common.h:1821
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:1832
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1696
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:1771
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1781
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:1743
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:1761
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1706
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1725
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1791
Base template class to support string formatting using printf() style templates.
Definition Common.h:1611
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1621
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:1658
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1640
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1285
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:1326
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1374
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1363
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1384
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1299
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1337
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1290
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:814
HGLOBAL m_h
memory handle
Definition Common.h:850
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:836
T * m_data
memory pointer
Definition Common.h:851
T * data() const noexcept
Return data pointer.
Definition Common.h:844
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:824
Base abstract template class to support generic object handle keeping.
Definition Common.h:1020
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:1110
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:1035
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1186
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:1131
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:1120
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:1025
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1044
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1160
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1071
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:1147
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:1083
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1212
void free()
Destroys the object.
Definition Common.h:1259
handle_type m_h
Object handle.
Definition Common.h:1274
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1237
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1225
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:1053
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1249
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1199
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1173
Numerical runtime error.
Definition Common.h:1483
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1506
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1494
error_type number() const
Returns the error number.
Definition Common.h:1515
_Tn error_type
Error number type.
Definition Common.h:1485
error_type m_num
Numeric error code.
Definition Common.h:1521
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition Common.h:990
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition Common.h:953
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:963
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition Common.h:943
_Ty * m_ptr
Pointer.
Definition Common.h:991
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:859
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition Common.h:913
_Ty * m_ptr
Pointer.
Definition Common.h:914
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition Common.h:876
~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:886
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition Common.h:866
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1916
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1939
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1947
void deallocate(_Ty *const _Ptr, const std::size_t _Count)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1954
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1918
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1932
Sanitizing BLOB.
Definition Common.h:1996
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:2001
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:2009
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1849
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1859
Windows runtime error.
Definition Common.h:1528
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1575
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1553
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1535
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1559
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:1585
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1567
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1544
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1871
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1881
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:1418
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:1449
#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:723
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:925
#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:1980
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1988
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1971
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:418
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:1680
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:510
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:699
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:684
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition Common.h:1895
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:1805
static int vsprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format, va_list arg)
Formats string using printf().
Definition Common.h:288
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:1675
static int vsnprintf(char *str, size_t capacity, const char *format, va_list arg)
Formats string using printf().
Definition Common.h:257
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:592
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1688
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:336
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:321
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:1800
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1813
static const T invalid
Invalid handle value.
Definition Common.h:1030
Deleter for unique_ptr using GlobalFree.
Definition Common.h:792
GlobalFree_delete()
Default construct.
Definition Common.h:796
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:803
LocalFree_delete() noexcept
Default construct.
Definition Common.h:766
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:761
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:782
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:771
Deleter for unique_ptr using LocalFree.
Definition Common.h:731
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:732
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:742
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:749
LocalFree_delete()
Default construct.
Definition Common.h:737
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1925
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1926