-
Notifications
You must be signed in to change notification settings - Fork 1
/
System.cpp
624 lines (570 loc) · 18.4 KB
/
System.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the LICENSE file distributed with
* this work for additional information regarding copyright ownership.
*
* Also, if exists, check the Licenses directory for information about
* third-party modules.
*
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "System.h"
#include "FileRoutines.h"
#include "Sid.h"
#include <Windows.h>
#include <lm.h>
//-----------------------------------------------------------
#define X_WCHAR_ENC(_x,_y) (WCHAR)(((USHORT)(_x)) ^ ((USHORT)_y+0x8C32))
#define _EXPAND_W(str) \
i = 0; do \
{ szTempW[i] = X_WCHAR_ENC(str[i], i); } \
while (szTempW[i++] != 0)
//-----------------------------------------------------------
typedef NET_API_STATUS (WINAPI *lpfnNetUserEnum)(_In_opt_ LPCWSTR servername, _In_ DWORD level, _In_ DWORD filter,
_Outptr_result_buffer_(_Inexpressible_("varies")) LPBYTE *bufptr,
_In_ DWORD prefmaxlen, _Out_ LPDWORD entriesread,
_Out_ LPDWORD totalentries, _Inout_opt_ PDWORD resume_handle);
typedef NET_API_STATUS (WINAPI *lpfnNetLocalGroupEnum)(_In_opt_ LPCWSTR servername, _In_ DWORD level,
_Outptr_result_buffer_(_Inexpressible_("varies")) LPBYTE *bufptr,
_In_ DWORD prefmaxlen, _Out_ LPDWORD entriesread,
_Out_ LPDWORD totalentries, _Inout_opt_ PDWORD_PTR resumehandle);
typedef NET_API_STATUS (WINAPI *lpfnNetApiBufferFree)(_Frees_ptr_opt_ LPVOID Buffer);
static lpfnNetUserEnum fnNetUserEnum = NULL;
static lpfnNetLocalGroupEnum fnNetLocalGroupEnum = NULL;
static lpfnNetApiBufferFree fnNetApiBufferFree = NULL;
//-----------------------------------------------------------
static VOID InitializeApis();
//-----------------------------------------------------------
namespace MX {
namespace System {
HRESULT GetOpSystemInfo(_Out_ CStringW &cStrOpSystemW)
{
static const WCHAR strW_Windows[] = {
X_WCHAR_ENC(L'W', 0), X_WCHAR_ENC(L'i', 1), X_WCHAR_ENC(L'n', 2), X_WCHAR_ENC(L'd', 3),
X_WCHAR_ENC(L'o', 4), X_WCHAR_ENC(L'w', 5), X_WCHAR_ENC(L's', 6), X_WCHAR_ENC(0, 7)
};
static const WCHAR strW_Windows2000[] = {
X_WCHAR_ENC(L'2', 0), X_WCHAR_ENC(L'0', 1), X_WCHAR_ENC(L'0', 2), X_WCHAR_ENC(L'0', 3),
X_WCHAR_ENC(0, 4)
};
static const WCHAR strW_WindowsXP[] = {
X_WCHAR_ENC(L'X', 0), X_WCHAR_ENC(L'P', 1), X_WCHAR_ENC(0, 2)
};
static const WCHAR strW_WindowsServer2003[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'0', 9), X_WCHAR_ENC(L'3', 10), X_WCHAR_ENC(0, 11)
};
static const WCHAR strW_WindowsServer2003R2[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'0', 9), X_WCHAR_ENC(L'3', 10), X_WCHAR_ENC(L' ', 11),
X_WCHAR_ENC(L'R', 12), X_WCHAR_ENC(L'2', 13), X_WCHAR_ENC(0, 14)
};
static const WCHAR strW_WindowsVista[] = {
X_WCHAR_ENC(L'V', 0), X_WCHAR_ENC(L'i', 1), X_WCHAR_ENC(L's', 2), X_WCHAR_ENC(L't', 3),
X_WCHAR_ENC(L'a', 4), X_WCHAR_ENC(0, 5)
};
static const WCHAR strW_WindowsServer2008[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'0', 9), X_WCHAR_ENC(L'8', 10), X_WCHAR_ENC(0, 11)
};
static const WCHAR strW_WindowsServer2008R2[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'0', 9), X_WCHAR_ENC(L'8', 10), X_WCHAR_ENC(L' ', 11),
X_WCHAR_ENC(L'R', 12), X_WCHAR_ENC(L'2', 13), X_WCHAR_ENC(0, 14)
};
static const WCHAR strW_Windows7[] = {
X_WCHAR_ENC(L'7', 0), X_WCHAR_ENC(0, 1)
};
static const WCHAR strW_WindowsServer2012[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'1', 9), X_WCHAR_ENC(L'2', 10), X_WCHAR_ENC(0, 11)
};
static const WCHAR strW_Windows8[] = {
X_WCHAR_ENC(L'8', 0), X_WCHAR_ENC(0, 1)
};
static const WCHAR strW_WindowsServer2012R2[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'1', 9), X_WCHAR_ENC(L'2', 10), X_WCHAR_ENC(L' ', 11),
X_WCHAR_ENC(L'R', 12), X_WCHAR_ENC(L'2', 13), X_WCHAR_ENC(0, 14)
};
static const WCHAR strW_Windows8_1[] = {
X_WCHAR_ENC(L'8', 0), X_WCHAR_ENC(L'.', 1), X_WCHAR_ENC(L'1', 2), X_WCHAR_ENC(0, 3)
};
static const WCHAR strW_Windows10[] = {
X_WCHAR_ENC(L'1', 0), X_WCHAR_ENC(L'0', 1), X_WCHAR_ENC(0, 2)
};
static const WCHAR strW_WindowsServer2016[] = {
X_WCHAR_ENC(L'S', 0), X_WCHAR_ENC(L'e', 1), X_WCHAR_ENC(L'r', 2), X_WCHAR_ENC(L'v', 3),
X_WCHAR_ENC(L'e', 4), X_WCHAR_ENC(L'r', 5), X_WCHAR_ENC(L' ', 6), X_WCHAR_ENC(L'2', 7),
X_WCHAR_ENC(L'0', 8), X_WCHAR_ENC(L'1', 9), X_WCHAR_ENC(L'6', 10), X_WCHAR_ENC(0, 11)
};
static const WCHAR strW_Unknown[] = {
X_WCHAR_ENC(L'?', 0), X_WCHAR_ENC(0, 1)
};
RTL_OSVERSIONINFOEXW sOviExW;
WCHAR szTempW[128];
SIZE_T i;
NTSTATUS nNtStatus;
::MxMemSet(&sOviExW, 0, sizeof(sOviExW));
sOviExW.dwOSVersionInfoSize = (DWORD)sizeof(sOviExW);
nNtStatus = ::MxRtlGetVersion((PRTL_OSVERSIONINFOW)&sOviExW);
if (!NT_SUCCESS(nNtStatus))
{
cStrOpSystemW.Empty();
return MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
}
if (sOviExW.dwPlatformId != VER_PLATFORM_WIN32_NT)
{
cStrOpSystemW.Empty();
return E_NOTIMPL;
}
_EXPAND_W(strW_Windows);
if (cStrOpSystemW.Copy(szTempW) == FALSE)
return E_OUTOFMEMORY;
_EXPAND_W(strW_Unknown);
if (sOviExW.dwMajorVersion == 5)
{
if (sOviExW.dwMinorVersion == 0)
{
_EXPAND_W(strW_Windows2000);
}
else if (sOviExW.dwMinorVersion == 1)
{
_EXPAND_W(strW_WindowsXP);
}
else if (sOviExW.dwMinorVersion == 2)
{
if (::GetSystemMetrics(SM_SERVERR2) == 0)
{
_EXPAND_W(strW_WindowsServer2003);
}
else
{
_EXPAND_W(strW_WindowsServer2003R2);
}
}
}
else if (sOviExW.dwMajorVersion == 6)
{
if (sOviExW.dwMinorVersion == 0)
{
if (sOviExW.wProductType == VER_NT_WORKSTATION)
{
_EXPAND_W(strW_WindowsVista);
}
else
{
_EXPAND_W(strW_WindowsServer2008);
}
}
else if (sOviExW.dwMinorVersion == 1)
{
if (sOviExW.wProductType == VER_NT_WORKSTATION)
{
_EXPAND_W(strW_Windows7);
}
else
{
_EXPAND_W(strW_WindowsServer2008R2);
}
}
else if (sOviExW.dwMinorVersion == 2)
{
if (sOviExW.wProductType == VER_NT_WORKSTATION)
{
_EXPAND_W(strW_Windows8);
}
else
{
_EXPAND_W(strW_WindowsServer2012);
}
}
else if (sOviExW.dwMinorVersion == 3)
{
if (sOviExW.wProductType == VER_NT_WORKSTATION)
{
_EXPAND_W(strW_Windows8_1);
}
else
{
_EXPAND_W(strW_WindowsServer2012R2);
}
}
else if (sOviExW.dwMinorVersion == 4)
{
if (sOviExW.wProductType == VER_NT_WORKSTATION)
{
_EXPAND_W(strW_Windows10);
}
else
{
_EXPAND_W(strW_WindowsServer2016);
}
}
}
else if (sOviExW.dwMajorVersion == 10)
{
if (sOviExW.wProductType == VER_NT_WORKSTATION)
{
_EXPAND_W(strW_Windows10);
}
else
{
_EXPAND_W(strW_WindowsServer2016);
}
}
if (cStrOpSystemW.Concat(L" ") == FALSE ||
cStrOpSystemW.Concat(szTempW) == FALSE)
{
return E_OUTOFMEMORY;
}
//done
return S_OK;
}
HRESULT _GetComputerNameEx(_In_ COMPUTER_NAME_FORMAT NameType, _Out_ CStringW &cStrNameW)
{
DWORD dwBufLen;
HRESULT hRes;
for (dwBufLen = 128; dwBufLen <= 65536; dwBufLen <<= 1)
{
if (cStrNameW.EnsureBuffer((SIZE_T)dwBufLen + 1) == FALSE)
{
hRes = E_OUTOFMEMORY;
break;
}
if (::GetComputerNameExW(NameType, (LPWSTR)cStrNameW, &dwBufLen) != FALSE)
{
hRes = S_OK;
((LPWSTR)cStrNameW)[dwBufLen] = 0;
if (dwBufLen == 0)
goto try_other_method;
break;
}
hRes = MX_HRESULT_FROM_LASTERROR();
if (hRes != MX_E_MoreData)
break;
}
if (dwBufLen > 65536)
hRes = MX_E_BufferOverflow;
if (FAILED(hRes) && hRes != E_OUTOFMEMORY)
{
try_other_method:
if (NameType == ComputerNameDnsDomain || NameType == ComputerNameDnsHostname)
{
if (cStrNameW.EnsureBuffer(MAX_COMPUTERNAME_LENGTH + 2) != FALSE)
{
dwBufLen = MAX_COMPUTERNAME_LENGTH + 1;
if (::GetComputerNameW((LPWSTR)cStrNameW, &dwBufLen) != FALSE)
{
((LPWSTR)cStrNameW)[dwBufLen] = 0;
hRes = S_OK;
}
else
{
hRes = MX_HRESULT_FROM_LASTERROR();
}
}
else
{
hRes = E_OUTOFMEMORY;
}
}
}
//done
if (SUCCEEDED(hRes))
cStrNameW.Refresh();
else
cStrNameW.Empty();
return hRes;
}
HRESULT LoadAppDll(_In_z_ LPCWSTR szLibraryNameW, _Out_ HINSTANCE *lphInst)
{
CStringW cStrFileNameW;
HRESULT hRes;
*lphInst = NULL;
hRes = FileRoutines::GetAppFolderPath(cStrFileNameW);
if (SUCCEEDED(hRes))
{
if (cStrFileNameW.Concat(szLibraryNameW) != FALSE)
{
HINSTANCE hDll = ::LoadLibraryW((LPCWSTR)cStrFileNameW);
if (hDll != NULL)
*lphInst = hDll;
else
hRes = MX_HRESULT_FROM_LASTERROR();
}
else
{
hRes = E_OUTOFMEMORY;
}
}
//done
return hRes;
}
HRESULT LoadSystem32Dll(_In_z_ LPCWSTR szLibraryNameW, _Out_ HINSTANCE *lphInst)
{
CStringW cStrFileNameW;
HRESULT hRes;
*lphInst = NULL;
hRes = FileRoutines::GetWindowsSystemPath(cStrFileNameW);
if (SUCCEEDED(hRes))
{
if (cStrFileNameW.Concat(szLibraryNameW) != FALSE)
{
HINSTANCE hDll = ::LoadLibraryW((LPCWSTR)cStrFileNameW);
if (hDll != NULL)
*lphInst = hDll;
else
hRes = MX_HRESULT_FROM_LASTERROR();
}
else
{
hRes = E_OUTOFMEMORY;
}
}
//done
return hRes;
}
VOID RegisterAppInRestartManager()
{
typedef HRESULT (WINAPI *lpfnRegisterApplicationRestart)(_In_opt_ PCWSTR pwzCommandline, _In_ DWORD dwFlags);
lpfnRegisterApplicationRestart fnRegisterApplicationRestart;
HINSTANCE hKernel32Dll;
hKernel32Dll = ::GetModuleHandleW(L"kernel32.dll");
if (hKernel32Dll != NULL)
{
fnRegisterApplicationRestart = (lpfnRegisterApplicationRestart)::GetProcAddress(hKernel32Dll,
"RegisterApplicationRestart");
if (fnRegisterApplicationRestart != NULL)
{
fnRegisterApplicationRestart(L"/restartmanager", RESTART_NO_CRASH | RESTART_NO_HANG |
RESTART_NO_REBOOT);
}
}
return;
}
HRESULT GetAllUsers(_Inout_ TArrayListWithFree<LPWSTR> &aUsersList)
{
static const LPCWSTR aBuiltinUsersW[] = { L"S-1-5-18", L"S-1-5-19", L"S-1-5-20" };
LPUSER_INFO_0 lpUserInfo0 = NULL;
DWORD i, dwEntries, dwTotalEntries, dwResumeHandle;
NET_API_STATUS nStatus;
CSid cSid;
CStringW cStrTempW;
HRESULT hRes, hRes2;
aUsersList.RemoveAllElements();
InitializeApis();
if (fnNetUserEnum == NULL)
return MX_E_ProcNotFound;
for (i=0; i<(DWORD)MX_ARRAYLEN(aBuiltinUsersW); i++)
{
hRes = cSid.Set(aBuiltinUsersW[i]);
if (SUCCEEDED(hRes))
{
hRes = cSid.GetAccountName(cStrTempW);
if (SUCCEEDED(hRes))
{
if (cStrTempW.InsertN(L"/", 0, 1) == FALSE ||
cStrTempW.Insert(aBuiltinUsersW[i], 0) == FALSE ||
cStrTempW.InsertN((cSid.IsAnyWellKnownSid() != FALSE) ? L"+" : L"-", 0, 1) == FALSE)
{
hRes = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hRes))
{
if (aUsersList.AddElement((LPWSTR)cStrTempW) != FALSE)
cStrTempW.Detach();
else
hRes = E_OUTOFMEMORY;
}
}
if (FAILED(hRes))
goto done;
}
dwEntries = dwTotalEntries = dwResumeHandle = 0;
do
{
lpUserInfo0 = NULL;
nStatus = fnNetUserEnum(NULL, 0, FILTER_NORMAL_ACCOUNT, (LPBYTE*)&lpUserInfo0, 16384, &dwEntries, &dwTotalEntries,
&dwResumeHandle);
hRes = HRESULT_FROM_WIN32(nStatus);
if (nStatus == NERR_Success || nStatus == ERROR_MORE_DATA)
{
for (i = 0; i < dwEntries; i++)
{
cStrTempW.Empty();
hRes2 = cSid.Set(lpUserInfo0[i].usri0_name);
if (SUCCEEDED(hRes2))
hRes2 = cSid.GetStringSid(cStrTempW);
if (SUCCEEDED(hRes2))
{
if (cStrTempW.ConcatN(L"/", 1) == FALSE ||
cStrTempW.Concat(lpUserInfo0[i].usri0_name) == FALSE ||
cStrTempW.InsertN((cSid.IsAnyWellKnownSid() != FALSE) ? L"+" : L"-", 0, 1) == FALSE)
{
hRes2 = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hRes2))
{
if (aUsersList.AddElement((LPWSTR)cStrTempW) != FALSE)
cStrTempW.Detach();
else
hRes2 = E_OUTOFMEMORY;
}
if (FAILED(hRes2))
{
hRes = hRes2;
break;
}
}
}
if (lpUserInfo0 != NULL)
fnNetApiBufferFree(lpUserInfo0);
}
while (hRes == HRESULT_FROM_WIN32(ERROR_MORE_DATA));
done:
//done
if (FAILED(hRes))
aUsersList.RemoveAllElements();
return hRes;
}
HRESULT GetAllGroups(_Inout_ TArrayListWithFree<LPWSTR> &aGroupsList)
{
static const LPCWSTR aBuiltinUsersW[] = { L"S-1-1-0" };
LPLOCALGROUP_INFO_0 lpGroupInfo0 = NULL;
DWORD i, dwEntries, dwTotalEntries;
DWORD_PTR dwResumeHandle;
NET_API_STATUS nStatus;
CSid cSid;
CStringW cStrTempW;
HRESULT hRes, hRes2;
aGroupsList.RemoveAllElements();
InitializeApis();
if (fnNetLocalGroupEnum == NULL)
return MX_E_ProcNotFound;
for (i=0; i<(DWORD)MX_ARRAYLEN(aBuiltinUsersW); i++)
{
hRes = cSid.Set(aBuiltinUsersW[i]);
if (SUCCEEDED(hRes))
{
hRes = cSid.GetAccountName(cStrTempW);
if (SUCCEEDED(hRes))
{
if (cStrTempW.InsertN(L"/", 0, 1) == FALSE ||
cStrTempW.Insert(aBuiltinUsersW[i], 0) == FALSE ||
cStrTempW.InsertN((cSid.IsAnyWellKnownSid() != FALSE) ? L"+" : L"-", 0, 1) == FALSE)
{
hRes = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hRes))
{
if (aGroupsList.AddElement((LPWSTR)cStrTempW) != FALSE)
cStrTempW.Detach();
else
hRes = E_OUTOFMEMORY;
}
}
if (FAILED(hRes))
goto done;
}
dwEntries = dwTotalEntries = 0;
dwResumeHandle = 0;
do
{
lpGroupInfo0 = NULL;
nStatus = fnNetLocalGroupEnum(NULL, 0, (LPBYTE*)&lpGroupInfo0, 16384, &dwEntries, &dwTotalEntries,
&dwResumeHandle);
hRes = HRESULT_FROM_WIN32(nStatus);
if (nStatus == NERR_Success || nStatus == ERROR_MORE_DATA)
{
for (i = 0; i < dwEntries; i++)
{
cStrTempW.Empty();
hRes2 = cSid.Set(lpGroupInfo0[i].lgrpi0_name);
if (SUCCEEDED(hRes2))
hRes2 = cSid.GetStringSid(cStrTempW);
if (SUCCEEDED(hRes2))
{
if (cStrTempW.ConcatN(L"/", 1) == FALSE ||
cStrTempW.Concat(lpGroupInfo0[i].lgrpi0_name) == FALSE ||
cStrTempW.InsertN((cSid.IsAnyWellKnownSid() != FALSE) ? L"+" : L"-", 0, 1) == FALSE)
{
hRes2 = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hRes2))
{
if (aGroupsList.AddElement((LPWSTR)cStrTempW) != FALSE)
cStrTempW.Detach();
else
hRes2 = E_OUTOFMEMORY;
}
if (FAILED(hRes2))
{
hRes = hRes2;
break;
}
}
}
if (lpGroupInfo0 != NULL)
fnNetApiBufferFree(lpGroupInfo0);
}
while (hRes == HRESULT_FROM_WIN32(ERROR_MORE_DATA));
done:
//done
if (FAILED(hRes))
aGroupsList.RemoveAllElements();
return hRes;
}
}; //namespace System
}; //namespace MX
//-----------------------------------------------------------
static VOID InitializeApis()
{
static LONG volatile nInitialized = 0;
if (_InterlockedCompareExchange(&nInitialized, 2, 0) == 0)
{
HINSTANCE _hNetApi32Dll;
LPVOID _fnNetUserEnum, _fnNetLocalGroupEnum, _fnNetApiBufferFree;
if (SUCCEEDED(MX::System::LoadSystem32Dll(L"netapi32.dll", &_hNetApi32Dll)))
{
_fnNetUserEnum = ::GetProcAddress(_hNetApi32Dll, "NetUserEnum");
_fnNetLocalGroupEnum = ::GetProcAddress(_hNetApi32Dll, "NetLocalGroupEnum");
_fnNetApiBufferFree = ::GetProcAddress(_hNetApi32Dll, "NetApiBufferFree");
if (_fnNetUserEnum != NULL && _fnNetLocalGroupEnum != NULL && _fnNetApiBufferFree != NULL)
{
fnNetUserEnum = (lpfnNetUserEnum)_fnNetUserEnum;
fnNetLocalGroupEnum = (lpfnNetLocalGroupEnum)_fnNetLocalGroupEnum;
fnNetApiBufferFree = (lpfnNetApiBufferFree)_fnNetApiBufferFree;
}
else
{
::FreeLibrary(_hNetApi32Dll);
}
}
_InterlockedExchange(&nInitialized, 1);
}
else while (__InterlockedRead(&nInitialized) == 2)
{
::MxSleep(50);
}
return;
}