WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Common.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2023 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include <Windows.h>
10#include <assert.h>
11#include <stdarg.h>
12#include <tchar.h>
13#include <iostream>
14#include <memory>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
35
38
42#ifndef __L
43#define __L(x) L ## x
44#endif
45
49#ifndef _L
50#define _L(x) __L(x)
51#endif
52
56#define WINSTD_STRING_IMPL(x) #x
57
61#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
62
66#define WINSTD_NONCOPYABLE(C) \
67private: \
68 C (_In_ const C &h) noexcept; \
69 C& operator=(_In_ const C &h) noexcept;
70
74#define WINSTD_NONMOVABLE(C) \
75private: \
76 C (_Inout_ C &&h) noexcept; \
77 C& operator=(_Inout_ C &&h) noexcept;
78
79#ifndef WINSTD_STACK_BUFFER_BYTES
93#define WINSTD_STACK_BUFFER_BYTES 1024
94#endif
95
97
100
104#ifdef UNICODE
105#define PRINTF_LPTSTR "ls"
106#else
107#define PRINTF_LPTSTR "s"
108#endif
109
113#ifdef OLE2ANSI
114#define PRINTF_LPOLESTR "hs"
115#else
116#define PRINTF_LPOLESTR "ls"
117#endif
118
122#ifdef _UNICODE
123#define _tcin (std::wcin )
124#else
125#define _tcin (std::cin )
126#endif
127
131#ifdef _UNICODE
132#define _tcout (std::wcout)
133#else
134#define _tcout (std::cout)
135#endif
136
140#ifdef _UNICODE
141#define _tcerr (std::wcerr)
142#else
143#define _tcerr (std::cerr)
144#endif
145
149#ifdef _UNICODE
150#define _tclog (std::wclog)
151#else
152#define _tclog (std::clog)
153#endif
154
156
159
163#define WINSTD_HANDLE_IMPL(C, INVAL) \
164public: \
165 C ( ) noexcept { } \
166 C (_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>( h ) { } \
167 C (_Inout_ C &&h) noexcept : handle<handle_type, INVAL>(std::move(h)) { } \
168 C& operator=(_In_opt_ handle_type h) noexcept { handle<handle_type, INVAL>::operator=( h ); return *this; } \
169 C& operator=(_Inout_ C &&h) noexcept { handle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
170WINSTD_NONCOPYABLE(C)
171
175#define WINSTD_DPLHANDLE_IMPL(C, INVAL) \
176public: \
177 C ( ) noexcept { } \
178 C (_In_opt_ handle_type h) noexcept : dplhandle<handle_type, INVAL>( h ) { } \
179 C (_In_ const C &h) noexcept : dplhandle<handle_type, INVAL>(duplicate_internal(h.m_h)) { } \
180 C (_Inout_ C &&h) noexcept : dplhandle<handle_type, INVAL>(std::move (h )) { } \
181 C& operator=(_In_opt_ handle_type h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
182 C& operator=(_In_ const C &h) noexcept { dplhandle<handle_type, INVAL>::operator=( h ); return *this; } \
183 C& operator=(_Inout_ C &&h) noexcept { dplhandle<handle_type, INVAL>::operator=(std::move(h)); return *this; } \
184private:
185
187
188#ifndef _FormatMessage_format_string_
189#define _FormatMessage_format_string_ _In_z_
190#endif
191
193#ifndef _LPCBYTE_DEFINED
194#define _LPCBYTE_DEFINED
195typedef const BYTE *LPCBYTE;
196#endif
198
199#pragma warning(push)
200// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
201// Threfore turn off compiler warning instead. ;)
202#pragma warning(disable: 4995)
203#pragma warning(disable: 4996)
204#pragma warning(disable: 4505) // Don't warn on unused code
205
208
219#if _MSC_VER <= 1600
220static int vsnprintf(_Out_z_cap_(capacity) char *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
221{
222 return _vsnprintf(str, capacity, format, arg);
223}
224#endif
225
236static int vsnprintf(_Out_z_cap_(capacity) wchar_t *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const wchar_t *format, _In_ va_list arg) noexcept
237{
238 return _vsnwprintf(str, capacity, format, arg);
239}
240
250template<class _Elem, class _Traits, class _Ax>
251static int vsprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, _In_ va_list arg)
252{
253 _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
254
255 // Try with stack buffer first.
256 int count = vsnprintf(buf, _countof(buf) - 1, format, arg);
257 if (count >= 0) {
258 // Copy from stack.
259 str.assign(buf, count);
260 } else {
261 for (size_t capacity = 2*WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem);; capacity *= 2) {
262 // Allocate on heap and retry.
263 auto buf_dyn = std::make_unique<_Elem[]>(capacity);
264 count = vsnprintf(buf_dyn.get(), capacity - 1, format, arg);
265 if (count >= 0) {
266 str.assign(buf_dyn.get(), count);
267 break;
268 }
269 }
270 }
271
272 return count;
273}
274
283template<class _Elem, class _Traits, class _Ax>
284static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
285{
286 va_list arg;
287 va_start(arg, format);
288 const int res = vsprintf(str, format, arg);
289 va_end(arg);
290 return res;
291}
292
298template<class _Traits, class _Ax>
299static _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
300{
301 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
302
303 // Try to convert to stack buffer first.
304 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
305 if (cch) {
306 // Copy from stack. Be careful not to include zero terminator.
307 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
308 }
309 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
310 // Query the required output size. Allocate buffer. Then convert again.
311 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
312 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
313 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
314 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
315 }
316
317 return cch;
318}
319
325template<class _Ax>
326static _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
327{
328 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
329
330 // Try to convert to stack buffer first.
331 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
332 if (cch) {
333 // Copy from stack.
334 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
335 }
336 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
337 // Query the required output size. Allocate buffer. Then convert again.
338 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
339 sMultiByteStr.resize(cch);
340 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
341 }
342
343 return cch;
344}
345
351template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
352static _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
353{
354 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
355
356 // Try to convert to stack buffer first.
357 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
358 if (cch) {
359 // Copy from stack.
360 sMultiByteStr.assign(szStackBuffer, cch);
361 }
362 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
363 // Query the required output size. Allocate buffer. Then convert again.
364 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
365 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
366 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
367 sMultiByteStr.assign(szBuffer.get(), cch);
368 }
369
370 return cch;
371}
372
380template<class _Traits, class _Ax>
381static _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
382{
383 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
384
385 // Try to convert to stack buffer first.
386 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
387 if (cch) {
388 // Copy from stack. Be careful not to include zero terminator.
389 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
390 }
391 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
392 // Query the required output size. Allocate buffer. Then convert again.
393 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
394 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
395 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
396 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
397 SecureZeroMemory(szBuffer.get(), sizeof(CHAR) * cch);
398 }
399
400 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
401
402 return cch;
403}
404
412template<class _Ax>
413static _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
414{
415 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
416
417 // Try to convert to stack buffer first.
418 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
419 if (cch) {
420 // Copy from stack.
421 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
422 }
423 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
424 // Query the required output size. Allocate buffer. Then convert again.
425 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
426 sMultiByteStr.resize(cch);
427 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
428 }
429
430 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
431
432 return cch;
433}
434
442template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
443static _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
444{
445 CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
446
447 // Try to convert to stack buffer first.
448 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
449 if (cch) {
450 // Copy from stack.
451 sMultiByteStr.assign(szStackBuffer, cch);
452 }
453 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
454 // Query the required output size. Allocate buffer. Then convert again.
455 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
456 std::unique_ptr<CHAR[]> szBuffer(new CHAR[cch]);
457 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
458 sMultiByteStr.assign(szBuffer.get(), cch);
459 SecureZeroMemory(szBuffer.get(), sizeof(CHAR) * cch);
460 }
461
462 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
463
464 return cch;
465}
466
472template<class _Traits, class _Ax>
473static _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
474{
475 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
476
477 // Try to convert to stack buffer first.
478 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
479 if (cch) {
480 // Copy from stack.
481 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
482 }
483 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
484 // Query the required output size. Allocate buffer. Then convert again.
485 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
486 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
487 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
488 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
489 }
490
491 return cch;
492}
493
499template<class _Ax>
500static _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
501{
502 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
503
504 // Try to convert to stack buffer first.
505 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
506 if (cch) {
507 // Copy from stack.
508 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
509 }
510 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
511 // Query the required output size. Allocate buffer. Then convert again.
512 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
513 sWideCharStr.resize(cch);
514 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
515 }
516
517 return cch;
518}
519
525template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
526static _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
527{
528 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
529
530 // Try to convert to stack buffer first.
531 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
532 if (cch) {
533 // Copy from stack.
534 sWideCharStr.assign(szStackBuffer, cch);
535 }
536 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
537 // Query the required output size. Allocate buffer. Then convert again.
538 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
539 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
540 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szBuffer.get(), cch);
541 sWideCharStr.assign(szBuffer.get(), cch);
542 }
543
544 return cch;
545}
546
554template<class _Traits, class _Ax>
555static _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
556{
557 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
558
559 // Try to convert to stack buffer first.
560 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
561 if (cch) {
562 // Copy from stack.
563 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
564 }
565 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
566 // Query the required output size. Allocate buffer. Then convert again.
567 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
568 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
569 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
570 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
571 SecureZeroMemory(szBuffer.get(), sizeof(WCHAR) * cch);
572 }
573
574 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
575
576 return cch;
577}
578
586template<class _Ax>
587static _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
588{
589 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
590
591 // Try to convert to stack buffer first.
592 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
593 if (cch) {
594 // Copy from stack.
595 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
596 }
597 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
598 // Query the required output size. Allocate buffer. Then convert again.
599 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
600 sWideCharStr.resize(cch);
601 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
602 }
603
604 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
605
606 return cch;
607}
608
616template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
617static _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
618{
619 WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
620
621 // Try to convert to stack buffer first.
622 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
623 if (cch) {
624 // Copy from stack.
625 sWideCharStr.assign(szStackBuffer, cch);
626 }
627 else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
628 // Query the required output size. Allocate buffer. Then convert again.
629 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
630 std::unique_ptr<WCHAR[]> szBuffer(new WCHAR[cch]);
631 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szBuffer.get(), cch);
632 sWideCharStr.assign(szBuffer.get(), cch);
633 SecureZeroMemory(szBuffer.get(), sizeof(WCHAR) * cch);
634 }
635
636 SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
637
638 return cch;
639}
640
646template<class _Traits, class _Ax>
647static DWORD FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
648{
649 std::unique_ptr<CHAR[], winstd::LocalFree_delete<CHAR[]> > lpBuffer;
650 DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>((LPSTR*)get_ptr(lpBuffer)), 0, Arguments);
651 if (dwResult)
652 str.assign(lpBuffer.get(), dwResult);
653 return dwResult;
654}
655
661template<class _Traits, class _Ax>
662static DWORD FormatMessage(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_opt_ va_list *Arguments)
663{
664 std::unique_ptr<WCHAR[], winstd::LocalFree_delete<WCHAR[]> > lpBuffer;
665 DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>((LPWSTR*)get_ptr(lpBuffer)), 0, Arguments);
666 if (dwResult)
667 str.assign(lpBuffer.get(), dwResult);
668 return dwResult;
669}
670
672
673#pragma warning(pop)
674
675namespace winstd
676{
679
683#ifdef _UNICODE
684 typedef std::wstring tstring;
685#else
686 typedef std::string tstring;
687#endif
688
692 template <class _Ty>
694 {
696
701
705 template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
706
712 void operator()(_Ty *_Ptr) const
713 {
714 LocalFree(_Ptr);
715 }
716 };
717
721 template <class _Ty>
722 struct LocalFree_delete<_Ty[]>
723 {
725
729 LocalFree_delete() noexcept {}
730
734 void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
735 {
736 LocalFree(_Ptr);
737 }
738
744 template<class _Other>
745 void operator()(_Other *) const
746 {
747 LocalFree(_Ptr);
748 }
749 };
750
755 {
760
766 void operator()(HGLOBAL _Ptr) const
767 {
768 GlobalFree(_Ptr);
769 }
770 };
771
775 template <class T>
777 {
780
781 public:
787 globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
788 {
789 m_data = reinterpret_cast<T*>(GlobalLock(hMem));
790 if (!m_data)
791 throw win_runtime_error("GlobalLock failed");
792 }
793
800 {
801 GlobalUnlock(m_h);
802 }
803
807 T* data() const noexcept
808 {
809 return m_data;
810 }
811
812 protected:
813 HGLOBAL m_h;
815 };
816
820 template<class _Ty, class _Dx>
822 {
823 public:
829 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) :
830 m_own(owner),
831 m_ptr(owner.release())
832 {}
833
840 m_own(other.m_own),
841 m_ptr(other.m_ptr)
842 {
843 other.m_ptr = nullptr;
844 }
845
850 {
851 if (m_ptr != nullptr)
852 m_own.reset(m_ptr);
853 }
854
860 operator typename _Ty**()
861 {
862 return &m_ptr;
863 }
864
870 operator typename _Ty*&()
871 {
872 return m_ptr;
873 }
874
875 protected:
876 std::unique_ptr<_Ty, _Dx> &m_own;
877 _Ty *m_ptr;
878 };
879
887 template<class _Ty, class _Dx>
888 ref_unique_ptr<_Ty, _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty, _Dx> &owner) noexcept
889 {
890 return ref_unique_ptr<_Ty, _Dx>(owner);
891 }
892
897 template<class _Ty, class _Dx>
898 class ref_unique_ptr<_Ty[], _Dx>
899 {
900 public:
906 ref_unique_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx> &owner) noexcept :
907 m_own(owner),
908 m_ptr(owner.release())
909 {}
910
916 ref_unique_ptr(_Inout_ ref_unique_ptr<_Ty[], _Dx> &&other) :
917 m_own(other.m_own),
918 m_ptr(other.m_ptr)
919 {
920 other.m_ptr = nullptr;
921 }
922
927 {
928 if (m_ptr != nullptr)
929 m_own.reset(m_ptr);
930 }
931
937 operator typename _Ty**() noexcept
938 {
939 return &m_ptr;
940 }
941
947 operator typename _Ty*&()
948 {
949 return m_ptr;
950 }
951
952 protected:
953 std::unique_ptr<_Ty[], _Dx> &m_own;
954 _Ty *m_ptr;
955 };
956
965 template<class _Ty, class _Dx>
966 ref_unique_ptr<_Ty[], _Dx> get_ptr(_Inout_ std::unique_ptr<_Ty[], _Dx>& owner) noexcept
967 {
968 return ref_unique_ptr<_Ty[], _Dx>(owner);
969 }
970
972
975
981 template <class T, const T INVAL>
982 class handle
983 {
984 public:
988 typedef T handle_type;
989
993 static const T invalid;
994
998 handle() noexcept : m_h(invalid)
999 {
1000 }
1001
1007 handle(_In_opt_ handle_type h) noexcept : m_h(h)
1008 {
1009 }
1010
1017 {
1018 // Transfer handle.
1019 m_h = h.m_h;
1020 h.m_h = invalid;
1021 }
1022
1023 private:
1024 // This class is noncopyable.
1025 handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1026 handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
1027
1028 public:
1035 {
1036 attach(h);
1037 return *this;
1038 }
1039
1045 #pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1047 {
1048 if (this != std::addressof(h)) {
1049 // Transfer handle.
1050 if (m_h != invalid)
1051 free_internal();
1052 m_h = h.m_h;
1053 h.m_h = invalid;
1054 }
1055 return *this;
1056 }
1057
1063 operator handle_type() const
1064 {
1065 return m_h;
1066 }
1067
1074 {
1075 assert(m_h != invalid);
1076 return *m_h;
1077 }
1078
1084 {
1085 assert(m_h == invalid);
1086 return &m_h;
1087 }
1088
1095 {
1096 assert(m_h != invalid);
1097 return m_h;
1098 }
1099
1110 bool operator!() const
1111 {
1112 return m_h == invalid;
1113 }
1114
1123 bool operator<(_In_opt_ handle_type h) const
1124 {
1125 return m_h < h;
1126 }
1127
1136 bool operator<=(_In_opt_ handle_type h) const
1137 {
1138 return !operator>(h);
1139 }
1140
1149 bool operator>=(_In_opt_ handle_type h) const
1150 {
1151 return !operator<(h);
1152 }
1153
1162 bool operator>(_In_opt_ handle_type h) const
1163 {
1164 return h < m_h;
1165 }
1166
1175 bool operator!=(_In_opt_ handle_type h) const
1176 {
1177 return !operator==(h);
1178 }
1179
1188 bool operator==(_In_opt_ handle_type h) const
1189 {
1190 return m_h == h;
1191 }
1192
1200 void attach(_In_opt_ handle_type h) noexcept
1201 {
1202 if (m_h != invalid)
1203 free_internal();
1204 m_h = h;
1205 }
1206
1213 {
1214 handle_type h = m_h;
1215 m_h = invalid;
1216 return h;
1217 }
1218
1222 void free()
1223 {
1224 if (m_h != invalid) {
1225 free_internal();
1226 m_h = invalid;
1227 }
1228 }
1229
1230 protected:
1234 virtual void free_internal() noexcept = 0;
1235
1236 protected:
1238 };
1239
1240 template <class T, const T INVAL>
1241 const T handle<T, INVAL>::invalid = INVAL;
1242
1246 template <class T, T INVAL>
1247 class dplhandle : public handle<T, INVAL>
1248 {
1249 public:
1253 dplhandle() noexcept
1254 {
1255 }
1256
1263 {
1264 }
1265
1271 dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
1272 {
1273 }
1274
1280 dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
1281 {
1282 }
1283
1290 {
1292 return *this;
1293 }
1294
1301 {
1302 if (this != std::addressof(h)) {
1303 if (h.m_h != invalid) {
1304 handle_type h_new = duplicate_internal(h.m_h);
1305
1306 if (m_h != invalid)
1307 free_internal();
1308
1309 m_h = h_new;
1310 } else {
1311 if (m_h != invalid)
1312 free_internal();
1313
1314 m_h = invalid;
1315 }
1316 }
1317 return *this;
1318 }
1319
1325 #pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
1327 {
1329 return *this;
1330 }
1331
1338 {
1339 return m_h != invalid ? duplicate_internal(m_h) : invalid;
1340 }
1341
1348 {
1349 if (m_h != invalid)
1350 free_internal();
1351
1352 m_h = h != invalid ? duplicate_internal(h) : invalid;
1353 }
1354
1355 protected:
1364 virtual handle_type duplicate_internal(_In_ handle_type h) const = 0;
1365 };
1366
1368
1371
1375 template <typename _Tn>
1376 class num_runtime_error : public std::runtime_error
1377 {
1378 public:
1379 typedef _Tn error_type;
1380
1381 public:
1388 num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
1389 m_num(num),
1390 runtime_error(msg)
1391 {
1392 }
1393
1400 num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
1401 m_num(num),
1402 runtime_error(msg)
1403 {
1404 }
1405
1410 {
1411 return m_num;
1412 }
1413
1414 protected:
1416 };
1417
1422 {
1423 public:
1429 win_runtime_error(_In_ error_type num) : num_runtime_error<DWORD>(num, message(num))
1430 {}
1431
1438 win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
1439 {}
1440
1447 win_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<DWORD>(num, std::string(msg) + ": " + message(num))
1448 {}
1449
1453 win_runtime_error() : num_runtime_error<DWORD>(GetLastError(), message(GetLastError()))
1454 {}
1455
1461 win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
1462 {}
1463
1469 win_runtime_error(_In_z_ const char *msg) : num_runtime_error<DWORD>(GetLastError(), std::string(msg) + ": " + message(GetLastError()))
1470 {}
1471
1472 protected:
1478 static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
1479 {
1480 error_type runtime_num = GetLastError();
1481 std::wstring wstr;
1482 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
1483 // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
1484 wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
1485 } else
1486 sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
1487 std::string str;
1488 WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
1489 SetLastError(runtime_num);
1490 return str;
1491 }
1492 };
1493
1495
1498
1502 template<class _Elem, class _Traits, class _Ax>
1503 class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
1504 {
1505 public:
1508
1514 basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
1515 {
1516 va_list arg;
1517 va_start(arg, format);
1518 vsprintf(*this, format, arg);
1519 va_end(arg);
1520 }
1521
1523
1526
1533 basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1534 {
1535 _Myt format;
1536 ATLENSURE(format.LoadString(hInstance, nFormatID));
1537
1538 va_list arg;
1539 va_start(arg, nFormatID);
1540 vsprintf(*this, format, arg);
1541 va_end(arg);
1542 }
1543
1551 basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1552 {
1553 _Myt format;
1554 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1555
1556 va_list arg;
1557 va_start(arg, nFormatID);
1558 vsprintf(*this, format, arg);
1559 va_end(arg);
1560 }
1561
1563 };
1564
1569
1574
1578#ifdef _UNICODE
1580#else
1582#endif
1583
1587 template<class _Elem, class _Traits, class _Ax>
1588 class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
1589 {
1590 public:
1593
1599 basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
1600 {
1601 va_list arg;
1602 va_start(arg, format);
1603 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1604 va_end(arg);
1605 }
1606
1608
1611
1618 basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
1619 {
1620 _Myt format(GetManager());
1621 ATLENSURE(format.LoadString(hInstance, nFormatID));
1622
1623 va_list arg;
1624 va_start(arg, nFormatID);
1625 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1626 va_end(arg);
1627 }
1628
1636 basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
1637 {
1638 _Myt format(GetManager());
1639 ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
1640
1641 va_list arg;
1642 va_start(arg, nFormatID);
1643 FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
1644 va_end(arg);
1645 }
1646
1648
1654 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
1655 {
1656 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
1657 }
1658
1664 basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
1665 {
1666 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
1667 }
1668
1674 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
1675 {
1676 FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
1677 }
1678
1684 basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
1685 {
1686 FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
1687 }
1688 };
1689
1694
1699
1703#ifdef _UNICODE
1704 typedef wstring_msg tstring_msg;
1705#else
1707#endif
1708
1712 template<class _Elem, class _Traits, class _Ax>
1713 class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
1714 {
1715 public:
1718
1725 basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
1726 {
1727 sprintf<_Elem, _Traits, _Ax>(*this, format,
1728 guid.Data1,
1729 guid.Data2,
1730 guid.Data3,
1731 guid.Data4[0], guid.Data4[1],
1732 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
1733 }
1734
1736 };
1737
1741 class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
1742 {
1743 public:
1746
1752 string_guid(_In_ const GUID &guid) :
1753 basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
1754 {
1755 }
1756
1758 };
1759
1763 class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
1764 {
1765 public:
1768
1774 wstring_guid(_In_ const GUID &guid) :
1775 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}")
1776 {
1777 }
1778
1780 };
1781
1785#ifdef _UNICODE
1786 typedef wstring_guid tstring_guid;
1787#else
1789#endif
1790
1792
1795
1796 // winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
1797 #pragma warning(push)
1798 #pragma warning(disable: 4100)
1799
1807 template<class _Ty>
1808 class sanitizing_allocator : public std::allocator<_Ty>
1809 {
1810 public:
1811 typedef std::allocator<_Ty> _Mybase;
1812
1816 template<class _Other>
1817 struct rebind
1818 {
1820 };
1821
1826 {
1827 }
1828
1833 {
1834 }
1835
1839 template<class _Other>
1840 sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
1841 {
1842 }
1843
1847 void deallocate(_In_ pointer _Ptr, _In_ size_type _Size)
1848 {
1849 // Sanitize then free.
1850 SecureZeroMemory(_Ptr, _Size);
1851 _Mybase::deallocate(_Ptr, _Size);
1852 }
1853 };
1854
1855 #pragma warning(pop)
1856
1864 typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
1865
1873 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
1874
1878#ifdef _UNICODE
1880#else
1882#endif
1883
1887 template<size_t N>
1889 {
1890 public:
1895 {
1896 ZeroMemory(m_data, N);
1897 }
1898
1903 {
1904 SecureZeroMemory(m_data, N);
1905 }
1906
1907 public:
1908 unsigned char m_data[N];
1909 };
1910
1912}
Base template class to support converting GUID to string.
Definition Common.h:1714
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:1725
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1589
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:1664
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1674
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:1636
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:1654
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1599
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1618
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1684
Base template class to support string formatting using printf() style templates.
Definition Common.h:1504
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1514
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:1551
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1533
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1248
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:1289
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1337
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1326
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1347
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1262
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1300
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1253
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:777
HGLOBAL m_h
memory handle
Definition Common.h:813
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:799
T * m_data
memory pointer
Definition Common.h:814
T * data() const noexcept
Return data pointer.
Definition Common.h:807
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:787
Base abstract template class to support generic object handle keeping.
Definition Common.h:983
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:1073
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:998
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1149
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:1094
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:1083
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:988
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1007
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1123
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1034
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:1110
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:1046
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1175
void free()
Destroys the object.
Definition Common.h:1222
handle_type m_h
Object handle.
Definition Common.h:1237
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1200
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1188
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:1016
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1212
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1162
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1136
Numerical runtime error.
Definition Common.h:1377
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1400
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1388
error_type number() const
Returns the error number.
Definition Common.h:1409
_Tn error_type
Error number type.
Definition Common.h:1379
error_type m_num
Numeric error code.
Definition Common.h:1415
std::unique_ptr< _Ty[], _Dx > & m_own
Original owner of the pointer.
Definition Common.h:953
ref_unique_ptr(ref_unique_ptr< _Ty[], _Dx > &&other)
Moves object.
Definition Common.h:916
virtual ~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:926
ref_unique_ptr(std::unique_ptr< _Ty[], _Dx > &owner) noexcept
Takes ownership of the pointer.
Definition Common.h:906
_Ty * m_ptr
Pointer.
Definition Common.h:954
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:822
std::unique_ptr< _Ty, _Dx > & m_own
Original owner of the pointer.
Definition Common.h:876
_Ty * m_ptr
Pointer.
Definition Common.h:877
ref_unique_ptr(ref_unique_ptr< _Ty, _Dx > &&other)
Moves object.
Definition Common.h:839
~ref_unique_ptr()
Returns ownership of the pointer.
Definition Common.h:849
ref_unique_ptr(std::unique_ptr< _Ty, _Dx > &owner)
Takes ownership of the pointer.
Definition Common.h:829
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1809
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1832
void deallocate(pointer _Ptr, size_type _Size)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1847
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1840
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1811
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1825
Sanitizing BLOB.
Definition Common.h:1889
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1894
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1902
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1742
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1752
Windows runtime error.
Definition Common.h:1422
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1469
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1447
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1429
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1453
static std::string message(error_type num, DWORD dwLanguageId=0)
Returns a user-readable Windows error message.
Definition Common.h:1478
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1461
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1438
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1764
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1774
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:66
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:93
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition Common.h:686
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:888
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition Common.h:74
std::basic_string< wchar_t, std::char_traits< wchar_t >, sanitizing_allocator< wchar_t > > sanitizing_wstring
A sanitizing variant of std::wstring.
Definition Common.h:1873
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1881
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1864
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:381
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:1573
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:473
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition Common.h:1788
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:1698
static int vsprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format, va_list arg)
Formats string using printf().
Definition Common.h:251
static DWORD FormatMessage(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< char, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition Common.h:647
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:1568
static int vsnprintf(char *str, size_t capacity, const char *format, va_list arg)
Formats string using printf().
Definition Common.h:220
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:555
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1581
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:299
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:284
basic_string_msg< char, std::char_traits< char >, std::allocator< char > > string_msg
Single-byte character implementation of a class to support string formatting using FormatMessage() st...
Definition Common.h:1693
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1706
static const T invalid
Invalid handle value.
Definition Common.h:993
Deleter for unique_ptr using GlobalFree.
Definition Common.h:755
GlobalFree_delete()
Default construct.
Definition Common.h:759
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:766
LocalFree_delete() noexcept
Default construct.
Definition Common.h:729
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:724
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:745
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:734
Deleter for unique_ptr using LocalFree.
Definition Common.h:694
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:695
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:705
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:712
LocalFree_delete()
Default construct.
Definition Common.h:700
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1818
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1819