e37402b675a2406fa73251df47ad75005df1574d
[reactos.git] / reactos / sdk / lib / 3rdparty / atlex / atlsec.h
1 /*
2 Copyright 1991-2017 Amebis
3
4 This file is part of atlex.
5
6 atlex is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 atlex is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with atlex. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #pragma once
21
22 #include <Security.h>
23
24 ///
25 /// \defgroup ATLSecurityAPI Security API
26 /// Integrates ATL classes with Microsoft Security API
27 ///
28 /// @{
29
30 ///
31 /// Retrieves the name of the user or other security principal associated with the calling thread and stores it in a ATL::CAtlStringA string.
32 ///
33 /// \sa [GetUserNameEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx)
34 ///
35 BOOLEAN GetUserNameExA(_In_ EXTENDED_NAME_FORMAT NameFormat, _Out_ ATL::CAtlStringA &sName)
36 {
37 CHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(CHAR)];
38 ULONG ulSize = _countof(szStackBuffer);
39
40 // Try with stack buffer first.
41 if (::GetUserNameExA(NameFormat, szStackBuffer, &ulSize)) {
42 // Allocate buffer on heap, copy from stack buffer, and zero terminate.
43 LPSTR szBuffer = sName.GetBuffer(ulSize);
44 if (!szBuffer) {
45 SetLastError(ERROR_OUTOFMEMORY);
46 return FALSE;
47 }
48 memcpy(szBuffer, szStackBuffer, ulSize);
49 sName.ReleaseBuffer(ulSize);
50 return TRUE;
51 } else {
52 if (::GetLastError() == ERROR_MORE_DATA) {
53 // Allocate buffer on heap and retry.
54 LPSTR szBuffer = sName.GetBuffer(ulSize - 1);
55 if (!szBuffer) {
56 SetLastError(ERROR_OUTOFMEMORY);
57 return FALSE;
58 }
59 if (::GetUserNameExA(NameFormat, szBuffer, &ulSize)) {
60 sName.ReleaseBuffer(ulSize);
61 return TRUE;
62 } else {
63 sName.ReleaseBuffer(0);
64 return FALSE;
65 }
66 } else {
67 // Return error.
68 return FALSE;
69 }
70 }
71 }
72
73
74 ///
75 /// Retrieves the name of the user or other security principal associated with the calling thread and stores it in a ATL::CAtlStringW string.
76 ///
77 /// \sa [GetUserNameEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx)
78 ///
79 BOOLEAN GetUserNameExW(_In_ EXTENDED_NAME_FORMAT NameFormat, _Out_ ATL::CAtlStringW &sName)
80 {
81 WCHAR szStackBuffer[ATL_STACK_BUFFER_BYTES/sizeof(WCHAR)];
82 ULONG ulSize = _countof(szStackBuffer);
83
84 // Try with stack buffer first.
85 if (::GetUserNameExW(NameFormat, szStackBuffer, &ulSize)) {
86 // Allocate buffer on heap, copy from stack buffer, and zero terminate.
87 LPWSTR szBuffer = sName.GetBuffer(ulSize);
88 if (!szBuffer) {
89 SetLastError(ERROR_OUTOFMEMORY);
90 return FALSE;
91 }
92 wmemcpy(szBuffer, szStackBuffer, ulSize);
93 sName.ReleaseBuffer(ulSize);
94 return TRUE;
95 } else {
96 if (::GetLastError() == ERROR_MORE_DATA) {
97 // Allocate buffer on heap and retry.
98 LPWSTR szBuffer = sName.GetBuffer(ulSize - 1);
99 if (!szBuffer) {
100 SetLastError(ERROR_OUTOFMEMORY);
101 return FALSE;
102 }
103 if (::GetUserNameExW(NameFormat, szBuffer, &ulSize)) {
104 sName.ReleaseBuffer(ulSize);
105 return TRUE;
106 } else {
107 sName.ReleaseBuffer(0);
108 return FALSE;
109 }
110 } else {
111 // Return error.
112 return FALSE;
113 }
114 }
115 }
116
117 /// @}