From 5b39dc76f1bc82f941d5c800ab9f34407a06b53a Mon Sep 17 00:00:00 2001 From: Igor Pavlov <87184205+ip7z@users.noreply.github.com> Date: Wed, 21 Jun 2023 00:00:00 +0000 Subject: 23.01 --- CPP/Windows/SecurityUtils.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'CPP/Windows/SecurityUtils.cpp') diff --git a/CPP/Windows/SecurityUtils.cpp b/CPP/Windows/SecurityUtils.cpp index ede83fa..d4282d0 100644 --- a/CPP/Windows/SecurityUtils.cpp +++ b/CPP/Windows/SecurityUtils.cpp @@ -4,9 +4,6 @@ #include "SecurityUtils.h" -#define MY_CAST_FUNC (void(*)()) -// #define MY_CAST_FUNC - namespace NWindows { namespace NSecurity { @@ -35,7 +32,7 @@ bool MyLookupAccountSid(LPCTSTR systemName, PSID sid, static void SetLsaString(LPWSTR src, PLSA_UNICODE_STRING dest) { - size_t len = (size_t)wcslen(src); + const size_t len = (size_t)wcslen(src); dest->Length = (USHORT)(len * sizeof(WCHAR)); dest->MaximumLength = (USHORT)((len + 1) * sizeof(WCHAR)); dest->Buffer = src; @@ -72,13 +69,14 @@ typedef BOOL (WINAPI * Func_LookupAccountNameW)( static PSID GetSid(LPWSTR accountName) { #ifndef _UNICODE - HMODULE hModule = GetModuleHandle(TEXT("Advapi32.dll")); - if (hModule == NULL) + const HMODULE hModule = GetModuleHandle(TEXT("advapi32.dll")); + if (!hModule) return NULL; - Func_LookupAccountNameW lookupAccountNameW = (Func_LookupAccountNameW) - MY_CAST_FUNC - GetProcAddress(hModule, "LookupAccountNameW"); - if (lookupAccountNameW == NULL) + const + Func_LookupAccountNameW lookupAccountNameW = Z7_GET_PROC_ADDRESS( + Func_LookupAccountNameW, hModule, + "LookupAccountNameW"); + if (!lookupAccountNameW) return NULL; #endif @@ -88,21 +86,21 @@ static PSID GetSid(LPWSTR accountName) #ifdef _UNICODE ::LookupAccountNameW #else - lookupAccountNameW + lookupAccountNameW #endif - (NULL, accountName, NULL, &sidLen, NULL, &domainLen, &sidNameUse)) + (NULL, accountName, NULL, &sidLen, NULL, &domainLen, &sidNameUse)) { if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - PSID pSid = ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen); + const PSID pSid = ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen); LPWSTR domainName = (LPWSTR)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (domainLen + 1) * sizeof(WCHAR)); - BOOL res = + const BOOL res = #ifdef _UNICODE ::LookupAccountNameW #else - lookupAccountNameW + lookupAccountNameW #endif - (NULL, accountName, pSid, &sidLen, domainName, &domainLen, &sidNameUse); + (NULL, accountName, pSid, &sidLen, domainName, &domainLen, &sidNameUse); ::HeapFree(GetProcessHeap(), 0, domainName); if (res) return pSid; @@ -111,7 +109,7 @@ static PSID GetSid(LPWSTR accountName) return NULL; } -#define MY__SE_LOCK_MEMORY_NAME L"SeLockMemoryPrivilege" +#define Z7_WIN_SE_LOCK_MEMORY_NAME L"SeLockMemoryPrivilege" bool AddLockMemoryPrivilege() { @@ -131,13 +129,13 @@ bool AddLockMemoryPrivilege() != 0) return false; LSA_UNICODE_STRING userRights; - wchar_t s[128] = MY__SE_LOCK_MEMORY_NAME; + wchar_t s[128] = Z7_WIN_SE_LOCK_MEMORY_NAME; SetLsaString(s, &userRights); WCHAR userName[256 + 2]; DWORD size = 256; if (!GetUserNameW(userName, &size)) return false; - PSID psid = GetSid(userName); + const PSID psid = GetSid(userName); if (psid == NULL) return false; bool res = false; @@ -176,7 +174,7 @@ bool AddLockMemoryPrivilege() res = true; } */ - NTSTATUS status = policy.AddAccountRights(psid, &userRights); + const NTSTATUS status = policy.AddAccountRights(psid, &userRights); if (status == 0) res = true; // ULONG res = LsaNtStatusToWinError(status); -- cgit v1.2.3-55-g6feb