14#pragma warning(disable: 4505)
25#define WINSTD_WINHANDLE_IMPL(C, INVAL) \
28 C (_In_opt_ handle_type h) noexcept : win_handle<INVAL>( h ) { } \
29 C (_Inout_ C &&h) noexcept : win_handle<INVAL>(std::move(h)) { } \
30 C& operator=(_In_opt_ handle_type h) noexcept { win_handle<INVAL>::operator=( h ); return *this; } \
31 C& operator=(_Inout_ C &&h) noexcept { win_handle<INVAL>::operator=(std::move(h)); return *this; } \
35template<
class _Traits,
class _Ax>
36static DWORD GetModuleFileNameA(_In_opt_ HMODULE hModule, _Out_ std::basic_string<char, _Traits, _Ax> &sValue)
noexcept
43 DWORD dwResult = ::GetModuleFileNameA(hModule, szStackBuffer, _countof(szStackBuffer));
44 if (dwResult < _countof(szStackBuffer)) {
46 sValue.assign(szStackBuffer, dwResult);
51 std::unique_ptr<char[]> szBuffer(
new char[dwCapacity]);
52 dwResult = ::GetModuleFileNameA(hModule, szBuffer.get(), dwCapacity);
53 if (dwResult < dwCapacity) {
54 sValue.assign(szBuffer.get(), dwResult);
66template<
class _Traits,
class _Ax>
67static DWORD GetModuleFileNameW(_In_opt_ HMODULE hModule, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sValue)
noexcept
72 DWORD dwResult = ::GetModuleFileNameW(hModule, szStackBuffer, _countof(szStackBuffer));
73 if (dwResult < _countof(szStackBuffer)) {
75 sValue.assign(szStackBuffer, dwResult);
80 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[dwCapacity]);
81 dwResult = ::GetModuleFileNameW(hModule, szBuffer.get(), dwCapacity);
82 if (dwResult < dwCapacity) {
83 sValue.assign(szBuffer.get(), dwResult);
91template<
class _Traits,
class _Ax>
92static _Success_(
return != 0) int GetWindowTextA(_In_ HWND hWnd, _Out_ std::basic_string<
char, _Traits, _Ax> &sValue) noexcept
99 iResult = ::GetWindowTextLengthA(hWnd);
104 iResult = ::GetWindowTextA(hWnd, szBuffer, _countof(szBuffer));
105 sValue.assign(szBuffer, iResult);
108 std::unique_ptr<char[]> szBuffer(
new char[++iResult]);
109 iResult = ::GetWindowTextA(hWnd, szBuffer.get(), iResult);
110 sValue.assign(szBuffer.get(), iResult);
124template<
class _Traits,
class _Ax>
125static _Success_(
return != 0) int GetWindowTextW(_In_ HWND hWnd, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sValue) noexcept
132 iResult = ::GetWindowTextLengthW(hWnd);
137 iResult = ::GetWindowTextW(hWnd, szBuffer, _countof(szBuffer));
138 sValue.assign(szBuffer, iResult);
141 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[++iResult]);
142 iResult = ::GetWindowTextW(hWnd, szBuffer.get(), iResult);
143 sValue.assign(szBuffer.get(), iResult);
153template<
class _Ty,
class _Ax>
154static _Success_(
return != 0) BOOL GetFileVersionInfoA(_In_z_ LPCSTR lptstrFilename, __reserved DWORD dwHandle, _Out_ std::vector<_Ty, _Ax> &aValue) noexcept
159 DWORD dwVerInfoSize = ::GetFileVersionInfoSizeA(lptstrFilename, &dwHandle);
160 if (dwVerInfoSize != 0) {
162 aValue.resize((dwVerInfoSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
163 return ::GetFileVersionInfoA(lptstrFilename, dwHandle, dwVerInfoSize, aValue.data());
173template<
class _Ty,
class _Ax>
174static _Success_(
return != 0) BOOL GetFileVersionInfoW(_In_z_ LPCWSTR lptstrFilename, __reserved DWORD dwHandle, _Out_ std::vector<_Ty, _Ax> &aValue) noexcept
179 DWORD dwVerInfoSize = ::GetFileVersionInfoSizeW(lptstrFilename, &dwHandle);
180 if (dwVerInfoSize != 0) {
182 aValue.resize((dwVerInfoSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
183 return ::GetFileVersionInfoW(lptstrFilename, dwHandle, dwVerInfoSize, aValue.data());
189template<
class _Traits,
class _Ax>
190static _Success_(
return != 0) DWORD ExpandEnvironmentStringsA(_In_z_ LPCSTR lpSrc, _Out_ std::basic_string<
char, _Traits, _Ax> &sValue) noexcept
194 for (DWORD dwSizeOut = (DWORD)strlen(lpSrc) + 0x100;;) {
195 DWORD dwSizeIn = dwSizeOut;
196 std::unique_ptr<char[]> szBuffer(
new char[(
size_t)dwSizeIn + 2]);
197 dwSizeOut = ::ExpandEnvironmentStringsA(lpSrc, szBuffer.get(), dwSizeIn);
198 if (dwSizeOut == 0) {
201 }
else if (dwSizeOut <= dwSizeIn) {
203 sValue.assign(szBuffer.get(), dwSizeOut - 1);
217template<
class _Traits,
class _Ax>
218static _Success_(
return != 0) DWORD ExpandEnvironmentStringsW(_In_z_ LPCWSTR lpSrc, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sValue) noexcept
220 for (DWORD dwSizeOut = (DWORD)wcslen(lpSrc) + 0x100;;) {
221 DWORD dwSizeIn = dwSizeOut;
222 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[(
size_t)dwSizeIn + 1]);
223 dwSizeOut = ::ExpandEnvironmentStringsW(lpSrc, szBuffer.get(), dwSizeIn);
224 if (dwSizeOut == 0) {
227 }
else if (dwSizeOut <= dwSizeIn) {
229 sValue.assign(szBuffer.get(), dwSizeOut - 1);
239template<
class _Traits,
class _Ax>
240static VOID GuidToStringA(_In_ LPCGUID lpGuid, _Out_ std::basic_string<char, _Traits, _Ax> &str)
noexcept
244 sprintf(str,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
248 lpGuid->Data4[0], lpGuid->Data4[1],
249 lpGuid->Data4[2], lpGuid->Data4[3], lpGuid->Data4[4], lpGuid->Data4[5], lpGuid->Data4[6], lpGuid->Data4[7]);
258template<
class _Traits,
class _Ax>
259static VOID GuidToStringW(_In_ LPCGUID lpGuid, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &str)
noexcept
263 sprintf(str, L
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
267 lpGuid->Data4[0], lpGuid->Data4[1],
268 lpGuid->Data4[2], lpGuid->Data4[3], lpGuid->Data4[4], lpGuid->Data4[5], lpGuid->Data4[6], lpGuid->Data4[7]);
273#define GuidToString GuidToStringW
275#define GuidToString GuidToStringA
279static _Success_(
return) BOOL StringToGuidA(_In_z_ LPCSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCSTR *lpszGuidEnd = NULL)
noexcept
284 unsigned long long ullTmp;
286 if (!lpszGuid || !lpGuid || *lpszGuid !=
'{')
return FALSE;
289 g.Data1 = strtoul(lpszGuid, &lpszEnd, 16);
290 if (errno == ERANGE)
return FALSE;
293 if (*lpszGuid !=
'-')
return FALSE;
296 ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
297 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
298 g.Data2 =
static_cast<unsigned short>(ulTmp);
301 if (*lpszGuid !=
'-')
return FALSE;
304 ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
305 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
306 g.Data3 =
static_cast<unsigned short>(ulTmp);
309 if (*lpszGuid !=
'-')
return FALSE;
312 ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
313 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
314 g.Data4[0] =
static_cast<unsigned char>((ulTmp >> 8) & 0xff);
315 g.Data4[1] =
static_cast<unsigned char>( ulTmp & 0xff);
318 if (*lpszGuid !=
'-')
return FALSE;
321 ullTmp = _strtoui64(lpszGuid, &lpszEnd, 16);
322 if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF)
return FALSE;
323 g.Data4[2] =
static_cast<unsigned char>((ullTmp >> 40) & 0xff);
324 g.Data4[3] =
static_cast<unsigned char>((ullTmp >> 32) & 0xff);
325 g.Data4[4] =
static_cast<unsigned char>((ullTmp >> 24) & 0xff);
326 g.Data4[5] =
static_cast<unsigned char>((ullTmp >> 16) & 0xff);
327 g.Data4[6] =
static_cast<unsigned char>((ullTmp >> 8) & 0xff);
328 g.Data4[7] =
static_cast<unsigned char>( ullTmp & 0xff);
331 if (*lpszGuid !=
'}')
return FALSE;
335 *lpszGuidEnd = lpszGuid;
352static _Success_(
return) BOOL StringToGuidW(_In_z_ LPCWSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCWSTR *lpszGuidEnd = NULL)
noexcept
357 unsigned long long ullTmp;
359 if (!lpszGuid || !lpGuid || *lpszGuid !=
'{')
return FALSE;
362 g.Data1 = wcstoul(lpszGuid, &lpszEnd, 16);
363 if (errno == ERANGE)
return FALSE;
366 if (*lpszGuid !=
'-')
return FALSE;
369 ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
370 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
371 g.Data2 =
static_cast<unsigned short>(ulTmp);
374 if (*lpszGuid !=
'-')
return FALSE;
377 ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
378 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
379 g.Data3 =
static_cast<unsigned short>(ulTmp);
382 if (*lpszGuid !=
'-')
return FALSE;
385 ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
386 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
387 g.Data4[0] =
static_cast<unsigned char>((ulTmp >> 8) & 0xff);
388 g.Data4[1] =
static_cast<unsigned char>( ulTmp & 0xff);
391 if (*lpszGuid !=
'-')
return FALSE;
394 ullTmp = _wcstoui64(lpszGuid, &lpszEnd, 16);
395 if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF)
return FALSE;
396 g.Data4[2] =
static_cast<unsigned char>((ullTmp >> 40) & 0xff);
397 g.Data4[3] =
static_cast<unsigned char>((ullTmp >> 32) & 0xff);
398 g.Data4[4] =
static_cast<unsigned char>((ullTmp >> 24) & 0xff);
399 g.Data4[5] =
static_cast<unsigned char>((ullTmp >> 16) & 0xff);
400 g.Data4[6] =
static_cast<unsigned char>((ullTmp >> 8) & 0xff);
401 g.Data4[7] =
static_cast<unsigned char>( ullTmp & 0xff);
404 if (*lpszGuid !=
'}')
return FALSE;
408 *lpszGuidEnd = lpszGuid;
416#define StringToGuid StringToGuidW
418#define StringToGuid StringToGuidA
439template<
class _Traits,
class _Ax>
440static LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Out_ std::basic_string<char, _Traits, _Ax> &sValue)
noexcept
444 DWORD dwSize =
sizeof(aStackBuffer), dwType;
447 lResult = ::RegQueryValueExA(hReg, pszName, NULL, &dwType, aStackBuffer, &dwSize);
448 if (lResult == ERROR_SUCCESS) {
449 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
451 dwSize /=
sizeof(CHAR);
452 sValue.assign(
reinterpret_cast<LPCSTR
>(aStackBuffer), dwSize &&
reinterpret_cast<LPCSTR
>(aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
453 }
else if (dwType == REG_EXPAND_SZ) {
455 if (::ExpandEnvironmentStringsA(
reinterpret_cast<LPCSTR
>(aStackBuffer), sValue) == 0)
456 lResult = ::GetLastError();
459 lResult = ERROR_INVALID_DATA;
461 }
else if (lResult == ERROR_MORE_DATA) {
462 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
464 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[dwSize /
sizeof(CHAR)]);
465 if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
466 dwSize /=
sizeof(CHAR);
467 sValue.assign(szBuffer.get(), dwSize && szBuffer[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
469 }
else if (dwType == REG_EXPAND_SZ) {
471 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[dwSize /
sizeof(CHAR)]);
472 if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
473 if (::ExpandEnvironmentStringsA(szBuffer.get(), sValue) == 0)
474 lResult = ::GetLastError();
478 lResult = ERROR_INVALID_DATA;
503template<
class _Traits,
class _Ax>
504static LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sValue)
noexcept
508 DWORD dwSize =
sizeof(aStackBuffer), dwType;
511 lResult = ::RegQueryValueExW(hReg, pszName, NULL, &dwType, aStackBuffer, &dwSize);
512 if (lResult == ERROR_SUCCESS) {
513 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
515 dwSize /=
sizeof(WCHAR);
516 sValue.assign(
reinterpret_cast<LPCWSTR
>(aStackBuffer), dwSize &&
reinterpret_cast<LPCWSTR
>(aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
517 }
else if (dwType == REG_EXPAND_SZ) {
519 if (::ExpandEnvironmentStringsW(
reinterpret_cast<LPCWSTR
>(aStackBuffer), sValue) == 0)
520 lResult = ::GetLastError();
523 lResult = ERROR_INVALID_DATA;
525 }
else if (lResult == ERROR_MORE_DATA) {
526 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
528 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[dwSize /
sizeof(WCHAR)]);
529 if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
530 dwSize /=
sizeof(WCHAR);
531 sValue.assign(szBuffer.get(), dwSize && szBuffer[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
533 }
else if (dwType == REG_EXPAND_SZ) {
535 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[dwSize /
sizeof(WCHAR)]);
536 if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
537 if (::ExpandEnvironmentStringsW(szBuffer.get(), sValue) == 0)
538 lResult = ::GetLastError();
542 lResult = ERROR_INVALID_DATA;
550template<
class _Ty,
class _Ax>
551static LSTATUS RegQueryValueExA(_In_ HKEY hKey, _In_opt_z_ LPCSTR lpValueName, __reserved LPDWORD lpReserved, _Out_opt_ LPDWORD lpType, _Out_ std::vector<_Ty, _Ax> &aData)
noexcept
555 DWORD dwSize =
sizeof(aStackBuffer);
558 lResult = RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, aStackBuffer, &dwSize);
559 if (lResult == ERROR_SUCCESS) {
561 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
562 memcpy(aData.data(), aStackBuffer, dwSize);
563 }
else if (lResult == ERROR_MORE_DATA) {
565 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
566 lResult = RegQueryValueExA(hKey, lpValueName, lpReserved, NULL, aData.data(), &dwSize);
577template<
class _Ty,
class _Ax>
578static LSTATUS RegQueryValueExW(_In_ HKEY hKey, _In_opt_z_ LPCWSTR lpValueName, __reserved LPDWORD lpReserved, _Out_opt_ LPDWORD lpType, _Out_ std::vector<_Ty, _Ax> &aData)
noexcept
582 DWORD dwSize =
sizeof(aStackBuffer);
585 lResult = RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, aStackBuffer, &dwSize);
586 if (lResult == ERROR_SUCCESS) {
588 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
589 memcpy(aData.data(), aStackBuffer, dwSize);
590 }
else if (lResult == ERROR_MORE_DATA) {
592 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
593 lResult = RegQueryValueExW(hKey, lpValueName, lpReserved, NULL, aData.data(), &dwSize);
599#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
602template<
class _Traits,
class _Ax>
603static LSTATUS RegLoadMUIStringA(_In_ HKEY hKey, _In_opt_z_ LPCSTR pszValue, _Out_ std::basic_string<char, _Traits, _Ax> &sOut, _In_ DWORD Flags, _In_opt_z_ LPCSTR pszDirectory)
noexcept
608 return ERROR_CALL_NOT_IMPLEMENTED;
616template<
class _Traits,
class _Ax>
617static LSTATUS RegLoadMUIStringW(_In_ HKEY hKey, _In_opt_z_ LPCWSTR pszValue, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sOut, _In_ DWORD Flags, _In_opt_z_ LPCWSTR pszDirectory)
noexcept
623 Flags &= ~REG_MUI_STRING_TRUNCATE;
626 lResult = RegLoadMUIStringW(hKey, pszValue, szStackBuffer,
sizeof(szStackBuffer), &dwSize, Flags, pszDirectory);
627 if (lResult == ERROR_SUCCESS) {
629 sOut.assign(szStackBuffer, wcsnlen(szStackBuffer, dwSize/
sizeof(
wchar_t)));
630 }
else if (lResult == ERROR_MORE_DATA) {
632 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[(dwSize +
sizeof(
wchar_t) - 1)/
sizeof(
wchar_t)]);
633 sOut.assign(szBuffer.get(), (lResult = RegLoadMUIStringW(hKey, pszValue, szBuffer.get(), dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? wcsnlen(szBuffer.get(), dwSize/
sizeof(
wchar_t)) : 0);
646template<
class _Traits,
class _Ax>
647static _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
652 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
655 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (
size_t)cch - 1);
656 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
658 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
659 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
660 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
661 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
673static _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
678 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
681 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
682 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
684 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
685 sMultiByteStr.resize(cch);
686 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
697template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
698static _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
703 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
706 sMultiByteStr.assign(szStackBuffer, cch);
707 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
709 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
710 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
711 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
712 sMultiByteStr.assign(szBuffer.get(), cch);
725template<
class _Traits,
class _Ax>
726static _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
731 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
734 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (
size_t)cch - 1);
735 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
737 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
738 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
739 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
740 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
741 SecureZeroMemory(szBuffer.get(),
sizeof(CHAR)*cch);
744 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
757static _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
762 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
765 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
766 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
768 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
769 sMultiByteStr.resize(cch);
770 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
773 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
785template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
786static _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
791 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
794 sMultiByteStr.assign(szStackBuffer, cch);
795 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
797 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
798 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
799 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
800 sMultiByteStr.assign(szBuffer.get(), cch);
801 SecureZeroMemory(szBuffer.get(),
sizeof(CHAR)*cch);
804 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
814template<
class _Traits,
class _Ax>
815static _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
820 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
823 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
824 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
826 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
827 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
828 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
829 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
841static _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
846 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
849 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
850 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
852 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
853 sWideCharStr.resize(cch);
854 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
865template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
866static _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
871 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
874 sWideCharStr.assign(szStackBuffer, cch);
875 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
877 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
878 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
879 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szBuffer.get(), cch);
880 sWideCharStr.assign(szBuffer.get(), cch);
893template<
class _Traits,
class _Ax>
894static _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
899 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
902 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
903 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
905 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
906 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
907 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
908 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
909 SecureZeroMemory(szBuffer.get(),
sizeof(WCHAR)*cch);
912 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
925static _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
930 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
933 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
934 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
936 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
937 sWideCharStr.resize(cch);
938 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
941 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
953template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
954static _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
959 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
962 sWideCharStr.assign(szStackBuffer, cch);
963 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
965 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
966 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
967 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szBuffer.get(), cch);
968 sWideCharStr.assign(szBuffer.get(), cch);
969 SecureZeroMemory(szBuffer.get(),
sizeof(WCHAR)*cch);
972 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
982template<
class _Traits,
class _Ax>
983static _Success_(
return > 0) int NormalizeString(_In_ NORM_FORM NormForm, _In_ LPCWSTR lpSrcString, _In_
int cwSrcLength, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sDstString) noexcept
988 int cch = ::NormalizeString(NormForm, lpSrcString, cwSrcLength, szStackBuffer, _countof(szStackBuffer));
991 sDstString.assign(szStackBuffer, cwSrcLength != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
993 switch (::GetLastError()) {
994 case ERROR_INSUFFICIENT_BUFFER:
995 for (
int i = 10; i--;) {
998 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
999 cch = ::NormalizeString(NormForm, lpSrcString, cwSrcLength, szBuffer.get(), cch);
1001 sDstString.assign(szBuffer.get(), cwSrcLength != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
1004 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1025template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
1026static _Success_(
return > 0) int NormalizeString(_In_ NORM_FORM NormForm, _In_ const std::basic_string<
wchar_t, _Traits1, _Ax1> &sSrcString, _Out_ std::basic_string<
wchar_t, _Traits2, _Ax2> &sDstString) noexcept
1031 int cch = ::NormalizeString(NormForm, sSrcString.c_str(), (
int)sSrcString.length(), szStackBuffer, _countof(szStackBuffer));
1034 sDstString.assign(szStackBuffer, cch);
1036 switch (::GetLastError()) {
1037 case ERROR_INSUFFICIENT_BUFFER:
1038 for (
int i = 10; i--;) {
1041 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
1042 cch = ::NormalizeString(NormForm, sSrcString.c_str(), (
int)sSrcString.length(), szBuffer.get(), cch);
1044 sDstString.assign(szBuffer.get(), cch);
1047 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1064template<
class _Traits,
class _Ax>
1065static _Success_(
return != 0) int WINAPI LoadStringA(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_ std::basic_string<
char, _Traits, _Ax> &sBuffer) noexcept
1069 int i = LoadStringA(hInstance, uID,
reinterpret_cast<LPSTR
>(&pszStr), 0);
1071 sBuffer.assign(pszStr, i);
1082template<
class _Traits,
class _Ax>
1083static _Success_(
return != 0) int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sBuffer) noexcept
1087 int i = LoadStringW(hInstance, uID,
reinterpret_cast<LPWSTR
>(&pszStr), 0);
1089 sBuffer.assign(pszStr, i);
1100static VOID OutputDebugStrV(_In_z_ LPCSTR lpOutputString, _In_ va_list arg)
noexcept
1103 try { vsprintf(str, lpOutputString, arg); }
catch (...) {
return; }
1104 OutputDebugStringA(str.c_str());
1112static VOID OutputDebugStrV(_In_z_ LPCWSTR lpOutputString, _In_ va_list arg)
noexcept
1115 try { vsprintf(str, lpOutputString, arg); }
catch (...) {
return; }
1116 OutputDebugStringW(str.c_str());
1124static VOID OutputDebugStr(_In_z_ LPCSTR lpOutputString, ...) noexcept
1127 va_start(arg, lpOutputString);
1128 OutputDebugStrV(lpOutputString, arg);
1137static VOID OutputDebugStr(_In_z_ LPCWSTR lpOutputString, ...) noexcept
1140 va_start(arg, lpOutputString);
1141 OutputDebugStrV(lpOutputString, arg);
1146template<
class _Traits,
class _Ax>
1147static _Success_(
return != 0) int GetDateFormatA(_In_ LCID Locale, _In_ DWORD dwFlags, _In_opt_ const SYSTEMTIME *lpDate, _In_opt_z_ LPCSTR lpFormat, _Out_ std::basic_string<
char, _Traits, _Ax> &sDate) noexcept
1149 int iResult = GetDateFormatA(Locale, dwFlags, lpDate, lpFormat, NULL, 0);
1152 std::unique_ptr<char[]> szBuffer(
new char[iResult]);
1153 iResult = GetDateFormatA(Locale, dwFlags, lpDate, lpFormat, szBuffer.get(), iResult);
1154 sDate.assign(szBuffer.get(), iResult ? iResult - 1 : 0);
1166template<
class _Traits,
class _Ax>
1167static _Success_(
return != 0) int GetDateFormatW(_In_ LCID Locale, _In_ DWORD dwFlags, _In_opt_ const SYSTEMTIME *lpDate, _In_opt_z_ LPCWSTR lpFormat, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sDate) noexcept
1169 int iResult = GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, NULL, 0);
1172 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[iResult]);
1173 iResult = GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, szBuffer.get(), iResult);
1174 sDate.assign(szBuffer.get(), iResult ? iResult - 1 : 0);
1182template<
class _Traits,
class _Ax>
1183static _Success_(
return != 0) BOOL LookupAccountSidA(_In_opt_z_ LPCSTR lpSystemName, _In_ PSID lpSid, _Out_opt_ std::basic_string<
char, _Traits, _Ax> *sName, _Out_opt_ std::basic_string<
char, _Traits, _Ax> *sReferencedDomainName, _Out_ PSID_NAME_USE peUse) noexcept
1187 DWORD dwNameLen = 0, dwRefDomainLen = 0;
1189 if (LookupAccountSidA(lpSystemName, lpSid,
1191 NULL, &dwRefDomainLen,
1195 if (sName ) sName ->clear();
1196 if (sReferencedDomainName) sReferencedDomainName->clear();
1198 }
else if (GetLastError() == ERROR_MORE_DATA) {
1200 std::unique_ptr<char[]> bufName (
new char[dwNameLen ]);
1201 std::unique_ptr<char[]> bufRefDomain(
new char[dwRefDomainLen]);
1202 if (LookupAccountSidA(lpSystemName, lpSid,
1203 bufName .get(), &dwNameLen ,
1204 bufRefDomain.get(), &dwRefDomainLen,
1207 if (sName ) sName ->assign(bufName .get(), dwNameLen - 1);
1208 if (sReferencedDomainName) sReferencedDomainName->assign(bufRefDomain.get(), dwRefDomainLen - 1);
1221template<
class _Traits,
class _Ax>
1222static _Success_(
return != 0) BOOL LookupAccountSidW(_In_opt_z_ LPCWSTR lpSystemName, _In_ PSID lpSid, _Out_opt_ std::basic_string<
wchar_t, _Traits, _Ax> *sName, _Out_opt_ std::basic_string<
wchar_t, _Traits, _Ax> *sReferencedDomainName, _Out_ PSID_NAME_USE peUse) noexcept
1226 DWORD dwNameLen = 0, dwRefDomainLen = 0;
1228 if (LookupAccountSidW(lpSystemName, lpSid,
1230 NULL, &dwRefDomainLen,
1234 if (sName ) sName ->clear();
1235 if (sReferencedDomainName) sReferencedDomainName->clear();
1237 }
else if (GetLastError() == ERROR_MORE_DATA) {
1239 std::unique_ptr<wchar_t[]> bufName (
new wchar_t[dwNameLen ]);
1240 std::unique_ptr<wchar_t[]> bufRefDomain(
new wchar_t[dwRefDomainLen]);
1241 if (LookupAccountSidW(lpSystemName, lpSid,
1242 bufName .get(), &dwNameLen ,
1243 bufRefDomain.get(), &dwRefDomainLen,
1246 if (sName ) sName ->assign(bufName .get(), dwNameLen - 1);
1247 if (sReferencedDomainName) sReferencedDomainName->assign(bufRefDomain.get(), dwRefDomainLen - 1);
1261static _Success_(
return != 0) BOOL GetTokenInformation(_In_ HANDLE TokenHandle, _In_ TOKEN_INFORMATION_CLASS TokenInformationClass, _Out_ std::unique_ptr<_Ty> &TokenInformation) noexcept
1266 if (GetTokenInformation(TokenHandle, TokenInformationClass, szStackBuffer,
sizeof(szStackBuffer), &dwSize)) {
1268 TokenInformation.reset((_Ty*)(
new BYTE[dwSize /
sizeof(BYTE)]));
1269 if (!TokenInformation) {
1270 SetLastError(ERROR_OUTOFMEMORY);
1273 memcpy(TokenInformation.get(), szStackBuffer, dwSize);
1275 }
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1277 TokenInformation.reset((_Ty*)(
new BYTE[dwSize /
sizeof(BYTE)]));
1278 if (!TokenInformation) {
1279 SetLastError(ERROR_OUTOFMEMORY);
1282 return GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation.get(), dwSize, &dwSize);
1292template<
class _Traits,
class _Ax>
1293static _Success_(
return != 0) BOOL QueryFullProcessImageNameA(_In_ HANDLE hProcess, _In_ DWORD dwFlags, _Inout_ std::basic_string<
char, _Traits, _Ax>& sExeName)
1296 DWORD dwSize = _countof(szStackBuffer);
1299 if (::QueryFullProcessImageNameA(hProcess, dwFlags, szStackBuffer, &dwSize)) {
1301 sExeName.assign(szStackBuffer, dwSize);
1304 for (DWORD dwCapacity = 2 *
WINSTD_STACK_BUFFER_BYTES /
sizeof(
char); GetLastError() == ERROR_INSUFFICIENT_BUFFER; dwCapacity *= 2) {
1306 std::unique_ptr<char[]> szBuffer(
new char[dwCapacity]);
1307 dwSize = dwCapacity;
1308 if (::QueryFullProcessImageNameA(hProcess, dwFlags, szBuffer.get(), &dwSize)) {
1309 sExeName.assign(szBuffer.get(), dwSize);
1321template<
class _Traits,
class _Ax>
1322static _Success_(
return != 0) BOOL QueryFullProcessImageNameW(_In_ HANDLE hProcess, _In_ DWORD dwFlags, _Inout_ std::basic_string<
wchar_t, _Traits, _Ax>& sExeName)
1325 DWORD dwSize = _countof(szStackBuffer);
1328 if (::QueryFullProcessImageNameW(hProcess, dwFlags, szStackBuffer, &dwSize)) {
1330 sExeName.assign(szStackBuffer, dwSize);
1333 for (DWORD dwCapacity = 2 *
WINSTD_STACK_BUFFER_BYTES /
sizeof(
wchar_t); GetLastError() == ERROR_INSUFFICIENT_BUFFER; dwCapacity *= 2) {
1335 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[dwCapacity]);
1336 dwSize = dwCapacity;
1337 if (::QueryFullProcessImageNameW(hProcess, dwFlags, szBuffer.get(), &dwSize)) {
1338 sExeName.assign(szBuffer.get(), dwSize);
1357 template<HANDLE INVALID>
1415 bool load(_In_z_ LPCTSTR lpFileName, __reserved
handle_type hFile, _In_ DWORD dwFlags) noexcept
1417 handle_type h = LoadLibraryEx(lpFileName, hFile, dwFlags);
1455 bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwProcessId) noexcept
1457 handle_type h = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
1498 bool create(_In_z_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_ DWORD dwCreationDisposition, _In_opt_ DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL, _In_opt_ HANDLE hTemplateFile = NULL) noexcept
1500 handle_type h = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
1527 bool create(_In_ HANDLE hFile, _In_ DWORD flProtect, _In_ DWORD dwMaximumSizeHigh, _In_ DWORD dwMaximumSizeLow, _In_opt_ LPSECURITY_ATTRIBUTES lpFileMappingAttributes = NULL, _In_opt_ LPCTSTR lpName = NULL) noexcept
1529 handle_type h = CreateFileMapping(hFile, lpFileMappingAttributes, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
1560 if (!UnmapViewOfFile(_Ptr))
1582 if (!UnmapViewOfFile(_Ptr))
1589 template<
class _Other>
1592 if (!UnmapViewOfFile(_Ptr))
1615 bool create(_In_ BOOL bManualReset, _In_ BOOL bInitialState, _In_opt_ LPSECURITY_ATTRIBUTES lpEventAttributes = NULL, _In_opt_z_ LPCTSTR lpName = NULL) noexcept
1617 handle_type h = CreateEvent(lpEventAttributes, bManualReset, bInitialState, lpName);
1635 bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_z_ LPCTSTR lpName) noexcept
1637 handle_type h = OpenEvent(dwDesiredAccess, bInheritHandle, lpName);
1663 InitializeCriticalSection(&
m_data);
1664 } __except(EXCEPTION_EXECUTE_HANDLER) {
1665 throw std::runtime_error(
"InitializeCriticalSection failed");
1676 DeleteCriticalSection(&
m_data);
1684 operator LPCRITICAL_SECTION() noexcept
1722 bool find(_In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFileData) noexcept
1724 handle_type h = FindFirstFile(lpFileName, lpFindFileData);
1773 bool create(_In_ DWORD flOptions, _In_ SIZE_T dwInitialSize, _In_ SIZE_T dwMaximumSize) noexcept
1775 handle_type h = HeapCreate(flOptions, dwInitialSize, dwMaximumSize);
1799 PROCESS_HEAP_ENTRY e;
1801 while (HeapWalk(
m_h, &e) != FALSE) {
1802 if ((e.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
1804 _T(
"Allocated block%s%s\n")
1805 _T(
" Data portion begins at: %#p\n Size: %d bytes\n")
1806 _T(
" Overhead: %d bytes\n Region index: %d\n"),
1807 (e.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0 ?
tstring_printf(_T(
", movable with HANDLE %#p"), e.Block.hMem).c_str() : _T(
""),
1808 (e.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0 ? _T(
", DDESHARE") : _T(
""),
1818 const DWORD dwResult = GetLastError();
1819 if (dwResult != ERROR_NO_MORE_ITEMS)
1820 OutputDebugStr(_T(
"HeapWalk failed (error %u).\n"), dwResult);
1844 template <
class _Ty>
1861 template <
class _Other>
1882 template <
class _Other>
1908 UNREFERENCED_PARAMETER(size);
1910 HeapFree(
m_heap, 0, ptr);
1921 ::new ((
void*)ptr) _Ty(val);
1932 ::new ((
void*)ptr) _Ty(std::forward<_Ty>(val));
1975 if (!ActivateActCtx(hActCtx, &
m_cookie))
2012 m_cookie = hToken && ImpersonateLoggedOnUser(hToken);
2059 SetConsoleCtrlHandler(
m_handler, FALSE);
2100 m_proc(std::move(h.m_proc)),
2113 VirtualFreeEx(
m_proc,
m_h, 0, MEM_RELEASE);
2123 if (
this != std::addressof(other)) {
2125 m_proc = std::move(other.m_proc);
2156 _In_ HANDLE hProcess,
2157 _In_opt_ LPVOID lpAddress,
2159 _In_ DWORD flAllocationType,
2160 _In_ DWORD flProtect)
noexcept
2162 handle_type h = VirtualAllocEx(hProcess, lpAddress, dwSize, flAllocationType, flProtect);
2178 VirtualFreeEx(
m_proc,
m_h, 0, MEM_RELEASE);
2213 __declspec(deprecated(
"Use RegCreateKeyEx - mind it returns error number rather than SetLastError"))
2216 _In_z_ LPCTSTR lpSubKey,
2217 _In_opt_ LPTSTR lpClass,
2218 _In_ DWORD dwOptions,
2219 _In_ REGSAM samDesired,
2220 _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL,
2221 _Out_opt_ LPDWORD lpdwDisposition = NULL) noexcept
2224 const LSTATUS s = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, &h, lpdwDisposition);
2225 if (s == ERROR_SUCCESS) {
2243 __declspec(deprecated(
"Use RegOpenKeyEx - mind it returns error number rather than SetLastError"))
2246 _In_opt_z_ LPCTSTR lpSubKey,
2247 _In_ DWORD ulOptions,
2248 _In_ REGSAM samDesired) noexcept
2251 const LSTATUS s = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &h);
2252 if (s == ERROR_SUCCESS) {
2270 bool delete_subkey(_In_z_ LPCTSTR szSubkey)
2274 s = RegDeleteKey(
m_h, szSubkey);
2275 if (s == ERROR_SUCCESS || s == ERROR_FILE_NOT_FOUND)
2281 s = RegOpenKeyEx(
m_h, szSubkey, 0, KEY_ENUMERATE_SUB_KEYS, &h);
2282 if (s == ERROR_SUCCESS)
2289 TCHAR szName[MAX_PATH];
2290 DWORD dwSize = _countof(szName);
2291 s = RegEnumKeyEx(k, 0, szName, &dwSize, NULL, NULL, NULL, NULL);
2292 if (s == ERROR_SUCCESS)
2293 k.delete_subkey(szName);
2294 else if (s == ERROR_NO_MORE_ITEMS)
2303 s = RegDeleteKey(
m_h, szSubkey);
2304 if (s == ERROR_SUCCESS)
2369 hProcess = INVALID_HANDLE_VALUE;
2370 hThread = INVALID_HANDLE_VALUE;
2380 #pragma warning(push)
2381 #pragma warning(disable: 6001)
2383 if (hProcess != INVALID_HANDLE_VALUE)
2384 CloseHandle(hProcess);
2386 if (hThread != INVALID_HANDLE_VALUE)
2387 CloseHandle(hThread);
2389 #pragma warning(pop)
2422 bool open(_In_z_ LPCTSTR lpUNCServerName, _In_z_ LPCTSTR lpSourceName) noexcept
2424 handle_type h = RegisterEventSource(lpUNCServerName, lpSourceName);
2440 DeregisterEventSource(
m_h);
2450#pragma warning(push)
2451#pragma warning(disable: 4505)
2454static LSTATUS RegCreateKeyExA(
2456 _In_ LPCSTR lpSubKey,
2457 _Reserved_ DWORD Reserved,
2458 _In_opt_ LPSTR lpClass,
2459 _In_ DWORD dwOptions,
2460 _In_ REGSAM samDesired,
2461 _In_opt_ CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes,
2463 _Out_opt_ LPDWORD lpdwDisposition)
2466 LSTATUS s = RegCreateKeyExA(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, &h, lpdwDisposition);
2467 if (s == ERROR_SUCCESS)
2477static LSTATUS RegCreateKeyExW(
2479 _In_ LPCWSTR lpSubKey,
2480 _Reserved_ DWORD Reserved,
2481 _In_opt_ LPWSTR lpClass,
2482 _In_ DWORD dwOptions,
2483 _In_ REGSAM samDesired,
2484 _In_opt_ CONST LPSECURITY_ATTRIBUTES lpSecurityAttributes,
2486 _Out_opt_ LPDWORD lpdwDisposition)
2489 LSTATUS s = RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, &h, lpdwDisposition);
2490 if (s == ERROR_SUCCESS)
2496static LSTATUS RegOpenKeyExA(
2498 _In_opt_ LPCSTR lpSubKey,
2499 _In_opt_ DWORD ulOptions,
2500 _In_ REGSAM samDesired,
2504 LSTATUS s = RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, &h);
2505 if (s == ERROR_SUCCESS)
2519static LSTATUS RegOpenKeyExW(
2521 _In_opt_ LPCWSTR lpSubKey,
2522 _In_opt_ DWORD ulOptions,
2523 _In_ REGSAM samDesired,
2527 LSTATUS s = RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired, &h);
2528 if (s == ERROR_SUCCESS)
Activates given activation context in constructor and deactivates it in destructor.
Definition: Win.h:1961
actctx_activator(HANDLE hActCtx) noexcept
Construct the activator and activates the given activation context.
Definition: Win.h:1973
virtual ~actctx_activator()
Deactivates activation context and destructs the activator.
Definition: Win.h:1984
ULONG_PTR m_cookie
Cookie for context deactivation.
Definition: Win.h:1991
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1114
Console control handler stack management.
Definition: Win.h:2034
console_ctrl_handler(PHANDLER_ROUTINE HandlerRoutine) noexcept
Construct the console control handler object and pushes the given handler to the console control hand...
Definition: Win.h:2046
virtual ~console_ctrl_handler()
Pops console control handler from the console control handler stack.
Definition: Win.h:2056
PHANDLER_ROUTINE m_handler
Pointer to console control handler.
Definition: Win.h:2064
BOOL m_cookie
Did pushing the console control handler succeed?
Definition: Win.h:2063
Critical section wrapper.
Definition: Win.h:1650
CRITICAL_SECTION m_data
Critical section struct.
Definition: Win.h:1690
virtual ~critical_section()
Releases all resources used by an unowned critical section object.
Definition: Win.h:1674
critical_section()
Construct the object and initializes a critical section object.
Definition: Win.h:1660
Event log handle wrapper.
Definition: Win.h:2397
void free_internal() noexcept override
Closes an event log handle.
Definition: Win.h:2438
virtual ~event_log()
Closes an event log handle.
Definition: Win.h:2406
__declspec(deprecated("Use RegisterEventSource")) bool open(LPCTSTR lpUNCServerName
Retrieves a registered handle to the specified event log.
Event handle wrapper.
Definition: Win.h:1601
__declspec(deprecated("Use CreateEvent")) bool create(BOOL bManualReset
Creates or opens a named or unnamed event object.
File mapping.
Definition: Win.h:1513
__declspec(deprecated("Use CreateFileMapping")) bool create(HANDLE hFile
Creates or opens a named or unnamed file mapping object for a specified file.
File handle wrapper.
Definition: Win.h:1484
__declspec(deprecated("Use CreateFile")) bool create(LPCTSTR lpFileName
Opens file handle.
Find-file handle wrapper.
Definition: Win.h:1697
__declspec(deprecated("Use FindFirstFile")) bool find(LPCTSTR lpFileName
Searches a directory for a file or subdirectory with a name that matches a specific name (or partial ...
virtual ~find_file()
Closes a file search handle.
Definition: Win.h:1706
void free_internal() noexcept override
Closes a file search handle.
Definition: Win.h:1738
Base abstract template class to support generic object handle keeping.
Definition: Common.h:603
virtual void free_internal() noexcept=0
Abstract member function that must be implemented by child classes to do the actual object destructio...
HMODULE handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:608
handle_type m_h
Object handle.
Definition: Common.h:854
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:817
HeapAlloc allocator.
Definition: Win.h:1846
SIZE_T size_type
An unsigned integral type that can represent the length of any sequence that an object of template cl...
Definition: Win.h:1855
_Ty value_type
A type that is managed by the allocator.
Definition: Win.h:1848
heap_allocator(const heap_allocator< _Other > &other)
Constructs allocator from another type.
Definition: Win.h:1883
HANDLE m_heap
Heap handle.
Definition: Win.h:1954
pointer allocate(size_type count)
Allocates a new memory block.
Definition: Win.h:1894
ptrdiff_t difference_type
A signed integral type that can represent the difference between values of pointers to the type of ob...
Definition: Win.h:1856
heap_allocator(HANDLE heap)
Constructs allocator.
Definition: Win.h:1873
_Ty & reference
A type that provides a reference to the type of object managed by the allocator.
Definition: Win.h:1851
void construct(pointer ptr, _Ty &&val)
Calls moving constructor for the element.
Definition: Win.h:1930
void deallocate(pointer ptr, size_type size)
Frees memory block.
Definition: Win.h:1906
size_type max_size() const
Returns maximum memory block size.
Definition: Win.h:1948
void construct(pointer ptr, const _Ty &val)
Calls copying constructor for the element.
Definition: Win.h:1919
const _Ty & const_reference
A type that provides a constant reference to type of object managed by the allocator.
Definition: Win.h:1853
const _Ty * const_pointer
A type that provides a constant pointer to the type of object managed by the allocator.
Definition: Win.h:1852
_Ty * pointer
A type that provides a pointer to the type of object managed by the allocator.
Definition: Win.h:1850
void destroy(pointer ptr)
Calls destructor for the element.
Definition: Win.h:1940
Heap handle wrapper.
Definition: Win.h:1748
bool enumerate() noexcept
Enumerates allocated heap blocks using OutputDebugString()
Definition: Win.h:1790
__declspec(deprecated("Use HeapCreate")) bool create(DWORD flOptions
Creates the heap.
void free_internal() noexcept override
Destroys the heap.
Definition: Win.h:1834
virtual ~heap()
Destroys the heap.
Definition: Win.h:1757
Module handle wrapper.
Definition: Win.h:1390
void free_internal() noexcept override
Frees the module.
Definition: Win.h:1431
__declspec(deprecated("Use LoadLibraryEx")) bool load(LPCTSTR lpFileName
Loads the specified module into the address space of the calling process.
virtual ~library()
Frees the module.
Definition: Win.h:1399
Process handle wrapper.
Definition: Win.h:1441
__declspec(deprecated("Use OpenProcess")) bool open(DWORD dwDesiredAccess
Opens process handle.
Registry wrapper class.
Definition: Win.h:2189
__declspec(deprecated("Use RegCreateKeyEx - mind it returns error number rather than SetLastError")) bool create(HKEY hKey
Creates the specified registry key. If the key already exists, the function opens it.
virtual ~reg_key()
Closes a handle to the registry key.
Definition: Win.h:2198
SID wrapper class.
Definition: Win.h:2328
void free_internal() noexcept override
Closes a handle to the SID.
Definition: Win.h:2349
virtual ~security_id()
Closes a handle to the SID.
Definition: Win.h:2337
Lets the calling thread impersonate the security context of a logged-on user.
Definition: Win.h:1998
BOOL m_cookie
Did impersonation succeed?
Definition: Win.h:2027
user_impersonator(HANDLE hToken) noexcept
Construct the impersonator and impersonates the given user.
Definition: Win.h:2010
virtual ~user_impersonator()
Reverts to current user and destructs the impersonator.
Definition: Win.h:2020
Memory in virtual address space of a process handle wrapper.
Definition: Win.h:2071
vmemory & operator=(vmemory &&other) noexcept
Move assignment.
Definition: Win.h:2121
bool alloc(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect) noexcept
Reserves, commits, or changes the state of a region of memory within the virtual address space of a s...
Definition: Win.h:2155
void free_internal() noexcept override
Frees the memory.
Definition: Win.h:2176
void attach(HANDLE proc, handle_type h) noexcept
Sets a new memory handle for the class.
Definition: Win.h:2138
virtual ~vmemory()
Frees the memory.
Definition: Win.h:2110
vmemory(handle_type h, HANDLE proc) noexcept
Initializes a new class instance with an already available object handle.
Definition: Win.h:2088
vmemory() noexcept
Initializes a new class instance with the memory handle set to INVAL.
Definition: Win.h:2078
vmemory(vmemory &&h) noexcept
Move constructor.
Definition: Win.h:2099
HANDLE m_proc
Handle of memory's process.
Definition: Win.h:2182
Windows HANDLE wrapper class.
Definition: Win.h:1359
void free_internal() noexcept override
Closes an open object handle.
Definition: Win.h:1380
virtual ~win_handle()
Closes an open object handle.
Definition: Win.h:1368
Windows runtime error.
Definition: Common.h:1047
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:52
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:79
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:60
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:161
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:613
win_handle< INVALID_HANDLE_VALUE > process_snapshot
Process snapshot handle wrapper.
Definition: Win.h:1478
#define WINSTD_WINHANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Win.h:25
win_handle< NULL > thread
Thread handle wrapper.
Definition: Win.h:1471
UnmapViewOfFile_delete()
Default construct.
Definition: Win.h:1575
void operator()(_Other *) const
Delete a pointer of another type.
Definition: Win.h:1590
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Win.h:1580
UnmapViewOfFile_delete< _Ty > _Myt
This type.
Definition: Win.h:1570
Deleter for unique_ptr using UnmapViewOfFile.
Definition: Win.h:1542
UnmapViewOfFile_delete(const UnmapViewOfFile_delete< _Ty2 > &)
Construct from another UnmapViewOfFile_delete.
Definition: Win.h:1553
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: Win.h:1558
UnmapViewOfFile_delete< _Ty > _Myt
This type.
Definition: Win.h:1543
UnmapViewOfFile_delete()
Default construct.
Definition: Win.h:1548
A structure that enables an allocator for objects of one type to allocate storage for objects of anot...
Definition: Win.h:1863
heap_allocator< _Other > other
Other allocator type.
Definition: Win.h:1864