# WinStd: include/WinStd/Common.h Source File

Loading...
Searching...
No Matches
Common.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2024 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include <Windows.h>
#include <assert.h>
#include <intsafe.h>
#include <stdarg.h>
#include <tchar.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#ifndef __L
#define __L(x) L ## x
#endif
#ifndef _L
#define _L(x) __L(x)
#endif
#define WINSTD_STRING_IMPL(x) #x
#define WINSTD_STRING(x) WINSTD_STRING_IMPL(x)
#define WINSTD_NONCOPYABLE(C) \
private: \
C (_In_ const C &h) noexcept; \
C& operator=(_In_ const C &h) noexcept;
#define WINSTD_NONMOVABLE(C) \
private: \
C (_Inout_ C &&h) noexcept; \
C& operator=(_Inout_ C &&h) noexcept;
#ifndef WINSTD_STACK_BUFFER_BYTES
#define WINSTD_STACK_BUFFER_BYTES 1024
#endif
#ifdef UNICODE
#define PRINTF_LPTSTR "ls"
#else
#define PRINTF_LPTSTR "s"
#endif
#ifdef OLE2ANSI
#define PRINTF_LPOLESTR "hs"
#else
#define PRINTF_LPOLESTR "ls"
#endif
#ifdef _UNICODE
#define _tcin (std::wcin )
#else
#define _tcin (std::cin )
#endif
#ifdef _UNICODE
#define _tcout (std::wcout)
#else
#define _tcout (std::cout)
#endif
#ifdef _UNICODE
#define _tcerr (std::wcerr)
#else
#define _tcerr (std::cerr)
#endif
#ifdef _UNICODE
#define _tclog (std::wclog)
#else
#define _tclog (std::clog)
#endif
#define WINSTD_HANDLE_IMPL(C, T, INVAL) \
public: \
C ( ) noexcept {} \
C (_In_opt_ T h) noexcept : handle<T, INVAL>( h ) {} \
C (_Inout_ C &&h) noexcept : handle<T, INVAL>(std::move(h)) {} \
C& operator=(_In_opt_ T h) noexcept { handle<T, INVAL>::operator=( h ); return *this; } \
C& operator=(_Inout_ C &&h) noexcept { handle<T, INVAL>::operator=(std::move(h)); return *this; } \
WINSTD_NONCOPYABLE(C)
#define WINSTD_DPLHANDLE_IMPL(C, T, INVAL) \
public: \
C ( ) noexcept {} \
C (_In_opt_ T h) noexcept : dplhandle<T, INVAL>( h ) {} \
C (_In_ const C &h) noexcept : dplhandle<T, INVAL>(duplicate_internal(h.m_h)) {} \
C (_Inout_ C &&h) noexcept : dplhandle<T, INVAL>(std::move (h )) {} \
C& operator=(_In_opt_ T h) noexcept { dplhandle<T, INVAL>::operator=( h ); return *this; } \
C& operator=(_In_ const C &h) noexcept { dplhandle<T, INVAL>::operator=( h ); return *this; } \
C& operator=(_Inout_ C &&h) noexcept { dplhandle<T, INVAL>::operator=(std::move(h)); return *this; } \
private:
#ifndef _FormatMessage_format_string_
#define _FormatMessage_format_string_ _In_z_
#endif
#ifndef _LPCBYTE_DEFINED
#define _LPCBYTE_DEFINED
typedef const BYTE *LPCBYTE;
#endif
#pragma warning(push)
// Do not use _vsnprintf_s/_vsnwprintf_s(), since it terminates string by force even when we explicitly want to write unterminated string.
// Threfore turn off compiler warning instead. ;)
#pragma warning(disable: 4995)
#pragma warning(disable: 4996)
#pragma warning(disable: 4505) // Don't warn on unused code
#ifdef _WIN64
inline ULONGLONG ULongLongMult(ULONGLONG a, ULONGLONG b)
{
ULONGLONG result;
if (SUCCEEDED(ULongLongMult(a, b, &result)))
return result;
throw std::invalid_argument("multiply overflow");
}
#else
inline SIZE_T SIZETMult(SIZE_T a, SIZE_T b)
{
SIZE_T result;
if (SUCCEEDED(SIZETMult(a, b, &result)))
return result;
throw std::invalid_argument("multiply overflow");
}
#endif
#ifdef _WIN64
inline ULONGLONG ULongLongAdd(ULONGLONG a, ULONGLONG b)
{
ULONGLONG result;
if (SUCCEEDED(ULongLongAdd(a, b, &result)))
return result;
throw std::invalid_argument("add overflow");
}
#else
inline SIZE_T SIZETAdd(SIZE_T a, SIZE_T b)
{
SIZE_T result;
if (SUCCEEDED(SIZETAdd(a, b, &result)))
return result;
throw std::invalid_argument("add overflow");
}
#endif
template<class _Traits, class _Ax>
static int vsprintf(_Inout_ std::basic_string<char, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg)
{
char buf[WINSTD_STACK_BUFFER_BYTES/sizeof(char)];
// Try with stack buffer first.
int count = _vsnprintf(buf, _countof(buf), format, arg);
if (0 <= count && count < _countof(buf)) {
// Copy from stack.
str.append(buf, count);
return count;
}
if (count < 0) {
switch (errno) {
case 0:
count = _vsnprintf(NULL, 0, format, arg);
assert(count >= 0);
break;
case EINVAL: throw std::invalid_argument("invalid vsnprintf arguments");
case EILSEQ: throw std::runtime_error("encoding error");
default: throw std::runtime_error("failed to format string");
}
}
size_t offset = str.size();
str.resize(offset + count);
if (_vsnprintf(&str[offset], count + 1, format, arg) != count)
throw std::runtime_error("failed to format string");
return count;
}
template<class _Traits, class _Ax>
static int vsprintf(_Inout_ std::basic_string<wchar_t, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const wchar_t *format, _In_ va_list arg)
{
wchar_t buf[WINSTD_STACK_BUFFER_BYTES/sizeof(wchar_t)];
// Try with stack buffer first.
int count = _vsnwprintf(buf, _countof(buf), format, arg);
if (0 <= count && count < _countof(buf)) {
// Copy from stack.
str.append(buf, count);
return count;
}
if (count < 0) {
switch (errno) {
case 0:
count = _vsnwprintf(NULL, 0, format, arg);
assert(count >= 0);
break;
case EINVAL: throw std::invalid_argument("invalid vsnprintf arguments");
case EILSEQ: throw std::runtime_error("encoding error");
default: throw std::runtime_error("failed to format string");
}
}
size_t offset = str.size();
str.resize(offset + count);
if (_vsnwprintf(&str[offset], count + 1, format, arg) != count)
throw std::runtime_error("failed to format string");
return count;
}
template<class _Elem, class _Traits, class _Ax>
static int sprintf(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &str, _In_z_ _Printf_format_string_ const _Elem *format, ...)
{
va_list arg;
va_start(arg, format);
const int res = vsprintf(str, format, arg);
va_end(arg);
return res;
}
template<class _Traits, class _Ax>
static _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
{
CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
// Try to convert to stack buffer first.
int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
if (cch) {
// Copy from stack. Be careful not to include zero terminator.
sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cch);
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
}
return cch;
}
template<class _Ax>
static _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
{
CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
// Try to convert to stack buffer first.
int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
if (cch) {
// Copy from stack.
sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cch);
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
}
return cch;
}
template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
static _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
{
CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
// Try to convert to stack buffer first.
int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
if (cch) {
// Copy from stack.
sMultiByteStr.assign(szStackBuffer, cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cch);
cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
}
return cch;
}
template<class _Traits, class _Ax>
static _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
{
CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
// Try to convert to stack buffer first.
int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
if (cch) {
// Copy from stack. Be careful not to include zero terminator.
sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cch);
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cchWideChar != -1 ? strnlen(&sMultiByteStr[0], cch) : (size_t)cch - 1);
}
SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
return cch;
}
template<class _Ax>
static _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
{
CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
// Try to convert to stack buffer first.
int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
if (cch) {
// Copy from stack.
sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cch);
cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
}
SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
return cch;
}
template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
static _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
{
CHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(CHAR)];
// Try to convert to stack buffer first.
int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
if (cch) {
// Copy from stack.
sMultiByteStr.assign(szStackBuffer, cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
sMultiByteStr.resize(cch);
cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (int)sWideCharStr.length(), &sMultiByteStr[0], cch, lpDefaultChar, lpUsedDefaultChar);
}
SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
return cch;
}
template<class _Traits, class _Ax>
static _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
{
WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
// Try to convert to stack buffer first.
int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
if (cch) {
// Copy from stack.
sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
sWideCharStr.resize(cch);
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
}
return cch;
}
template<class _Ax>
static _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
{
WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
// Try to convert to stack buffer first.
int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
if (cch) {
// Copy from stack.
sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
sWideCharStr.resize(cch);
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
}
return cch;
}
template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
static _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
{
WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
// Try to convert to stack buffer first.
int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
if (cch) {
// Copy from stack.
sWideCharStr.assign(szStackBuffer, cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
sWideCharStr.resize(cch);
cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
}
return cch;
}
template<class _Traits, class _Ax>
static _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
{
WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
// Try to convert to stack buffer first.
int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
if (cch) {
// Copy from stack.
sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
sWideCharStr.resize(cch);
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, &sWideCharStr[0], cch);
sWideCharStr.resize(cbMultiByte != -1 ? wcsnlen(&sWideCharStr[0], cch) : (size_t)cch - 1);
}
SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
return cch;
}
template<class _Ax>
static _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
{
WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
// Try to convert to stack buffer first.
int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
if (cch) {
// Copy from stack.
sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
sWideCharStr.resize(cch);
cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
}
SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
return cch;
}
template<class _Traits1, class _Ax1, class _Traits2, class _Ax2>
static _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
{
WCHAR szStackBuffer[WINSTD_STACK_BUFFER_BYTES / sizeof(WCHAR)];
// Try to convert to stack buffer first.
int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
if (cch) {
// Copy from stack.
sWideCharStr.assign(szStackBuffer, cch);
}
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again.
cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), NULL, 0);
sWideCharStr.resize(cch);
cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (int)sMultiByteStr.length(), &sWideCharStr[0], cch);
}
SecureZeroMemory(szStackBuffer, sizeof(szStackBuffer));
return cch;
}
template<class _Traits, class _Ax>
static 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)
{
LPSTR lpBuffer;
DWORD dwResult = FormatMessageA(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPSTR>(&lpBuffer), 0, Arguments);
if (dwResult) {
str.assign(lpBuffer, dwResult);
LocalFree(lpBuffer);
}
return dwResult;
}
template<class _Traits, class _Ax>
static 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)
{
LPWSTR lpBuffer;
DWORD dwResult = FormatMessageW(dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER, lpSource, dwMessageId, dwLanguageId, reinterpret_cast<LPWSTR>(&lpBuffer), 0, Arguments);
if (dwResult) {
str.assign(lpBuffer, dwResult);
LocalFree(lpBuffer);
}
return dwResult;
}
#pragma warning(pop)
namespace winstd
{
#ifdef _UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif
template <class _Ty>
struct LocalFree_delete
{
typedef LocalFree_delete<_Ty> _Myt;
LocalFree_delete() {}
template <class _Ty2> LocalFree_delete(const LocalFree_delete<_Ty2>&) {}
void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const
{
LocalFree(_Ptr);
}
};
template <class _Ty>
struct LocalFree_delete<_Ty[]>
{
typedef LocalFree_delete<_Ty> _Myt;
LocalFree_delete() noexcept {}
void operator()(_Frees_ptr_opt_ _Ty *_Ptr) const noexcept
{
LocalFree(_Ptr);
}
template<class _Other>
void operator()(_Other *) const
{
LocalFree(_Ptr);
}
};
struct GlobalFree_delete
{
GlobalFree_delete() {}
void operator()(HGLOBAL _Ptr) const
{
GlobalFree(_Ptr);
}
};
template <class T>
class globalmem_accessor
{
WINSTD_NONCOPYABLE(globalmem_accessor)
WINSTD_NONMOVABLE(globalmem_accessor)
public:
globalmem_accessor(_In_ HGLOBAL hMem) : m_h(hMem)
{
m_data = reinterpret_cast<T*>(GlobalLock(hMem));
if (!m_data)
throw win_runtime_error("GlobalLock failed");
}
virtual ~globalmem_accessor()
{
GlobalUnlock(m_h);
}
T* data() const noexcept
{
return m_data;
}
protected:
HGLOBAL m_h;
T* m_data;
};
template <class T, const T INVAL>
class handle
{
public:
typedef T handle_type;
static const T invalid;
handle() noexcept : m_h(invalid)
{}
handle(_In_opt_ handle_type h) noexcept : m_h(h)
{}
handle(_Inout_ handle<handle_type, INVAL> &&h) noexcept
{
// Transfer handle.
m_h = h.m_h;
h.m_h = invalid;
}
private:
// This class is noncopyable.
handle(_In_ const handle<handle_type, INVAL> &h) noexcept {};
handle<handle_type, INVAL>& operator=(_In_ const handle<handle_type, INVAL> &h) noexcept {};
public:
handle<handle_type, INVAL>& operator=(_In_opt_ handle_type h) noexcept
{
attach(h);
return *this;
}
#pragma warning(suppress: 26432) // Move constructor is also present, but not detected by code analysis somehow.
handle<handle_type, INVAL>& operator=(_Inout_ handle<handle_type, INVAL> &&h) noexcept
{
if (this != std::addressof(h)) {
// Transfer handle.
if (m_h != invalid)
free_internal();
m_h = h.m_h;
h.m_h = invalid;
}
return *this;
}
operator handle_type() const
{
return m_h;
}
handle_type*& operator*() const
{
assert(m_h != invalid);
return *m_h;
}
handle_type* operator&()
{
assert(m_h == invalid);
return &m_h;
}
handle_type operator->() const
{
assert(m_h != invalid);
return m_h;
}
bool operator!() const
{
return m_h == invalid;
}
bool operator<(_In_opt_ handle_type h) const
{
return m_h < h;
}
bool operator<=(_In_opt_ handle_type h) const
{
return !operator>(h);
}
bool operator>=(_In_opt_ handle_type h) const
{
return !operator<(h);
}
bool operator>(_In_opt_ handle_type h) const
{
return h < m_h;
}
bool operator!=(_In_opt_ handle_type h) const
{
return !operator==(h);
}
bool operator==(_In_opt_ handle_type h) const
{
return m_h == h;
}
void attach(_In_opt_ handle_type h) noexcept
{
if (m_h != invalid)
free_internal();
m_h = h;
}
handle_type detach()
{
handle_type h = m_h;
m_h = invalid;
return h;
}
void free()
{
if (m_h != invalid) {
free_internal();
m_h = invalid;
}
}
protected:
virtual void free_internal() noexcept = 0;
protected:
handle_type m_h;
};
template <class T, const T INVAL>
const T handle<T, INVAL>::invalid = INVAL;
template <class T, T INVAL>
class dplhandle : public handle<T, INVAL>
{
public:
dplhandle() noexcept
{}
dplhandle(_In_opt_ handle_type h) noexcept : handle<handle_type, INVAL>(h)
{}
dplhandle<handle_type, INVAL>(_In_ const dplhandle<handle_type, INVAL> &h) : handle<handle_type, INVAL>(duplicate_internal(h.m_h))
{}
dplhandle<handle_type, INVAL>(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept : handle<handle_type, INVAL>(std::move(h))
{}
dplhandle<handle_type, INVAL>& operator=(_In_opt_ handle_type h) noexcept
{
handle<handle_type, INVAL>::operator=(h);
return *this;
}
dplhandle<handle_type, INVAL>& operator=(_In_ const dplhandle<handle_type, INVAL> &h) noexcept
{
if (this != std::addressof(h)) {
if (h.m_h != invalid) {
handle_type h_new = duplicate_internal(h.m_h);
if (m_h != invalid)
free_internal();
m_h = h_new;
} else {
if (m_h != invalid)
free_internal();
m_h = invalid;
}
}
return *this;
}
#pragma warning(disable: 26432) // Move constructor is also present, but not detected by code analysis somehow.
dplhandle<handle_type, INVAL>& operator=(_Inout_ dplhandle<handle_type, INVAL> &&h) noexcept
{
handle<handle_type, INVAL>::operator=(std::move(h));
return *this;
}
handle_type duplicate() const
{
return m_h != invalid ? duplicate_internal(m_h) : invalid;
}
void attach_duplicated(_In_opt_ handle_type h)
{
if (m_h != invalid)
free_internal();
m_h = h != invalid ? duplicate_internal(h) : invalid;
}
protected:
virtual handle_type duplicate_internal(_In_ handle_type h) const = 0;
};
inline std::string load_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage)
{
std::string sResult;
HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
if (hFoundRes) {
DWORD dwSize = SizeofResource(hModule, hFoundRes);
if (dwSize) {
HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
if (hLoadedRes) {
LPCWSTR szMessage = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
if (szMessage) {
WideCharToMultiByte(CP_UTF8, 0, szMessage, dwSize / sizeof(*szMessage), sResult, NULL, NULL);
return sResult;
} else
SetLastError(ERROR_LOCK_FAILED);
}
}
}
sprintf(sResult, "msg %u", nId);
return sResult;
}
inline std::string fmt_msg_from_res(_In_opt_ HMODULE hModule, _In_ UINT nId, _In_ WORD wLanguage, ...)
{
std::string sResult;
HRSRC hFoundRes = FindResourceExW(hModule, MAKEINTRESOURCEW(6), MAKEINTRESOURCEW(nId), wLanguage);
if (hFoundRes) {
DWORD dwSize = SizeofResource(hModule, hFoundRes);
if (dwSize) {
HGLOBAL hLoadedRes = LoadResource(hModule, hFoundRes);
if (hLoadedRes) {
LPCWSTR szFormat = reinterpret_cast<LPCWSTR>(LockResource(hLoadedRes));
if (szFormat) {
dwSize /= sizeof(*szFormat);
assert(wcsnlen(szFormat, dwSize) < dwSize); // Resource strings must be zero-terminated to make strings directly usable with sprintf.
va_list arg;
va_start(arg, wLanguage);
std::wstring sMessage;
vsprintf(sMessage, szFormat, arg);
va_end(arg);
WideCharToMultiByte(CP_UTF8, 0, sMessage, sResult, NULL, NULL);
return sResult;
} else
SetLastError(ERROR_LOCK_FAILED);
}
}
}
sprintf(sResult, "msg %u", nId);
return sResult;
}
template <typename _Tn>
class num_runtime_error : public std::runtime_error
{
public:
typedef _Tn error_type;
public:
num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
m_num(num),
runtime_error(msg)
{}
num_runtime_error(_In_ error_type num, _In_opt_z_ const char *msg = nullptr) :
m_num(num),
runtime_error(msg)
{}
error_type number() const
{
return m_num;
}
protected:
error_type m_num;
};
class last_error_saver
{
public:
last_error_saver(_In_ DWORD error = GetLastError()) :
m_error(error)
{}
~last_error_saver()
{
SetLastError(m_error);
}
protected:
DWORD m_error;
};
class win_runtime_error : public num_runtime_error<DWORD>
{
public:
win_runtime_error(_In_ error_type num) : num_runtime_error<DWORD>(num, message(num))
{}
win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
{}
win_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<DWORD>(num, std::string(msg) + ": " + message(num))
{}
win_runtime_error() : num_runtime_error<DWORD>(GetLastError(), message(GetLastError()))
{}
win_runtime_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
{}
win_runtime_error(_In_z_ const char *msg) : num_runtime_error<DWORD>(GetLastError(), std::string(msg) + ": " + message(GetLastError()))
{}
protected:
static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
{
last_error_saver last_error_save;
std::wstring wstr;
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;
}
};
template<class _Elem, class _Traits, class _Ax>
class basic_string_printf : public std::basic_string<_Elem, _Traits, _Ax>
{
public:
basic_string_printf(_In_z_ _Printf_format_string_ const _Elem *format, ...)
{
va_list arg;
va_start(arg, format);
vsprintf(*this, format, arg);
va_end(arg);
}
basic_string_printf(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
{
_Myt format;
ATLENSURE(format.LoadString(hInstance, nFormatID));
va_list arg;
va_start(arg, nFormatID);
vsprintf(*this, format, arg);
va_end(arg);
}
basic_string_printf(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
{
_Myt format;
ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
va_list arg;
va_start(arg, nFormatID);
vsprintf(*this, format, arg);
va_end(arg);
}
};
typedef basic_string_printf<char, std::char_traits<char>, std::allocator<char> > string_printf;
typedef basic_string_printf<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > wstring_printf;
#ifdef _UNICODE
typedef wstring_printf tstring_printf;
#else
typedef string_printf tstring_printf;
#endif
template<class _Elem, class _Traits, class _Ax>
class basic_string_msg : public std::basic_string<_Elem, _Traits, _Ax>
{
public:
basic_string_msg(_In_z_ _FormatMessage_format_string_ const _Elem *format, ...)
{
va_list arg;
va_start(arg, format);
FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
va_end(arg);
}
basic_string_msg(_In_ HINSTANCE hInstance, _In_ UINT nFormatID, ...)
{
_Myt format(GetManager());
ATLENSURE(format.LoadString(hInstance, nFormatID));
va_list arg;
va_start(arg, nFormatID);
FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
va_end(arg);
}
basic_string_msg(_In_ HINSTANCE hInstance, _In_ WORD wLanguageID, _In_ UINT nFormatID, ...)
{
_Myt format(GetManager());
ATLENSURE(format.LoadString(hInstance, nFormatID, wLanguageID));
va_list arg;
va_start(arg, nFormatID);
FormatMessage(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, *this, &arg);
va_end(arg);
}
basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ va_list *Arguments)
{
FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, Arguments);
}
basic_string_msg(_In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _In_opt_ DWORD_PTR *Arguments)
{
FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY, lpSource, dwMessageId, dwLanguageId, *this, (va_list*)Arguments);
}
basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ va_list *Arguments)
{
FormatMessage(dwFlags & ~FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, Arguments);
}
basic_string_msg(_In_ DWORD dwFlags, _In_z_ LPCTSTR pszFormat, _In_opt_ DWORD_PTR *Arguments)
{
FormatMessage(dwFlags | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_FROM_STRING, pszFormat, 0, 0, *this, (va_list*)Arguments);
}
};
typedef basic_string_msg<char, std::char_traits<char>, std::allocator<char> > string_msg;
typedef basic_string_msg<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > wstring_msg;
#ifdef _UNICODE
typedef wstring_msg tstring_msg;
#else
typedef string_msg tstring_msg;
#endif
template<class _Elem, class _Traits, class _Ax>
class basic_string_guid : public std::basic_string<_Elem, _Traits, _Ax>
{
public:
basic_string_guid(_In_ const GUID &guid, _In_z_ _Printf_format_string_ const _Elem *format)
{
sprintf<_Elem, _Traits, _Ax>(*this, format,
guid.Data1,
guid.Data2,
guid.Data3,
guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
}
};
class string_guid : public basic_string_guid<char, std::char_traits<char>, std::allocator<char> >
{
public:
string_guid(_In_ const GUID &guid) :
basic_string_guid<char, std::char_traits<char>, std::allocator<char> >(guid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}")
{}
};
class wstring_guid : public basic_string_guid<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
{
public:
wstring_guid(_In_ const GUID &guid) :
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}")
{}
};
#ifdef _UNICODE
typedef wstring_guid tstring_guid;
#else
typedef string_guid tstring_guid;
#endif
// winstd::sanitizing_allocator::destroy() member generates _Ptr parameter not used warning for primitive datatypes _Ty.
#pragma warning(push)
#pragma warning(disable: 4100)
template<class _Ty>
class sanitizing_allocator : public std::allocator<_Ty>
{
public:
typedef std::allocator<_Ty> _Mybase;
template<class _Other>
struct rebind
{
typedef sanitizing_allocator<_Other> other;
};
sanitizing_allocator() noexcept : _Mybase()
{}
sanitizing_allocator(_In_ const sanitizing_allocator<_Ty> &_Othr) : _Mybase(_Othr)
{}
template<class _Other>
sanitizing_allocator(_In_ const sanitizing_allocator<_Other> &_Othr) noexcept : _Mybase(_Othr)
{}
void deallocate(_In_ _Ty* const _Ptr, _In_ const std::size_t _Count)
{
// Sanitize then free.
SecureZeroMemory(_Ptr, sizeof(_Ty) * _Count);
_Mybase::deallocate(_Ptr, _Count);
}
};
#pragma warning(pop)
typedef std::basic_string<char, std::char_traits<char>, sanitizing_allocator<char> > sanitizing_string;
typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, sanitizing_allocator<wchar_t> > sanitizing_wstring;
#ifdef _UNICODE
typedef sanitizing_wstring sanitizing_tstring;
#else
typedef sanitizing_string sanitizing_tstring;
#endif
template<size_t N>
class sanitizing_blob
{
public:
sanitizing_blob()
{
ZeroMemory(m_data, N);
}
~sanitizing_blob()
{
SecureZeroMemory(m_data, N);
}
public:
unsigned char m_data[N];
};
}
winstd::basic_string_guid
Base template class to support converting GUID to string.
Definition Common.h:1695
winstd::basic_string_guid::basic_string_guid
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:1706
winstd::basic_string_msg
Base template class to support string formatting using FormatMessage() style templates.
Definition Common.h:1570
winstd::basic_string_msg::basic_string_msg
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:1645
winstd::basic_string_msg::basic_string_msg
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, va_list *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1655
winstd::basic_string_msg::basic_string_msg
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:1617
winstd::basic_string_msg::basic_string_msg
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:1635
winstd::basic_string_msg::basic_string_msg
basic_string_msg(const _Elem *format,...)
Initializes a new string and formats its contents using FormatMessage() style template.
Definition Common.h:1580
winstd::basic_string_msg::basic_string_msg
basic_string_msg(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using FormatMessage() style template in resources.
Definition Common.h:1599
winstd::basic_string_msg::basic_string_msg
basic_string_msg(DWORD dwFlags, LPCTSTR pszFormat, DWORD_PTR *Arguments)
Initializes a new string and formats its contents using FormatMessage() style.
Definition Common.h:1665
winstd::basic_string_printf
Base template class to support string formatting using printf() style templates.
Definition Common.h:1485
winstd::basic_string_printf::basic_string_printf
basic_string_printf(const _Elem *format,...)
Initializes a new string and formats its contents using printf() style template.
Definition Common.h:1495
winstd::basic_string_printf::basic_string_printf
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:1532
winstd::basic_string_printf::basic_string_printf
basic_string_printf(HINSTANCE hInstance, UINT nFormatID,...)
Initializes a new string and formats its contents using printf() style template in resources.
Definition Common.h:1514
winstd::dplhandle
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1137
winstd::dplhandle::duplicate_internal
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...
winstd::dplhandle::operator=
dplhandle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:1174
winstd::dplhandle::duplicate
handle_type duplicate() const
Duplicates and returns a new object handle.
Definition Common.h:1222
winstd::dplhandle::operator=
dplhandle< handle_type, INVAL > & operator=(dplhandle< handle_type, INVAL > &&h) noexcept
Moves the object.
Definition Common.h:1211
winstd::dplhandle::attach_duplicated
void attach_duplicated(handle_type h)
Duplicates an object handle and sets a new object handle.
Definition Common.h:1232
winstd::dplhandle::dplhandle
dplhandle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:1150
winstd::dplhandle::operator=
dplhandle< handle_type, INVAL > & operator=(const dplhandle< handle_type, INVAL > &h) noexcept
Duplicates the object.
Definition Common.h:1185
winstd::dplhandle::dplhandle
dplhandle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:1142
winstd::globalmem_accessor
Context scope automatic GlobalAlloc (un)access.
Definition Common.h:822
winstd::globalmem_accessor::m_h
HGLOBAL m_h
memory handle
Definition Common.h:858
winstd::globalmem_accessor::~globalmem_accessor
virtual ~globalmem_accessor()
Decrements the lock count associated with a memory object.
Definition Common.h:844
winstd::globalmem_accessor::m_data
T * m_data
memory pointer
Definition Common.h:859
winstd::globalmem_accessor::data
T * data() const noexcept
Return data pointer.
Definition Common.h:852
winstd::globalmem_accessor::globalmem_accessor
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:832
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition Common.h:874
winstd::handle::operator*
handle_type *& operator*() const
Returns the object handle value when the object handle is a pointer to a value (class,...
Definition Common.h:962
winstd::handle::free_internal
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
winstd::handle::handle
handle() noexcept
Initializes a new class instance with the object handle set to INVAL.
Definition Common.h:889
winstd::handle::operator>=
bool operator>=(handle_type h) const
Is handle greater than or equal to?
Definition Common.h:1038
winstd::handle::operator->
handle_type operator->() const
Provides object handle member access when the object handle is a pointer to a class or struct.
Definition Common.h:983
winstd::handle::operator&
handle_type * operator&()
Returns the object handle reference.
Definition Common.h:972
winstd::handle::handle
handle(handle_type h) noexcept
Initializes a new class instance with an already available object handle.
Definition Common.h:897
winstd::handle::operator<
bool operator<(handle_type h) const
Is handle less than?
Definition Common.h:1012
winstd::handle::operator=
handle< handle_type, INVAL > & operator=(handle_type h) noexcept
Attaches already available object handle.
Definition Common.h:923
winstd::handle::operator!
bool operator!() const
Tests if the object handle is invalid.
Definition Common.h:999
winstd::handle::operator=
handle< handle_type, INVAL > & operator=(handle< handle_type, INVAL > &&h) noexcept
Move assignment.
Definition Common.h:935
winstd::handle::operator!=
bool operator!=(handle_type h) const
Is handle not equal to?
Definition Common.h:1064
winstd::handle::free
void free()
Destroys the object.
Definition Common.h:1111
winstd::handle::m_h
handle_type m_h
Object handle.
Definition Common.h:1126
winstd::handle::attach
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1089
winstd::handle::operator==
bool operator==(handle_type h) const
Is handle equal to?
Definition Common.h:1077
winstd::handle::handle
handle(handle< handle_type, INVAL > &&h) noexcept
Move constructor.
Definition Common.h:905
winstd::handle::detach
handle_type detach()
Dismisses the object handle from this class.
Definition Common.h:1101
winstd::handle::handle_type
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:879
winstd::handle::operator>
bool operator>(handle_type h) const
Is handle greater than?
Definition Common.h:1051
winstd::handle::operator<=
bool operator<=(handle_type h) const
Is handle less than or equal to?
Definition Common.h:1025
winstd::last_error_saver
Saves GetLastError and restores SetLastError when going out of scope.
Definition Common.h:1374
winstd::last_error_saver::~last_error_saver
~last_error_saver()
Sets the last-error code for the calling thread.
Definition Common.h:1390
winstd::last_error_saver::last_error_saver
last_error_saver(DWORD error=GetLastError())
Saves the calling thread's last-error code value.
Definition Common.h:1381
winstd::num_runtime_error
Numerical runtime error.
Definition Common.h:1331
winstd::num_runtime_error::num_runtime_error
num_runtime_error(error_type num, const char *msg=nullptr)
Constructs an exception.
Definition Common.h:1353
winstd::num_runtime_error::num_runtime_error
num_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1342
winstd::num_runtime_error::number
error_type number() const
Returns the error number.
Definition Common.h:1361
winstd::num_runtime_error::m_num
error_type m_num
Numeric error code.
Definition Common.h:1367
winstd::num_runtime_error::error_type
_Tn error_type
Error number type.
Definition Common.h:1333
winstd::sanitizing_allocator
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition Common.h:1788
winstd::sanitizing_allocator::sanitizing_allocator
sanitizing_allocator(const sanitizing_allocator< _Ty > &_Othr)
Construct by copying.
Definition Common.h:1810
winstd::sanitizing_allocator::_Mybase
std::allocator< _Ty > _Mybase
Base type.
Definition Common.h:1790
winstd::sanitizing_allocator::sanitizing_allocator
sanitizing_allocator(const sanitizing_allocator< _Other > &_Othr) noexcept
Construct from a related allocator.
Definition Common.h:1817
winstd::sanitizing_allocator::deallocate
void deallocate(_Ty *const _Ptr, const std::size_t _Count)
Deallocate object at _Ptr sanitizing its content first.
Definition Common.h:1823
winstd::sanitizing_allocator::sanitizing_allocator
sanitizing_allocator() noexcept
Construct default allocator.
Definition Common.h:1804
winstd::sanitizing_blob
Sanitizing BLOB.
Definition Common.h:1865
winstd::sanitizing_blob::sanitizing_blob
sanitizing_blob()
Constructs uninitialized BLOB.
Definition Common.h:1870
winstd::sanitizing_blob::~sanitizing_blob
~sanitizing_blob()
Sanitizes BLOB.
Definition Common.h:1878
winstd::string_guid
Single-byte character implementation of a class to support converting GUID to string.
Definition Common.h:1723
winstd::string_guid::string_guid
string_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1733
winstd::win_runtime_error
Windows runtime error.
Definition Common.h:1403
winstd::win_runtime_error::win_runtime_error
win_runtime_error(const char *msg)
Constructs an exception using GetLastError()
Definition Common.h:1450
winstd::win_runtime_error::win_runtime_error
win_runtime_error(error_type num, const char *msg)
Constructs an exception.
Definition Common.h:1428
winstd::win_runtime_error::win_runtime_error
win_runtime_error(error_type num)
Constructs an exception.
Definition Common.h:1410
winstd::win_runtime_error::win_runtime_error
win_runtime_error()
Constructs an exception using GetLastError()
Definition Common.h:1434
winstd::win_runtime_error::message
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:1460
winstd::win_runtime_error::win_runtime_error
win_runtime_error(const std::string &msg)
Constructs an exception using GetLastError()
Definition Common.h:1442
winstd::win_runtime_error::win_runtime_error
win_runtime_error(error_type num, const std::string &msg)
Constructs an exception.
Definition Common.h:1419
winstd::wstring_guid
Wide character implementation of a class to support converting GUID to string.
Definition Common.h:1744
winstd::wstring_guid::wstring_guid
wstring_guid(const GUID &guid)
Initializes a new string and formats its contents to string representation of given GUID.
Definition Common.h:1754
winstd::load_msg_from_res
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:1266
winstd::fmt_msg_from_res
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:1297
WINSTD_NONCOPYABLE
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:67
WINSTD_STACK_BUFFER_BYTES
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
winstd::tstring
std::string tstring
Multi-byte / Wide-character string (according to _UNICODE)
Definition Common.h:731
WINSTD_NONMOVABLE
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition Common.h:75
winstd::sanitizing_wstring
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:1849
winstd::sanitizing_tstring
sanitizing_string sanitizing_tstring
Multi-byte / Wide-character sanitizing string (according to _UNICODE)
Definition Common.h:1857
winstd::sanitizing_string
std::basic_string< char, std::char_traits< char >, sanitizing_allocator< char > > sanitizing_string
A sanitizing variant of std::string.
Definition Common.h:1840
SecureWideCharToMultiByte
static int SecureWideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, std::basic_string< char, _Traits, _Ax > &sMultiByteStr, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) noexcept
Maps a UTF-16 (wide character) string to a std::string. The new character string is not necessarily f...
Definition Common.h:429
winstd::wstring_printf
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:1554
MultiByteToWideChar
static int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, std::basic_string< wchar_t, _Traits, _Ax > &sWideCharStr) noexcept
Maps a character string to a UTF-16 (wide character) std::wstring. The character string is not necess...
Definition Common.h:518
FormatMessageW
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:705
FormatMessageA
static DWORD FormatMessageA(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, std::basic_string< char, _Traits, _Ax > &str, va_list *Arguments)
Formats a message string.
Definition Common.h:688
winstd::tstring_guid
string_guid tstring_guid
Multi-byte / Wide-character string GUID (according to _UNICODE)
Definition Common.h:1767
winstd::wstring_msg
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:1679
winstd::string_printf
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:1549
SecureMultiByteToWideChar
static int SecureMultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, std::basic_string< wchar_t, _Traits, _Ax > &sWideCharStr) noexcept
Maps a character string to a UTF-16 (wide character) std::wstring. The character string is not necess...
Definition Common.h:599
winstd::tstring_printf
string_printf tstring_printf
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1562
WideCharToMultiByte
static int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, std::basic_string< char, _Traits, _Ax > &sMultiByteStr, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) noexcept
Maps a UTF-16 (wide character) string to a std::string. The new character string is not necessarily f...
Definition Common.h:348
sprintf
static int sprintf(std::basic_string< _Elem, _Traits, _Ax > &str, const _Elem *format,...)
Formats string using printf().
Definition Common.h:333
vsprintf
static int vsprintf(std::basic_string< char, _Traits, _Ax > &str, const char *format, va_list arg)
Formats string using printf().
Definition Common.h:256
winstd::string_msg
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:1674
winstd::tstring_msg
string_msg tstring_msg
Multi-byte / Wide-character formatted string (according to _UNICODE)
Definition Common.h:1687
winstd::handle::invalid
static const T invalid
Invalid handle value.
Definition Common.h:884
winstd::GlobalFree_delete
Deleter for unique_ptr using GlobalFree.
Definition Common.h:800
winstd::GlobalFree_delete::GlobalFree_delete
GlobalFree_delete()
Default construct.
Definition Common.h:804
winstd::GlobalFree_delete::operator()
void operator()(HGLOBAL _Ptr) const
Delete a pointer.
Definition Common.h:811
winstd::LocalFree_delete< _Ty[]>::_Myt
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:769
winstd::LocalFree_delete< _Ty[]>::LocalFree_delete
LocalFree_delete() noexcept
Default construct.
Definition Common.h:774
winstd::LocalFree_delete< _Ty[]>::operator()
void operator()(_Other *) const
Delete a pointer of another type.
Definition Common.h:790
winstd::LocalFree_delete< _Ty[]>::operator()
void operator()(_Ty *_Ptr) const noexcept
Delete a pointer.
Definition Common.h:779
winstd::LocalFree_delete
Deleter for unique_ptr using LocalFree.
Definition Common.h:739
winstd::LocalFree_delete::_Myt
LocalFree_delete< _Ty > _Myt
This type.
Definition Common.h:740
winstd::LocalFree_delete::LocalFree_delete
LocalFree_delete(const LocalFree_delete< _Ty2 > &)
Construct from another LocalFree_delete.
Definition Common.h:750
winstd::LocalFree_delete::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition Common.h:757
winstd::LocalFree_delete::LocalFree_delete
LocalFree_delete()
Default construct.
Definition Common.h:745
winstd::sanitizing_allocator::rebind
Convert this type to sanitizing_allocator<_Other>
Definition Common.h:1797
winstd::sanitizing_allocator::rebind::other
sanitizing_allocator< _Other > other
Other type.
Definition Common.h:1798