Additional functions added.

This commit is contained in:
Simon Rozman
2015-03-24 14:10:49 +00:00
parent 03bf4dda55
commit f95b10d5e3
4 changed files with 171 additions and 0 deletions

View File

@@ -106,6 +106,7 @@
<ClInclude Include="atlcrypt.h" /> <ClInclude Include="atlcrypt.h" />
<ClInclude Include="atlmsi.h" /> <ClInclude Include="atlmsi.h" />
<ClInclude Include="atlwin.h" /> <ClInclude Include="atlwin.h" />
<ClInclude Include="atlwlan.h" />
<ClInclude Include="stdafx.h" /> <ClInclude Include="stdafx.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -28,5 +28,8 @@
<ClInclude Include="atlcrypt.h"> <ClInclude Include="atlcrypt.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="atlwlan.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

116
atlwin.h
View File

@@ -63,3 +63,119 @@ inline int GetWindowTextW(__in HWND hWnd, __out ATL::CAtlStringW &sValue)
return 0; 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;
}
}

51
atlwlan.h Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <atlstr.h>
#include <wlanapi.h>
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;
}
}
}