From f95b10d5e3020941b7fdf3d6dc50fca36c4c9b7c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 24 Mar 2015 14:10:49 +0000 Subject: [PATCH] Additional functions added. --- atl.vcxproj | 1 + atl.vcxproj.filters | 3 ++ atlwin.h | 116 ++++++++++++++++++++++++++++++++++++++++++++ atlwlan.h | 51 +++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 atlwlan.h diff --git a/atl.vcxproj b/atl.vcxproj index 90dd211..6036d3c 100644 --- a/atl.vcxproj +++ b/atl.vcxproj @@ -106,6 +106,7 @@ + diff --git a/atl.vcxproj.filters b/atl.vcxproj.filters index 83d005e..0f178db 100644 --- a/atl.vcxproj.filters +++ b/atl.vcxproj.filters @@ -28,5 +28,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/atlwin.h b/atlwin.h index 2c6d877..383c3f0 100644 --- a/atlwin.h +++ b/atlwin.h @@ -63,3 +63,119 @@ inline int GetWindowTextW(__in HWND hWnd, __out ATL::CAtlStringW &sValue) return 0; } } + + +inline BOOL RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCSTR pszName, _Inout_ ATL::CAtlStringA &sValue) +{ + DWORD dwSize = 0; + DWORD dwType; + + // Determine the type and size first. + if (::RegQueryValueExA(hReg, pszName, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS) { + if (dwType == REG_SZ || dwType == REG_MULTI_SZ) { + // The value is REG_SZ or REG_MULTI_SZ. Read it now. + LPSTR szTemp = sValue.GetBuffer(dwSize / sizeof(TCHAR)); + if (::RegQueryValueExA(hReg, pszName, NULL, NULL, (LPBYTE)szTemp, &dwSize) == ERROR_SUCCESS) { + sValue.ReleaseBuffer(); + return TRUE; + } else { + // Reading of the value failed. + sValue.ReleaseBuffer(0); + return FALSE; + } + } else if (dwType == REG_EXPAND_SZ) { + // The value is REG_EXPAND_SZ. Read it and expand environment variables. + LPSTR szTemp = (LPSTR)::LocalAlloc(LMEM_FIXED, dwSize); + if (!szTemp) AtlThrow(E_OUTOFMEMORY); + if (::RegQueryValueExA(hReg, pszName, NULL, NULL, (LPBYTE)szTemp, &dwSize) == ERROR_SUCCESS) { + // The value was read successfully. Now, expand the environment variables. + DWORD cCharFinal = dwSize / sizeof(TCHAR) + 0x100; // Initial estimate + + for (;;) { + DWORD cCharEx = cCharFinal; + LPSTR szTempEx = sValue.GetBuffer(cCharEx); + cCharFinal = ::ExpandEnvironmentStringsA(szTemp, szTempEx, cCharEx); + if (cCharFinal > cCharEx) { + // The buffer was to small. Repeat with a bigger one. + sValue.ReleaseBuffer(0); + } else { + // The buffer was sufficient. Break. + sValue.ReleaseBuffer(); + break; + } + } + + ::LocalFree(szTemp); + return TRUE; + } else { + // Reading of the value failed. + ::LocalFree(szTemp); + return FALSE; + } + } else { + // The value is not a string type. + return FALSE; + } + } else { + // The value with given name doesn't exist in this key. + return FALSE; + } +} + + +inline BOOL RegQueryStringValue(_In_ HKEY hReg, _In_z_ LPCWSTR pszName, _Inout_ ATL::CAtlStringW &sValue) +{ + DWORD dwSize = 0; + DWORD dwType; + + // Determine the type and size first. + if (::RegQueryValueExW(hReg, pszName, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS) { + if (dwType == REG_SZ || dwType == REG_MULTI_SZ) { + // The value is REG_SZ or REG_MULTI_SZ. Read it now. + LPWSTR szTemp = sValue.GetBuffer(dwSize / sizeof(TCHAR)); + if (::RegQueryValueExW(hReg, pszName, NULL, NULL, (LPBYTE)szTemp, &dwSize) == ERROR_SUCCESS) { + sValue.ReleaseBuffer(); + return TRUE; + } else { + // Reading of the value failed. + sValue.ReleaseBuffer(0); + return FALSE; + } + } else if (dwType == REG_EXPAND_SZ) { + // The value is REG_EXPAND_SZ. Read it and expand environment variables. + LPWSTR szTemp = (LPWSTR)::LocalAlloc(LMEM_FIXED, dwSize); + if (!szTemp) AtlThrow(E_OUTOFMEMORY); + if (::RegQueryValueExW(hReg, pszName, NULL, NULL, (LPBYTE)szTemp, &dwSize) == ERROR_SUCCESS) { + // The value was read successfully. Now, expand the environment variables. + DWORD cCharFinal = dwSize / sizeof(TCHAR) + 0x100; // Initial estimate + + for (;;) { + DWORD cCharEx = cCharFinal; + LPWSTR szTempEx = sValue.GetBuffer(cCharEx); + cCharFinal = ::ExpandEnvironmentStringsW(szTemp, szTempEx, cCharEx); + if (cCharFinal > cCharEx) { + // The buffer was to small. Repeat with a bigger one. + sValue.ReleaseBuffer(0); + } else { + // The buffer was sufficient. Break. + sValue.ReleaseBuffer(); + break; + } + } + + ::LocalFree(szTemp); + return TRUE; + } else { + // Reading of the value failed. + ::LocalFree(szTemp); + return FALSE; + } + } else { + // The value is not a string type. + return FALSE; + } + } else { + // The value with given name doesn't exist in this key. + return FALSE; + } +} diff --git a/atlwlan.h b/atlwlan.h new file mode 100644 index 0000000..fc72776 --- /dev/null +++ b/atlwlan.h @@ -0,0 +1,51 @@ +/* + Copyright 1991-2015 Amebis + + This file is part of libatl. + + Setup is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Setup is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Setup. If not, see . +*/ + +#pragma once + +#include +#include + + +inline DWORD WlanReasonCodeToString(__in DWORD dwReasonCode, __out ATL::CAtlStringW &sValue, __reserved PVOID pReserved) +{ + DWORD dwSize = 0; + + for (;;) { + // Increment size and allocate buffer. + LPWSTR szBuffer = sValue.GetBuffer(dwSize += 1024); + if (!szBuffer) return ERROR_OUTOFMEMORY; + + // Try! + DWORD dwResult = ::WlanReasonCodeToString(dwReasonCode, dwSize, szBuffer, pReserved); + if (dwResult == NO_ERROR) { + DWORD dwLength = (DWORD)wcsnlen(szBuffer, dwSize); + sValue.ReleaseBuffer(dwLength); + if (dwLength < dwSize - 1) { + // Buffer was long enough to get entire string. + sValue.FreeExtra(); + return NO_ERROR; + } + } else { + // Return error code. + sValue.ReleaseBuffer(0); + return dwResult; + } + } +}