14#pragma warning(disable: 4505)
23template<
class _Traits,
class _Ax>
24static DWORD GetModuleFileNameA(_In_opt_ HMODULE hModule, _Out_ std::basic_string<char, _Traits, _Ax> &sValue)
noexcept
31 DWORD dwResult = ::GetModuleFileNameA(hModule, szStackBuffer, _countof(szStackBuffer));
32 if (dwResult < _countof(szStackBuffer)) {
34 sValue.assign(szStackBuffer, dwResult);
39 std::unique_ptr<char[]> szBuffer(
new char[dwCapacity]);
40 dwResult = ::GetModuleFileNameA(hModule, szBuffer.get(), dwCapacity);
41 if (dwResult < dwCapacity) {
42 sValue.assign(szBuffer.get(), dwResult);
54template<
class _Traits,
class _Ax>
55static DWORD GetModuleFileNameW(_In_opt_ HMODULE hModule, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sValue)
noexcept
60 DWORD dwResult = ::GetModuleFileNameW(hModule, szStackBuffer, _countof(szStackBuffer));
61 if (dwResult < _countof(szStackBuffer)) {
63 sValue.assign(szStackBuffer, dwResult);
68 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[dwCapacity]);
69 dwResult = ::GetModuleFileNameW(hModule, szBuffer.get(), dwCapacity);
70 if (dwResult < dwCapacity) {
71 sValue.assign(szBuffer.get(), dwResult);
79template<
class _Traits,
class _Ax>
80static _Success_(
return != 0) int GetWindowTextA(_In_ HWND hWnd, _Out_ std::basic_string<
char, _Traits, _Ax> &sValue) noexcept
87 iResult = ::GetWindowTextLengthA(hWnd);
92 iResult = ::GetWindowTextA(hWnd, szBuffer, _countof(szBuffer));
93 sValue.assign(szBuffer, iResult);
96 std::unique_ptr<char[]> szBuffer(
new char[++iResult]);
97 iResult = ::GetWindowTextA(hWnd, szBuffer.get(), iResult);
98 sValue.assign(szBuffer.get(), iResult);
112template<
class _Traits,
class _Ax>
113static _Success_(
return != 0) int GetWindowTextW(_In_ HWND hWnd, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sValue) noexcept
120 iResult = ::GetWindowTextLengthW(hWnd);
125 iResult = ::GetWindowTextW(hWnd, szBuffer, _countof(szBuffer));
126 sValue.assign(szBuffer, iResult);
129 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[++iResult]);
130 iResult = ::GetWindowTextW(hWnd, szBuffer.get(), iResult);
131 sValue.assign(szBuffer.get(), iResult);
141template<
class _Ty,
class _Ax>
142static _Success_(
return != 0) BOOL GetFileVersionInfoA(_In_z_ LPCSTR lptstrFilename, __reserved DWORD dwHandle, _Out_ std::vector<_Ty, _Ax> &aValue) noexcept
147 DWORD dwVerInfoSize = ::GetFileVersionInfoSizeA(lptstrFilename, &dwHandle);
148 if (dwVerInfoSize != 0) {
150 aValue.resize((dwVerInfoSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
151 return ::GetFileVersionInfoA(lptstrFilename, dwHandle, dwVerInfoSize, aValue.data());
161template<
class _Ty,
class _Ax>
162static _Success_(
return != 0) BOOL GetFileVersionInfoW(_In_z_ LPCWSTR lptstrFilename, __reserved DWORD dwHandle, _Out_ std::vector<_Ty, _Ax> &aValue) noexcept
167 DWORD dwVerInfoSize = ::GetFileVersionInfoSizeW(lptstrFilename, &dwHandle);
168 if (dwVerInfoSize != 0) {
170 aValue.resize((dwVerInfoSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
171 return ::GetFileVersionInfoW(lptstrFilename, dwHandle, dwVerInfoSize, aValue.data());
177template<
class _Traits,
class _Ax>
178static _Success_(
return != 0) DWORD ExpandEnvironmentStringsA(_In_z_ LPCSTR lpSrc, _Out_ std::basic_string<
char, _Traits, _Ax> &sValue) noexcept
182 for (DWORD dwSizeOut = (DWORD)strlen(lpSrc) + 0x100;;) {
183 DWORD dwSizeIn = dwSizeOut;
184 std::unique_ptr<char[]> szBuffer(
new char[(
size_t)dwSizeIn + 2]);
185 dwSizeOut = ::ExpandEnvironmentStringsA(lpSrc, szBuffer.get(), dwSizeIn);
186 if (dwSizeOut == 0) {
189 }
else if (dwSizeOut <= dwSizeIn) {
191 sValue.assign(szBuffer.get(), dwSizeOut - 1);
205template<
class _Traits,
class _Ax>
206static _Success_(
return != 0) DWORD ExpandEnvironmentStringsW(_In_z_ LPCWSTR lpSrc, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sValue) noexcept
208 for (DWORD dwSizeOut = (DWORD)wcslen(lpSrc) + 0x100;;) {
209 DWORD dwSizeIn = dwSizeOut;
210 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[(
size_t)dwSizeIn + 1]);
211 dwSizeOut = ::ExpandEnvironmentStringsW(lpSrc, szBuffer.get(), dwSizeIn);
212 if (dwSizeOut == 0) {
215 }
else if (dwSizeOut <= dwSizeIn) {
217 sValue.assign(szBuffer.get(), dwSizeOut - 1);
227template<
class _Traits,
class _Ax>
228static VOID GuidToStringA(_In_ LPCGUID lpGuid, _Out_ std::basic_string<char, _Traits, _Ax> &str)
noexcept
232 sprintf(str,
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
236 lpGuid->Data4[0], lpGuid->Data4[1],
237 lpGuid->Data4[2], lpGuid->Data4[3], lpGuid->Data4[4], lpGuid->Data4[5], lpGuid->Data4[6], lpGuid->Data4[7]);
246template<
class _Traits,
class _Ax>
247static VOID GuidToStringW(_In_ LPCGUID lpGuid, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &str)
noexcept
251 sprintf(str, L
"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
255 lpGuid->Data4[0], lpGuid->Data4[1],
256 lpGuid->Data4[2], lpGuid->Data4[3], lpGuid->Data4[4], lpGuid->Data4[5], lpGuid->Data4[6], lpGuid->Data4[7]);
261#define GuidToString GuidToStringW
263#define GuidToString GuidToStringA
267static _Success_(
return) BOOL StringToGuidA(_In_z_ LPCSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCSTR *lpszGuidEnd = NULL)
noexcept
272 unsigned long long ullTmp;
274 if (!lpszGuid || !lpGuid || *lpszGuid !=
'{')
return FALSE;
277 g.Data1 = strtoul(lpszGuid, &lpszEnd, 16);
278 if (errno == ERANGE)
return FALSE;
281 if (*lpszGuid !=
'-')
return FALSE;
284 ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
285 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
286 g.Data2 =
static_cast<unsigned short>(ulTmp);
289 if (*lpszGuid !=
'-')
return FALSE;
292 ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
293 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
294 g.Data3 =
static_cast<unsigned short>(ulTmp);
297 if (*lpszGuid !=
'-')
return FALSE;
300 ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
301 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
302 g.Data4[0] =
static_cast<unsigned char>((ulTmp >> 8) & 0xff);
303 g.Data4[1] =
static_cast<unsigned char>( ulTmp & 0xff);
306 if (*lpszGuid !=
'-')
return FALSE;
309 ullTmp = _strtoui64(lpszGuid, &lpszEnd, 16);
310 if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF)
return FALSE;
311 g.Data4[2] =
static_cast<unsigned char>((ullTmp >> 40) & 0xff);
312 g.Data4[3] =
static_cast<unsigned char>((ullTmp >> 32) & 0xff);
313 g.Data4[4] =
static_cast<unsigned char>((ullTmp >> 24) & 0xff);
314 g.Data4[5] =
static_cast<unsigned char>((ullTmp >> 16) & 0xff);
315 g.Data4[6] =
static_cast<unsigned char>((ullTmp >> 8) & 0xff);
316 g.Data4[7] =
static_cast<unsigned char>( ullTmp & 0xff);
319 if (*lpszGuid !=
'}')
return FALSE;
323 *lpszGuidEnd = lpszGuid;
340static _Success_(
return) BOOL StringToGuidW(_In_z_ LPCWSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCWSTR *lpszGuidEnd = NULL)
noexcept
345 unsigned long long ullTmp;
347 if (!lpszGuid || !lpGuid || *lpszGuid !=
'{')
return FALSE;
350 g.Data1 = wcstoul(lpszGuid, &lpszEnd, 16);
351 if (errno == ERANGE)
return FALSE;
354 if (*lpszGuid !=
'-')
return FALSE;
357 ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
358 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
359 g.Data2 =
static_cast<unsigned short>(ulTmp);
362 if (*lpszGuid !=
'-')
return FALSE;
365 ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
366 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
367 g.Data3 =
static_cast<unsigned short>(ulTmp);
370 if (*lpszGuid !=
'-')
return FALSE;
373 ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
374 if (errno == ERANGE || ulTmp > 0xFFFF)
return FALSE;
375 g.Data4[0] =
static_cast<unsigned char>((ulTmp >> 8) & 0xff);
376 g.Data4[1] =
static_cast<unsigned char>( ulTmp & 0xff);
379 if (*lpszGuid !=
'-')
return FALSE;
382 ullTmp = _wcstoui64(lpszGuid, &lpszEnd, 16);
383 if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF)
return FALSE;
384 g.Data4[2] =
static_cast<unsigned char>((ullTmp >> 40) & 0xff);
385 g.Data4[3] =
static_cast<unsigned char>((ullTmp >> 32) & 0xff);
386 g.Data4[4] =
static_cast<unsigned char>((ullTmp >> 24) & 0xff);
387 g.Data4[5] =
static_cast<unsigned char>((ullTmp >> 16) & 0xff);
388 g.Data4[6] =
static_cast<unsigned char>((ullTmp >> 8) & 0xff);
389 g.Data4[7] =
static_cast<unsigned char>( ullTmp & 0xff);
392 if (*lpszGuid !=
'}')
return FALSE;
396 *lpszGuidEnd = lpszGuid;
404#define StringToGuid StringToGuidW
406#define StringToGuid StringToGuidA
427template<
class _Traits,
class _Ax>
428static LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Out_ std::basic_string<char, _Traits, _Ax> &sValue)
noexcept
432 DWORD dwSize =
sizeof(aStackBuffer), dwType;
435 lResult = ::RegQueryValueExA(hReg, pszName, NULL, &dwType, aStackBuffer, &dwSize);
436 if (lResult == ERROR_SUCCESS) {
437 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
439 dwSize /=
sizeof(CHAR);
440 sValue.assign(
reinterpret_cast<LPCSTR
>(aStackBuffer), dwSize &&
reinterpret_cast<LPCSTR
>(aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
441 }
else if (dwType == REG_EXPAND_SZ) {
443 if (::ExpandEnvironmentStringsA(
reinterpret_cast<LPCSTR
>(aStackBuffer), sValue) == 0)
444 lResult = ::GetLastError();
447 lResult = ERROR_INVALID_DATA;
449 }
else if (lResult == ERROR_MORE_DATA) {
450 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
452 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[dwSize /
sizeof(CHAR)]);
453 if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
454 dwSize /=
sizeof(CHAR);
455 sValue.assign(szBuffer.get(), dwSize && szBuffer[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
457 }
else if (dwType == REG_EXPAND_SZ) {
459 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[dwSize /
sizeof(CHAR)]);
460 if ((lResult = ::RegQueryValueExA(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
461 if (::ExpandEnvironmentStringsA(szBuffer.get(), sValue) == 0)
462 lResult = ::GetLastError();
466 lResult = ERROR_INVALID_DATA;
491template<
class _Traits,
class _Ax>
492static LSTATUS RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sValue)
noexcept
496 DWORD dwSize =
sizeof(aStackBuffer), dwType;
499 lResult = ::RegQueryValueExW(hReg, pszName, NULL, &dwType, aStackBuffer, &dwSize);
500 if (lResult == ERROR_SUCCESS) {
501 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
503 dwSize /=
sizeof(WCHAR);
504 sValue.assign(
reinterpret_cast<LPCWSTR
>(aStackBuffer), dwSize &&
reinterpret_cast<LPCWSTR
>(aStackBuffer)[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
505 }
else if (dwType == REG_EXPAND_SZ) {
507 if (::ExpandEnvironmentStringsW(
reinterpret_cast<LPCWSTR
>(aStackBuffer), sValue) == 0)
508 lResult = ::GetLastError();
511 lResult = ERROR_INVALID_DATA;
513 }
else if (lResult == ERROR_MORE_DATA) {
514 if (dwType == REG_SZ || dwType == REG_MULTI_SZ) {
516 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[dwSize /
sizeof(WCHAR)]);
517 if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
518 dwSize /=
sizeof(WCHAR);
519 sValue.assign(szBuffer.get(), dwSize && szBuffer[dwSize - 1] == 0 ? dwSize - 1 : dwSize);
521 }
else if (dwType == REG_EXPAND_SZ) {
523 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[dwSize /
sizeof(WCHAR)]);
524 if ((lResult = ::RegQueryValueExW(hReg, pszName, NULL, NULL,
reinterpret_cast<LPBYTE
>(szBuffer.get()), &dwSize)) == ERROR_SUCCESS) {
525 if (::ExpandEnvironmentStringsW(szBuffer.get(), sValue) == 0)
526 lResult = ::GetLastError();
530 lResult = ERROR_INVALID_DATA;
538template<
class _Ty,
class _Ax>
539static LSTATUS RegQueryValueExA(_In_ HKEY hKey, _In_opt_z_ LPCSTR lpValueName, __reserved LPDWORD lpReserved, _Out_opt_ LPDWORD lpType, _Out_ std::vector<_Ty, _Ax> &aData)
noexcept
543 DWORD dwSize =
sizeof(aStackBuffer);
546 lResult = RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, aStackBuffer, &dwSize);
547 if (lResult == ERROR_SUCCESS) {
549 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
550 memcpy(aData.data(), aStackBuffer, dwSize);
551 }
else if (lResult == ERROR_MORE_DATA) {
553 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
554 lResult = RegQueryValueExA(hKey, lpValueName, lpReserved, NULL, aData.data(), &dwSize);
565template<
class _Ty,
class _Ax>
566static LSTATUS RegQueryValueExW(_In_ HKEY hKey, _In_opt_z_ LPCWSTR lpValueName, __reserved LPDWORD lpReserved, _Out_opt_ LPDWORD lpType, _Out_ std::vector<_Ty, _Ax> &aData)
noexcept
570 DWORD dwSize =
sizeof(aStackBuffer);
573 lResult = RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, aStackBuffer, &dwSize);
574 if (lResult == ERROR_SUCCESS) {
576 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
577 memcpy(aData.data(), aStackBuffer, dwSize);
578 }
else if (lResult == ERROR_MORE_DATA) {
580 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
581 lResult = RegQueryValueExW(hKey, lpValueName, lpReserved, NULL, aData.data(), &dwSize);
587#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
590template<
class _Traits,
class _Ax>
591static 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
596 return ERROR_CALL_NOT_IMPLEMENTED;
604template<
class _Traits,
class _Ax>
605static 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
611 Flags &= ~REG_MUI_STRING_TRUNCATE;
614 lResult = RegLoadMUIStringW(hKey, pszValue, szStackBuffer,
sizeof(szStackBuffer), &dwSize, Flags, pszDirectory);
615 if (lResult == ERROR_SUCCESS) {
617 sOut.assign(szStackBuffer, wcsnlen(szStackBuffer, dwSize/
sizeof(
wchar_t)));
618 }
else if (lResult == ERROR_MORE_DATA) {
620 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[(dwSize +
sizeof(
wchar_t) - 1)/
sizeof(
wchar_t)]);
621 sOut.assign(szBuffer.get(), (lResult = RegLoadMUIStringW(hKey, pszValue, szBuffer.get(), dwSize, &dwSize, Flags, pszDirectory)) == ERROR_SUCCESS ? wcsnlen(szBuffer.get(), dwSize/
sizeof(
wchar_t)) : 0);
634template<
class _Traits,
class _Ax>
635static _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
640 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
643 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (
size_t)cch - 1);
644 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
646 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
647 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
648 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
649 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
661static _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
666 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
669 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
670 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
672 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
673 sMultiByteStr.resize(cch);
674 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
685template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
686static _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
691 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
694 sMultiByteStr.assign(szStackBuffer, cch);
695 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
697 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
698 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
699 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
700 sMultiByteStr.assign(szBuffer.get(), cch);
713template<
class _Traits,
class _Ax>
714static _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
719 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
722 sMultiByteStr.assign(szStackBuffer, cchWideChar != -1 ? strnlen(szStackBuffer, cch) : (
size_t)cch - 1);
723 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
725 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
726 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
727 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
728 sMultiByteStr.assign(szBuffer.get(), cchWideChar != -1 ? strnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
729 SecureZeroMemory(szBuffer.get(),
sizeof(CHAR)*cch);
732 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
745static _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
750 int cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
753 sMultiByteStr.assign(szStackBuffer, szStackBuffer + cch);
754 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
756 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
757 sMultiByteStr.resize(cch);
758 cch = ::WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, sMultiByteStr.data(), cch, lpDefaultChar, lpUsedDefaultChar);
761 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
773template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
774static _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
779 int cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, lpUsedDefaultChar);
782 sMultiByteStr.assign(szStackBuffer, cch);
783 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
785 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), NULL, 0, lpDefaultChar, lpUsedDefaultChar);
786 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
787 cch = ::WideCharToMultiByte(CodePage, dwFlags, sWideCharStr.c_str(), (
int)sWideCharStr.length(), szBuffer.get(), cch, lpDefaultChar, lpUsedDefaultChar);
788 sMultiByteStr.assign(szBuffer.get(), cch);
789 SecureZeroMemory(szBuffer.get(),
sizeof(CHAR)*cch);
792 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
802template<
class _Traits,
class _Ax>
803static _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
808 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
811 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
812 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
814 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
815 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
816 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
817 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
829static _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
834 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
837 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
838 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
840 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
841 sWideCharStr.resize(cch);
842 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
853template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
854static _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
859 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
862 sWideCharStr.assign(szStackBuffer, cch);
863 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
865 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
866 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
867 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szBuffer.get(), cch);
868 sWideCharStr.assign(szBuffer.get(), cch);
881template<
class _Traits,
class _Ax>
882static _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
887 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
890 sWideCharStr.assign(szStackBuffer, cbMultiByte != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
891 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
893 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
894 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
895 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szBuffer.get(), cch);
896 sWideCharStr.assign(szBuffer.get(), cbMultiByte != -1 ? wcsnlen(szBuffer.get(), cch) : (
size_t)cch - 1);
897 SecureZeroMemory(szBuffer.get(),
sizeof(WCHAR)*cch);
900 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
913static _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
918 int cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, szStackBuffer, _countof(szStackBuffer));
921 sWideCharStr.assign(szStackBuffer, szStackBuffer + cch);
922 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
924 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
925 sWideCharStr.resize(cch);
926 cch = ::MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, sWideCharStr.data(), cch);
929 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
941template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
942static _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
947 int cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szStackBuffer, _countof(szStackBuffer));
950 sWideCharStr.assign(szStackBuffer, cch);
951 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
953 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), NULL, 0);
954 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
955 cch = ::MultiByteToWideChar(CodePage, dwFlags, sMultiByteStr.c_str(), (
int)sMultiByteStr.length(), szBuffer.get(), cch);
956 sWideCharStr.assign(szBuffer.get(), cch);
957 SecureZeroMemory(szBuffer.get(),
sizeof(WCHAR)*cch);
960 SecureZeroMemory(szStackBuffer,
sizeof(szStackBuffer));
970template<
class _Traits,
class _Ax>
971static _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
976 int cch = ::NormalizeString(NormForm, lpSrcString, cwSrcLength, szStackBuffer, _countof(szStackBuffer));
979 sDstString.assign(szStackBuffer, cwSrcLength != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
981 switch (::GetLastError()) {
982 case ERROR_INSUFFICIENT_BUFFER:
983 for (
int i = 10; i--;) {
986 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
987 cch = ::NormalizeString(NormForm, lpSrcString, cwSrcLength, szBuffer.get(), cch);
989 sDstString.assign(szBuffer.get(), cwSrcLength != -1 ? wcsnlen(szStackBuffer, cch) : (
size_t)cch - 1);
992 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1013template<
class _Traits1,
class _Ax1,
class _Traits2,
class _Ax2>
1014static _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
1019 int cch = ::NormalizeString(NormForm, sSrcString.c_str(), (
int)sSrcString.length(), szStackBuffer, _countof(szStackBuffer));
1022 sDstString.assign(szStackBuffer, cch);
1024 switch (::GetLastError()) {
1025 case ERROR_INSUFFICIENT_BUFFER:
1026 for (
int i = 10; i--;) {
1029 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
1030 cch = ::NormalizeString(NormForm, sSrcString.c_str(), (
int)sSrcString.length(), szBuffer.get(), cch);
1032 sDstString.assign(szBuffer.get(), cch);
1035 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1052template<
class _Traits,
class _Ax>
1053static _Success_(
return != 0) int WINAPI LoadStringA(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_ std::basic_string<
char, _Traits, _Ax> &sBuffer) noexcept
1057 int i = LoadStringA(hInstance, uID,
reinterpret_cast<LPSTR
>(&pszStr), 0);
1059 sBuffer.assign(pszStr, i);
1070template<
class _Traits,
class _Ax>
1071static _Success_(
return != 0) int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_ std::basic_string<
wchar_t, _Traits, _Ax> &sBuffer) noexcept
1075 int i = LoadStringW(hInstance, uID,
reinterpret_cast<LPWSTR
>(&pszStr), 0);
1077 sBuffer.assign(pszStr, i);
1088static VOID OutputDebugStrV(_In_z_ LPCSTR lpOutputString, _In_ va_list arg)
noexcept
1091 try { vsprintf(str, lpOutputString, arg); }
catch (...) {
return; }
1092 OutputDebugStringA(str.c_str());
1100static VOID OutputDebugStrV(_In_z_ LPCWSTR lpOutputString, _In_ va_list arg)
noexcept
1103 try { vsprintf(str, lpOutputString, arg); }
catch (...) {
return; }
1104 OutputDebugStringW(str.c_str());
1112static VOID OutputDebugStr(_In_z_ LPCSTR lpOutputString, ...) noexcept
1115 va_start(arg, lpOutputString);
1116 OutputDebugStrV(lpOutputString, arg);
1125static VOID OutputDebugStr(_In_z_ LPCWSTR lpOutputString, ...) noexcept
1128 va_start(arg, lpOutputString);
1129 OutputDebugStrV(lpOutputString, arg);
1134template<
class _Traits,
class _Ax>
1135static _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
1137 int iResult = GetDateFormatA(Locale, dwFlags, lpDate, lpFormat, NULL, 0);
1140 std::unique_ptr<char[]> szBuffer(
new char[iResult]);
1141 iResult = GetDateFormatA(Locale, dwFlags, lpDate, lpFormat, szBuffer.get(), iResult);
1142 sDate.assign(szBuffer.get(), iResult ? iResult - 1 : 0);
1154template<
class _Traits,
class _Ax>
1155static _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
1157 int iResult = GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, NULL, 0);
1160 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[iResult]);
1161 iResult = GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, szBuffer.get(), iResult);
1162 sDate.assign(szBuffer.get(), iResult ? iResult - 1 : 0);
1170template<
class _Traits,
class _Ax>
1171static _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
1175 DWORD dwNameLen = 0, dwRefDomainLen = 0;
1177 if (LookupAccountSidA(lpSystemName, lpSid,
1179 NULL, &dwRefDomainLen,
1183 if (sName ) sName ->clear();
1184 if (sReferencedDomainName) sReferencedDomainName->clear();
1186 }
else if (GetLastError() == ERROR_MORE_DATA) {
1188 std::unique_ptr<char[]> bufName (
new char[dwNameLen ]);
1189 std::unique_ptr<char[]> bufRefDomain(
new char[dwRefDomainLen]);
1190 if (LookupAccountSidA(lpSystemName, lpSid,
1191 bufName .get(), &dwNameLen ,
1192 bufRefDomain.get(), &dwRefDomainLen,
1195 if (sName ) sName ->assign(bufName .get(), dwNameLen - 1);
1196 if (sReferencedDomainName) sReferencedDomainName->assign(bufRefDomain.get(), dwRefDomainLen - 1);
1209template<
class _Traits,
class _Ax>
1210static _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
1214 DWORD dwNameLen = 0, dwRefDomainLen = 0;
1216 if (LookupAccountSidW(lpSystemName, lpSid,
1218 NULL, &dwRefDomainLen,
1222 if (sName ) sName ->clear();
1223 if (sReferencedDomainName) sReferencedDomainName->clear();
1225 }
else if (GetLastError() == ERROR_MORE_DATA) {
1227 std::unique_ptr<wchar_t[]> bufName (
new wchar_t[dwNameLen ]);
1228 std::unique_ptr<wchar_t[]> bufRefDomain(
new wchar_t[dwRefDomainLen]);
1229 if (LookupAccountSidW(lpSystemName, lpSid,
1230 bufName .get(), &dwNameLen ,
1231 bufRefDomain.get(), &dwRefDomainLen,
1234 if (sName ) sName ->assign(bufName .get(), dwNameLen - 1);
1235 if (sReferencedDomainName) sReferencedDomainName->assign(bufRefDomain.get(), dwRefDomainLen - 1);
1249static _Success_(
return != 0) BOOL GetTokenInformation(_In_ HANDLE TokenHandle, _In_ TOKEN_INFORMATION_CLASS TokenInformationClass, _Out_ std::unique_ptr<_Ty> &TokenInformation) noexcept
1254 if (GetTokenInformation(TokenHandle, TokenInformationClass, szStackBuffer,
sizeof(szStackBuffer), &dwSize)) {
1256 TokenInformation.reset((_Ty*)(
new BYTE[dwSize /
sizeof(BYTE)]));
1257 if (!TokenInformation) {
1258 SetLastError(ERROR_OUTOFMEMORY);
1261 memcpy(TokenInformation.get(), szStackBuffer, dwSize);
1263 }
else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1265 TokenInformation.reset((_Ty*)(
new BYTE[dwSize /
sizeof(BYTE)]));
1266 if (!TokenInformation) {
1267 SetLastError(ERROR_OUTOFMEMORY);
1270 return GetTokenInformation(TokenHandle, TokenInformationClass, TokenInformation.get(), dwSize, &dwSize);
1280template<
class _Traits,
class _Ax>
1281static _Success_(
return != 0) BOOL QueryFullProcessImageNameA(_In_ HANDLE hProcess, _In_ DWORD dwFlags, _Inout_ std::basic_string<
char, _Traits, _Ax>& sExeName)
1284 DWORD dwSize = _countof(szStackBuffer);
1287 if (::QueryFullProcessImageNameA(hProcess, dwFlags, szStackBuffer, &dwSize)) {
1289 sExeName.assign(szStackBuffer, dwSize);
1292 for (DWORD dwCapacity = 2 *
WINSTD_STACK_BUFFER_BYTES /
sizeof(
char); GetLastError() == ERROR_INSUFFICIENT_BUFFER; dwCapacity *= 2) {
1294 std::unique_ptr<char[]> szBuffer(
new char[dwCapacity]);
1295 dwSize = dwCapacity;
1296 if (::QueryFullProcessImageNameA(hProcess, dwFlags, szBuffer.get(), &dwSize)) {
1297 sExeName.assign(szBuffer.get(), dwSize);
1309template<
class _Traits,
class _Ax>
1310static _Success_(
return != 0) BOOL QueryFullProcessImageNameW(_In_ HANDLE hProcess, _In_ DWORD dwFlags, _Inout_ std::basic_string<
wchar_t, _Traits, _Ax>& sExeName)
1313 DWORD dwSize = _countof(szStackBuffer);
1316 if (::QueryFullProcessImageNameW(hProcess, dwFlags, szStackBuffer, &dwSize)) {
1318 sExeName.assign(szStackBuffer, dwSize);
1321 for (DWORD dwCapacity = 2 *
WINSTD_STACK_BUFFER_BYTES /
sizeof(
wchar_t); GetLastError() == ERROR_INSUFFICIENT_BUFFER; dwCapacity *= 2) {
1323 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[dwCapacity]);
1324 dwSize = dwCapacity;
1325 if (::QueryFullProcessImageNameW(hProcess, dwFlags, szBuffer.get(), &dwSize)) {
1326 sExeName.assign(szBuffer.get(), dwSize);
1345 template<HANDLE INVALID>
1402 bool load(_In_z_ LPCTSTR lpFileName, __reserved
handle_type hFile, _In_ DWORD dwFlags)
noexcept
1404 handle_type h = LoadLibraryEx(lpFileName, hFile, dwFlags);
1439 bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwProcessId)
noexcept
1441 handle_type h = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
1465 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
1467 handle_type h = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
1491 bool create(_In_ BOOL bManualReset, _In_ BOOL bInitialState, _In_opt_ LPSECURITY_ATTRIBUTES lpEventAttributes = NULL, _In_opt_z_ LPCTSTR lpName = NULL)
noexcept
1493 handle_type h = CreateEvent(lpEventAttributes, bManualReset, bInitialState, lpName);
1510 bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_z_ LPCTSTR lpName)
noexcept
1512 handle_type h = OpenEvent(dwDesiredAccess, bInheritHandle, lpName);
1538 InitializeCriticalSection(&
m_data);
1539 } __except(EXCEPTION_EXECUTE_HANDLER) {
1540 throw std::runtime_error(
"InitializeCriticalSection failed");
1551 DeleteCriticalSection(&
m_data);
1559 operator LPCRITICAL_SECTION() noexcept
1596 bool find(_In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFileData)
noexcept
1598 handle_type h = FindFirstFile(lpFileName, lpFindFileData);
1646 bool create(_In_ DWORD flOptions, _In_ SIZE_T dwInitialSize, _In_ SIZE_T dwMaximumSize)
noexcept
1648 handle_type h = HeapCreate(flOptions, dwInitialSize, dwMaximumSize);
1672 PROCESS_HEAP_ENTRY e;
1674 while (HeapWalk(
m_h, &e) != FALSE) {
1675 if ((e.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
1677 _T(
"Allocated block%s%s\n")
1678 _T(
" Data portion begins at: %#p\n Size: %d bytes\n")
1679 _T(
" Overhead: %d bytes\n Region index: %d\n"),
1680 (e.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0 ?
tstring_printf(_T(
", movable with HANDLE %#p"), e.Block.hMem).c_str() : _T(
""),
1681 (e.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0 ? _T(
", DDESHARE") : _T(
""),
1691 const DWORD dwResult = GetLastError();
1692 if (dwResult != ERROR_NO_MORE_ITEMS)
1693 OutputDebugStr(_T(
"HeapWalk failed (error %u).\n"), dwResult);
1717 template <
class _Ty>
1734 template <
class _Other>
1755 template <
class _Other>
1781 UNREFERENCED_PARAMETER(size);
1783 HeapFree(
m_heap, 0, ptr);
1794 ::new ((
void*)ptr) _Ty(val);
1805 ::new ((
void*)ptr) _Ty(std::forward<_Ty>(val));
1848 if (!ActivateActCtx(hActCtx, &
m_cookie))
1885 m_cookie = hToken && ImpersonateLoggedOnUser(hToken);
1932 SetConsoleCtrlHandler(
m_handler, FALSE);
1973 m_proc(std::move(h.m_proc)),
1986 VirtualFreeEx(
m_proc,
m_h, 0, MEM_RELEASE);
1996 if (
this != std::addressof(other)) {
1998 m_proc = std::move(other.m_proc);
2029 _In_ HANDLE hProcess,
2030 _In_opt_ LPVOID lpAddress,
2032 _In_ DWORD flAllocationType,
2033 _In_ DWORD flProtect)
noexcept
2035 handle_type h = VirtualAllocEx(hProcess, lpAddress, dwSize, flAllocationType, flProtect);
2051 VirtualFreeEx(
m_proc,
m_h, 0, MEM_RELEASE);
2088 _In_z_ LPCTSTR lpSubKey,
2089 _In_opt_ LPTSTR lpClass,
2090 _In_ DWORD dwOptions,
2091 _In_ REGSAM samDesired,
2092 _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL,
2093 _Out_opt_ LPDWORD lpdwDisposition = NULL)
noexcept
2096 const LSTATUS s = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, &h, lpdwDisposition);
2097 if (s == ERROR_SUCCESS) {
2117 _In_opt_z_ LPCTSTR lpSubKey,
2118 _In_ DWORD ulOptions,
2119 _In_ REGSAM samDesired)
noexcept
2122 const LONG s = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &h);
2123 if (s == ERROR_SUCCESS) {
2145 s = RegDeleteKey(
m_h, szSubkey);
2146 if (s == ERROR_SUCCESS || s == ERROR_FILE_NOT_FOUND)
2151 if (!k.
open(
m_h, szSubkey, 0, KEY_ENUMERATE_SUB_KEYS))
2154 TCHAR szName[MAX_PATH];
2155 DWORD dwSize = _countof(szName);
2156 s = RegEnumKeyEx(k, 0, szName, &dwSize, NULL, NULL, NULL, NULL);
2157 if (s == ERROR_SUCCESS)
2159 else if (s == ERROR_NO_MORE_ITEMS)
2168 s = RegDeleteKey(
m_h, szSubkey);
2169 if (s == ERROR_SUCCESS)
2234 hProcess = INVALID_HANDLE_VALUE;
2235 hThread = INVALID_HANDLE_VALUE;
2245 #pragma warning(push)
2246 #pragma warning(disable: 6001)
2248 if (hProcess != INVALID_HANDLE_VALUE)
2249 CloseHandle(hProcess);
2251 if (hThread != INVALID_HANDLE_VALUE)
2252 CloseHandle(hThread);
2254 #pragma warning(pop)
Activates given activation context in constructor and deactivates it in destructor.
Definition: Win.h:1834
actctx_activator(_In_ HANDLE hActCtx) noexcept
Construct the activator and activates the given activation context.
Definition: Win.h:1846
virtual ~actctx_activator()
Deactivates activation context and destructs the activator.
Definition: Win.h:1857
ULONG_PTR m_cookie
Cookie for context deactivation.
Definition: Win.h:1864
Base template class to support string formatting using printf() style templates.
Definition: Common.h:1502
Console control handler stack management.
Definition: Win.h:1907
virtual ~console_ctrl_handler()
Pops console control handler from the console control handler stack.
Definition: Win.h:1929
PHANDLER_ROUTINE m_handler
Pointer to console control handler.
Definition: Win.h:1937
console_ctrl_handler(_In_opt_ PHANDLER_ROUTINE HandlerRoutine) noexcept
Construct the console control handler object and pushes the given handler to the console control hand...
Definition: Win.h:1919
BOOL m_cookie
Did pushing the console control handler succeed?
Definition: Win.h:1936
Critical section wrapper.
Definition: Win.h:1525
CRITICAL_SECTION m_data
Critical section struct.
Definition: Win.h:1565
virtual ~critical_section()
Releases all resources used by an unowned critical section object.
Definition: Win.h:1549
critical_section()
Construct the object and initializes a critical section object.
Definition: Win.h:1535
Event handle wrapper.
Definition: Win.h:1480
bool create(_In_ BOOL bManualReset, _In_ BOOL bInitialState, _In_opt_ LPSECURITY_ATTRIBUTES lpEventAttributes=NULL, _In_opt_z_ LPCTSTR lpName=NULL) noexcept
Creates or opens a named or unnamed event object.
Definition: Win.h:1491
bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_z_ LPCTSTR lpName) noexcept
Opens an existing named event object.
Definition: Win.h:1510
File handle wrapper.
Definition: Win.h:1454
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
Opens file handle.
Definition: Win.h:1465
Find-file handle wrapper.
Definition: Win.h:1572
virtual ~find_file()
Closes a file search handle.
Definition: Win.h:1581
void free_internal() noexcept override
Closes a file search handle.
Definition: Win.h:1612
bool find(_In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFileData) noexcept
Searches a directory for a file or subdirectory with a name that matches a specific name (or partial ...
Definition: Win.h:1596
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
HMODULE handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
handle_type m_h
Object handle.
Definition: Common.h:833
HeapAlloc allocator.
Definition: Win.h:1719
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:1728
_Ty value_type
A type that is managed by the allocator.
Definition: Win.h:1721
void deallocate(_In_ pointer ptr, _In_ size_type size)
Frees memory block.
Definition: Win.h:1779
void construct(_Inout_ pointer ptr, _In_ const _Ty &val)
Calls copying constructor for the element.
Definition: Win.h:1792
HANDLE m_heap
Heap handle.
Definition: Win.h:1827
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:1729
_Ty & reference
A type that provides a reference to the type of object managed by the allocator.
Definition: Win.h:1724
pointer allocate(_In_ size_type count)
Allocates a new memory block.
Definition: Win.h:1767
heap_allocator(_In_ const heap_allocator< _Other > &other)
Constructs allocator from another type.
Definition: Win.h:1756
size_type max_size() const
Returns maximum memory block size.
Definition: Win.h:1821
heap_allocator(_In_ HANDLE heap)
Constructs allocator.
Definition: Win.h:1746
void construct(_Inout_ pointer ptr, _Inout_ _Ty &&val)
Calls moving constructor for the element.
Definition: Win.h:1803
const _Ty & const_reference
A type that provides a constant reference to type of object managed by the allocator.
Definition: Win.h:1726
const _Ty * const_pointer
A type that provides a constant pointer to the type of object managed by the allocator.
Definition: Win.h:1725
_Ty * pointer
A type that provides a pointer to the type of object managed by the allocator.
Definition: Win.h:1723
void destroy(_Inout_ pointer ptr)
Calls destructor for the element.
Definition: Win.h:1813
Heap handle wrapper.
Definition: Win.h:1622
bool create(_In_ DWORD flOptions, _In_ SIZE_T dwInitialSize, _In_ SIZE_T dwMaximumSize) noexcept
Creates the heap.
Definition: Win.h:1646
bool enumerate() noexcept
Enumerates allocated heap blocks using OutputDebugString()
Definition: Win.h:1663
void free_internal() noexcept override
Destroys the heap.
Definition: Win.h:1707
virtual ~heap()
Destroys the heap.
Definition: Win.h:1631
Module handle wrapper.
Definition: Win.h:1378
void free_internal() noexcept override
Frees the module.
Definition: Win.h:1418
bool load(_In_z_ LPCTSTR lpFileName, __reserved handle_type hFile, _In_ DWORD dwFlags) noexcept
Loads the specified module into the address space of the calling process.
Definition: Win.h:1402
virtual ~library()
Frees the module.
Definition: Win.h:1387
Process handle wrapper.
Definition: Win.h:1428
bool open(_In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwProcessId) noexcept
Opens process handle.
Definition: Win.h:1439
Registry wrapper class.
Definition: Win.h:2062
bool open(_In_ HKEY hKey, _In_opt_z_ LPCTSTR lpSubKey, _In_ DWORD ulOptions, _In_ REGSAM samDesired) noexcept
Opens the specified registry key.
Definition: Win.h:2115
bool delete_subkey(_In_z_ LPCTSTR szSubkey)
Deletes the specified registry subkey.
Definition: Win.h:2141
void free_internal() noexcept override
Closes a handle to the registry key.
Definition: Win.h:2183
bool create(_In_ HKEY hKey, _In_z_ LPCTSTR lpSubKey, _In_opt_ LPTSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes=NULL, _Out_opt_ LPDWORD lpdwDisposition=NULL) noexcept
Creates the specified registry key. If the key already exists, the function opens it.
Definition: Win.h:2086
virtual ~reg_key()
Closes a handle to the registry key.
Definition: Win.h:2071
SID wrapper class.
Definition: Win.h:2193
void free_internal() noexcept override
Closes a handle to the SID.
Definition: Win.h:2214
virtual ~security_id()
Closes a handle to the SID.
Definition: Win.h:2202
Lets the calling thread impersonate the security context of a logged-on user.
Definition: Win.h:1871
BOOL m_cookie
Did impersonation succeed?
Definition: Win.h:1900
user_impersonator(_In_opt_ HANDLE hToken) noexcept
Construct the impersonator and impersonates the given user.
Definition: Win.h:1883
virtual ~user_impersonator()
Reverts to current user and destructs the impersonator.
Definition: Win.h:1893
Memory in virtual address space of a process handle wrapper.
Definition: Win.h:1944
vmemory(_Inout_ vmemory &&h) noexcept
Move constructor.
Definition: Win.h:1972
void attach(_In_ HANDLE proc, _In_opt_ handle_type h) noexcept
Sets a new memory handle for the class.
Definition: Win.h:2011
void free_internal() noexcept override
Frees the memory.
Definition: Win.h:2049
vmemory & operator=(_Inout_ vmemory &&other) noexcept
Move assignment.
Definition: Win.h:1994
bool alloc(_In_ HANDLE hProcess, _In_opt_ LPVOID lpAddress, _In_ SIZE_T dwSize, _In_ DWORD flAllocationType, _In_ 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:2028
virtual ~vmemory()
Frees the memory.
Definition: Win.h:1983
vmemory(_In_ handle_type h, _In_ HANDLE proc) noexcept
Initializes a new class instance with an already available object handle.
Definition: Win.h:1961
vmemory() noexcept
Initializes a new class instance with the memory handle set to INVAL.
Definition: Win.h:1951
HANDLE m_proc
Handle of memory's process.
Definition: Win.h:2055
Windows HANDLE wrapper class.
Definition: Win.h:1347
void free_internal() noexcept override
Closes an open object handle.
Definition: Win.h:1368
virtual ~win_handle()
Closes an open object handle.
Definition: Win.h:1356
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:53
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:61
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:138
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:590
A structure that enables an allocator for objects of one type to allocate storage for objects of anot...
Definition: Win.h:1736
heap_allocator< _Other > other
Other allocator type.
Definition: Win.h:1737