From 6e3388997c505666c9b302630edd5dc89ab17828 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 21 Apr 2015 11:10:31 +0000 Subject: [PATCH] GetUserNameEx() wrapper added. --- atl.vcxproj | 1 + atl.vcxproj.filters | 3 ++ atlsec.h | 84 +++++++++++++++++++++++++++++++++++++++++++++ atlshlwapi.h | 4 +-- stdafx.h | 8 +++++ 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 atlsec.h diff --git a/atl.vcxproj b/atl.vcxproj index a8d6020..a6bf798 100644 --- a/atl.vcxproj +++ b/atl.vcxproj @@ -107,6 +107,7 @@ + diff --git a/atl.vcxproj.filters b/atl.vcxproj.filters index 98c6f2d..f305a55 100644 --- a/atl.vcxproj.filters +++ b/atl.vcxproj.filters @@ -40,5 +40,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/atlsec.h b/atlsec.h new file mode 100644 index 0000000..78a1e9d --- /dev/null +++ b/atlsec.h @@ -0,0 +1,84 @@ +/* + 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 . +*/ + +#include + + +BOOLEAN GetUserNameExA(__in EXTENDED_NAME_FORMAT NameFormat, __out ATL::CAtlStringA &sName) +{ + ULONG ulSize = 0; + + // Query the final string length first. + if (!::GetUserNameExA(NameFormat, NULL, &ulSize)) { + if (::GetLastError() == ERROR_MORE_DATA) { + // Prepare the buffer and retry. + LPSTR szBuffer = sName.GetBuffer(ulSize - 1); + if (!szBuffer) { + SetLastError(ERROR_OUTOFMEMORY); + return FALSE; + } + if (::GetUserNameExA(NameFormat, szBuffer, &ulSize)) { + sName.ReleaseBuffer(ulSize); + return TRUE; + } else { + sName.ReleaseBuffer(0); + return FALSE; + } + } else { + // Return error. + return FALSE; + } + } else { + // The result is empty. + sName.Empty(); + return NO_ERROR; + } +} + + +BOOLEAN GetUserNameExW(__in EXTENDED_NAME_FORMAT NameFormat, __out ATL::CAtlStringW &sName) +{ + ULONG ulSize = 0; + + // Query the final string length first. + if (!::GetUserNameExW(NameFormat, NULL, &ulSize)) { + if (::GetLastError() == ERROR_MORE_DATA) { + // Prepare the buffer and retry. + LPWSTR szBuffer = sName.GetBuffer(ulSize - 1); + if (!szBuffer) { + SetLastError(ERROR_OUTOFMEMORY); + return FALSE; + } + if (::GetUserNameExW(NameFormat, szBuffer, &ulSize)) { + sName.ReleaseBuffer(ulSize); + return TRUE; + } else { + sName.ReleaseBuffer(0); + return FALSE; + } + } else { + // Return error. + return FALSE; + } + } else { + // The result is empty. + sName.Empty(); + return NO_ERROR; + } +} diff --git a/atlshlwapi.h b/atlshlwapi.h index 76f9035..4b4dcfa 100644 --- a/atlshlwapi.h +++ b/atlshlwapi.h @@ -32,7 +32,7 @@ inline BOOL PathCanonicalizeA(__out ATL::CAtlStringA &sValue, __in LPCSTR pszPat return FALSE; } BOOL bResult = ::PathCanonicalizeA(szBuffer, pszPath); - sValue.ReleaseBuffer(bResult ? strnlen(szBuffer, MAX_PATH) : 0); + sValue.ReleaseBuffer(bResult ? (int)strnlen(szBuffer, MAX_PATH) : 0); sValue.FreeExtra(); return bResult; } @@ -47,7 +47,7 @@ inline BOOL PathCanonicalizeW(__out ATL::CAtlStringW &sValue, __in LPCWSTR pszPa return FALSE; } BOOL bResult = ::PathCanonicalizeW(szBuffer, pszPath); - sValue.ReleaseBuffer(bResult ? wcsnlen(szBuffer, MAX_PATH) : 0); + sValue.ReleaseBuffer(bResult ? (int)wcsnlen(szBuffer, MAX_PATH) : 0); sValue.FreeExtra(); return bResult; } diff --git a/stdafx.h b/stdafx.h index f2ab7f7..99c70cc 100644 --- a/stdafx.h +++ b/stdafx.h @@ -22,4 +22,12 @@ #include "atlwin.h" #include "atlcrypt.h" +#include "atleap.h" +#include "atlex.h" #include "atlmsi.h" +#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL) || defined(SECURITY_MAC) +#include "atlsec.h" +#endif +#include "atlshlwapi.h" +#include "atlwin.h" +#include "atlwlan.h"