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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/*
 * PROJECT:     ReactOS System Control Panel Applet
 * LICENSE:     GPL - See COPYING in the top level directory
 * FILE:        dll/cpl/sysdm/userprofile.c
 * PURPOSE:     Computer settings for networking
 * COPYRIGHT:   Copyright Thomas Weidenmueller <w3seek@reactos.org>
 *              Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
 *
 */

#include "precomp.h"
#include <sddl.h>
#include <winnls.h>

#include <debug.h>

typedef struct _PROFILEDATA
{
    DWORD dwRefCount;
    DWORD dwState;
    BOOL bUnknownProfile;
    PWSTR pszFullName;
    PWSTR pszProfilePath;
} PROFILEDATA, *PPROFILEDATA;


static
BOOL
OnProfileTypeInit(
    _In_ HWND hwndDlg,
    _In_ PPROFILEDATA pProfileData)
{
    PWSTR pszRawBuffer = NULL, pszCookedBuffer = NULL;
    INT nLength;

    nLength = LoadStringW(hApplet, IDS_USERPROFILE_TYPE_TEXT, (PWSTR)&pszRawBuffer, 0);
    pszRawBuffer = NULL;
    if (nLength == 0)
        return FALSE;

    pszRawBuffer = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(WCHAR));
    if (pszRawBuffer == NULL)
        return FALSE;

    LoadStringW(hApplet, IDS_USERPROFILE_TYPE_TEXT, pszRawBuffer, nLength + 1);

    pszCookedBuffer = HeapAlloc(GetProcessHeap(), 0, (nLength + wcslen(pProfileData->pszFullName) + 1) * sizeof(WCHAR));
    if (pszCookedBuffer == NULL)
        goto done;

    swprintf(pszCookedBuffer, pszRawBuffer, pProfileData->pszFullName);

    /* Set the full text */
    SetDlgItemText(hwndDlg, IDC_USERPROFILE_TYPE_TEXT, pszCookedBuffer);

    /* FIXME: Right now, we support local user profiles only! */
    EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_TYPE_ROAMING), FALSE);
    Button_SetCheck(GetDlgItem(hwndDlg, IDC_USERPROFILE_TYPE_LOCAL), BST_CHECKED);
    EnableWindow(GetDlgItem(hwndDlg, IDOK), FALSE);

done:
    if (pszCookedBuffer != NULL)
        HeapFree(GetProcessHeap(), 0, pszCookedBuffer);

    if (pszRawBuffer != NULL)
        HeapFree(GetProcessHeap(), 0, pszRawBuffer);

    return TRUE;
}


static
INT_PTR
CALLBACK
UserProfileTypeDlgProc(
    _In_ HWND hwndDlg,
    _In_ UINT uMsg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
            OnProfileTypeInit(hwndDlg, (PPROFILEDATA)lParam);
            return TRUE;

        case WM_DESTROY:
            break;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                case IDCANCEL:
                    EndDialog(hwndDlg,
                              LOWORD(wParam));
                    return TRUE;
            }
            break;
    }

    return FALSE;
}


static
BOOL
ChangeUserProfileType(
    _In_ HWND hwndDlg)
{
    HWND hwndListView;
    LVITEM Item;
    INT iSelected;

    DPRINT("ChangeUserProfileType(%p)\n", hwndDlg);

    hwndListView = GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST);
    if (hwndListView == NULL)
        return FALSE;

    iSelected = ListView_GetNextItem(hwndListView, -1, LVNI_SELECTED);
    if (iSelected == -1)
        return FALSE;

    ZeroMemory(&Item, sizeof(LVITEM));
    Item.mask = LVIF_PARAM;
    Item.iItem = iSelected;
    if (!ListView_GetItem(hwndListView, &Item))
        return FALSE;

    if (Item.lParam == 0)
        return FALSE;

    if (DialogBoxParam(hApplet,
                       MAKEINTRESOURCE(IDD_USERPROFILE_TYPE),
                       hwndDlg,
                       UserProfileTypeDlgProc,
                       (LPARAM)Item.lParam) == IDOK)
    {
        /* FIXME: Update the profile list view */
        return TRUE;
    }

    return FALSE;
}


static
BOOL
DeleteUserProfile(
    _In_ HWND hwndDlg)
{
    WCHAR szTitle[64], szRawText[128], szCookedText[256];
    HWND hwndListView;
    LVITEM Item;
    INT iSelected;
    PPROFILEDATA pProfileData;

    DPRINT("DeleteUserProfile()\n");

    hwndListView = GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST);
    if (hwndListView == NULL)
        return FALSE;

    iSelected = ListView_GetNextItem(hwndListView, -1, LVNI_SELECTED);
    if (iSelected == -1)
        return FALSE;

    ZeroMemory(&Item, sizeof(LVITEM));
    Item.mask = LVIF_PARAM;
    Item.iItem = iSelected;
    if (!ListView_GetItem(hwndListView, &Item))
        return FALSE;

    if (Item.lParam == 0)
        return FALSE;

    pProfileData = (PPROFILEDATA)Item.lParam;
    if (pProfileData->dwRefCount != 0)
        return FALSE;

    LoadStringW(hApplet, IDS_USERPROFILE_CONFIRM_DELETE_TITLE, szTitle, ARRAYSIZE(szTitle));
    LoadStringW(hApplet, IDS_USERPROFILE_CONFIRM_DELETE, szRawText, ARRAYSIZE(szRawText));
    swprintf(szCookedText, szRawText, pProfileData->pszFullName);

    if (MessageBoxW(hwndDlg,
                    szCookedText,
                    szTitle,
                    MB_ICONQUESTION | MB_YESNO) == IDYES)
    {
        /* FIXME: Delete the profile here! */
        return TRUE;
    }

    return FALSE;
}


static
INT_PTR
CALLBACK
CopyUserProfileDlgProc(
    _In_ HWND hwndDlg,
    _In_ UINT uMsg,
    _In_ WPARAM wParam,
    _In_ LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
            return TRUE;

        case WM_DESTROY:
            break;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                case IDCANCEL:
                    EndDialog(hwndDlg,
                              LOWORD(wParam));
                    return TRUE;
            }
            break;
    }

    return FALSE;
}


static
BOOL
CopyUserProfile(
    _In_ HWND hwndDlg)
{
    HWND hwndListView;
    LVITEM Item;
    INT iSelected;

    DPRINT("CopyUserProfile()\n");

    hwndListView = GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST);
    if (hwndListView == NULL)
        return FALSE;

    iSelected = ListView_GetNextItem(hwndListView, -1, LVNI_SELECTED);
    if (iSelected == -1)
        return FALSE;

    ZeroMemory(&Item, sizeof(LVITEM));
    Item.mask = LVIF_PARAM;
    Item.iItem = iSelected;
    if (!ListView_GetItem(hwndListView, &Item))
        return FALSE;

    if (Item.lParam == 0)
        return FALSE;

    if (DialogBoxParam(hApplet,
                       MAKEINTRESOURCE(IDD_USERPROFILE_COPY),
                       hwndDlg,
                       CopyUserProfileDlgProc,
                       (LPARAM)Item.lParam) == IDOK)
    {
        /* FIXME: Update the profile list view */
        return TRUE;
    }

    return FALSE;
}


static VOID
SetListViewColumns(
    _In_ HWND hwndListView)
{
    LV_COLUMN column;
    RECT rect;
    TCHAR szStr[32];

    GetClientRect(hwndListView, &rect);

    SendMessage(hwndListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);

    memset(&column, 0x00, sizeof(column));
    column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM | LVCF_TEXT;
    column.fmt = LVCFMT_LEFT;
    column.cx = (INT)((rect.right - rect.left) * 0.40);
    column.iSubItem = 0;
    LoadString(hApplet, IDS_USERPROFILE_NAME, szStr, ARRAYSIZE(szStr));
    column.pszText = szStr;
    (void)ListView_InsertColumn(hwndListView, 0, &column);

    column.fmt = LVCFMT_RIGHT;
    column.cx = (INT)((rect.right - rect.left) * 0.15);
    column.iSubItem = 1;
    LoadString(hApplet, IDS_USERPROFILE_SIZE, szStr, ARRAYSIZE(szStr));
    column.pszText = szStr;
    (void)ListView_InsertColumn(hwndListView, 1, &column);

    column.fmt = LVCFMT_LEFT;
    column.cx = (INT)((rect.right - rect.left) * 0.15);
    column.iSubItem = 2;
    LoadString(hApplet, IDS_USERPROFILE_TYPE, szStr, ARRAYSIZE(szStr));
    column.pszText = szStr;
    (void)ListView_InsertColumn(hwndListView, 2, &column);

    column.fmt = LVCFMT_LEFT;
    column.cx = (INT)((rect.right - rect.left) * 0.15);
    column.iSubItem = 3;
    LoadString(hApplet, IDS_USERPROFILE_STATUS, szStr, ARRAYSIZE(szStr));
    column.pszText = szStr;
    (void)ListView_InsertColumn(hwndListView, 3, &column);

    column.fmt = LVCFMT_LEFT;
    column.cx = (INT)((rect.right - rect.left) * 0.15) - GetSystemMetrics(SM_CYHSCROLL);
    column.iSubItem = 4;
    LoadString(hApplet, IDS_USERPROFILE_MODIFIED, szStr, ARRAYSIZE(szStr));
    column.pszText = szStr;
    (void)ListView_InsertColumn(hwndListView, 4, &column);
}


static
BOOL
GetProfileSize(
    _In_ PWSTR pszProfilePath,
    _Inout_ PULONGLONG pullProfileSize)
{
    HANDLE hFile = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA FindData;
    DWORD dwProfilePathLength;
    ULARGE_INTEGER Size;
    BOOL bResult = TRUE;

    dwProfilePathLength = wcslen(pszProfilePath);

    wcscat(pszProfilePath, L"\\*.*");

    hFile = FindFirstFileW(pszProfilePath, &FindData);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        if ((GetLastError() != ERROR_FILE_NOT_FOUND) &&
            (GetLastError() != ERROR_PATH_NOT_FOUND))
            bResult = FALSE;

        goto done;
    }

    do
    {
        if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if ((_wcsicmp(FindData.cFileName, L".") == 0) ||
                (_wcsicmp(FindData.cFileName, L"..") == 0))
                continue;

            pszProfilePath[dwProfilePathLength + 1] = UNICODE_NULL;
            wcscat(pszProfilePath, FindData.cFileName);

            if (!GetProfileSize(pszProfilePath, pullProfileSize))
            {
                bResult = FALSE;
                goto done;
            }
        }
        else
        {
            Size.u.LowPart = FindData.nFileSizeLow;
            Size.u.HighPart = FindData.nFileSizeHigh;
            *pullProfileSize += Size.QuadPart;
        }
    }
    while (FindNextFile(hFile, &FindData));

done:
    pszProfilePath[dwProfilePathLength] = UNICODE_NULL;

    if (hFile != INVALID_HANDLE_VALUE)
        FindClose(hFile);

    return bResult;
}


static
BOOL
GetProfileName(
    _In_ PSID pProfileSid,
    _In_ DWORD dwNameBufferSize,
    _Out_ PWSTR pszNameBuffer,
    _Out_ PBOOL pbUnknownProfile)
{
    WCHAR szAccountName[128], szDomainName[128];
    DWORD dwAccountNameSize, dwDomainNameSize;
    SID_NAME_USE Use;

    dwAccountNameSize = ARRAYSIZE(szAccountName);
    dwDomainNameSize = ARRAYSIZE(szDomainName);
    if (!LookupAccountSidW(NULL,
                           pProfileSid,
                           szAccountName,
                           &dwAccountNameSize,
                           szDomainName,
                           &dwDomainNameSize,
                           &Use))
    {
        /* Unknown account */
        LoadStringW(hApplet, IDS_USERPROFILE_ACCOUNT_UNKNOWN, pszNameBuffer, dwNameBufferSize);
        *pbUnknownProfile = TRUE;
    }
    else
    {
        /* Show only the user accounts */
        if (Use != SidTypeUser)
            return FALSE;

        if (szAccountName[0] == UNICODE_NULL)
        {
            /* Deleted account */
            LoadStringW(hApplet, IDS_USERPROFILE_ACCOUNT_DELETED, pszNameBuffer, dwNameBufferSize);
        }
        else
        {
            /* Normal account */
            wsprintf(pszNameBuffer, L"%s\\%s", szDomainName, szAccountName);
        }
        *pbUnknownProfile = FALSE;
    }

    return TRUE;
}


static
VOID
FormatProfileSize(
    _Out_ LPWSTR Buffer,
    _In_ double size)
{
    const LPWSTR units[] = {L"MB", L"GB", L"TB"};
    int i = 0, j;

    size /= 1024;
    size /= 1024;

    while (size >= 1024 && i < 3)<--- Assuming that condition 'i<3' is not redundant
    {
        size /= 1024;
        i++;
    }

    if (size < 10)
        j = 2;
    else if (size < 100)
        j = 1;
    else
        j = 0;

    swprintf(Buffer, L"%.*f %s", j, size, units[i]);<--- Array index out of bounds
}


static VOID
AddUserProfile(
    _In_ HWND hwndListView,
    _In_ PSID pProfileSid,
    _In_ HKEY hProfileKey)
{
    WCHAR szTempProfilePath[MAX_PATH], szProfilePath[MAX_PATH];
    WCHAR szNameBuffer[256];
    PPROFILEDATA pProfileData = NULL;
    DWORD dwProfileData, dwSize, dwType, dwState = 0, dwRefCount = 0;
    DWORD dwProfilePathLength;
    PWSTR ptr;
    INT nId, iItem;
    LV_ITEM lvi;
    WIN32_FIND_DATA FindData;
    HANDLE hFile;
    SYSTEMTIME SystemTime;
    ULONGLONG ullProfileSize;
    BOOL bUnknownProfile;
    DWORD dwError;

    /* Get the profile path */
    dwSize = MAX_PATH * sizeof(WCHAR);
    dwError = RegQueryValueExW(hProfileKey,
                               L"ProfileImagePath",
                               NULL,
                               &dwType,
                               (LPBYTE)szTempProfilePath,
                               &dwSize);
    if (dwError != ERROR_SUCCESS)
        return;

    /* Expand it */
    ExpandEnvironmentStringsW(szTempProfilePath,
                              szProfilePath,
                              MAX_PATH);

    /* Check if the profile path exists */
    hFile = FindFirstFileW(szProfilePath, &FindData);
    if (hFile == INVALID_HANDLE_VALUE)
        return;

    FindClose(hFile);

    /* Get the length of the profile path */
    dwProfilePathLength = wcslen(szProfilePath);

    /* Check for the ntuser.dat file */
    wcscat(szProfilePath, L"\\ntuser.dat");
    hFile = FindFirstFileW(szProfilePath, &FindData);
    if (hFile == INVALID_HANDLE_VALUE)
        return;

    FindClose(hFile);
    szProfilePath[dwProfilePathLength] = UNICODE_NULL;

    /* Get the profile size */
    ullProfileSize = 0ULL;
    GetProfileSize(szProfilePath, &ullProfileSize);

    /* Get the profile name */
    if (!GetProfileName(pProfileSid,
                        ARRAYSIZE(szNameBuffer),
                        szNameBuffer,
                        &bUnknownProfile))
        return;

    /* Get the profile state value */
    dwSize = sizeof(dwState);
    if (RegQueryValueExW(hProfileKey,
                         L"State",
                         NULL,
                         &dwType,
                         (LPBYTE)&dwState,
                         &dwSize) != ERROR_SUCCESS)
    {
        dwState = 0;
    }

    /* Get the profile reference counter */
    dwSize = sizeof(dwRefCount);
    if (RegQueryValueExW(hProfileKey,
                         L"RefCount",
                         NULL,
                         &dwType,
                         (LPBYTE)&dwRefCount,
                         &dwSize) != ERROR_SUCCESS)
    {
        dwRefCount = 0;
    }

    /* Create and fill the profile data entry */
    dwProfileData = sizeof(PROFILEDATA) +
                    ((wcslen(szNameBuffer) + 1) * sizeof(WCHAR)) +
                    ((wcslen(szProfilePath) + 1) * sizeof(WCHAR));
    pProfileData = HeapAlloc(GetProcessHeap(),
                             HEAP_ZERO_MEMORY,
                             dwProfileData);
    if (pProfileData == NULL)
        return;

    pProfileData->dwRefCount = dwRefCount;
    pProfileData->dwState = dwState;
    pProfileData->bUnknownProfile = bUnknownProfile;

    ptr = (PWSTR)((ULONG_PTR)pProfileData + sizeof(PROFILEDATA));
    pProfileData->pszFullName = ptr;

    wcscpy(pProfileData->pszFullName, szNameBuffer);

    ptr = (PWSTR)((ULONG_PTR)ptr + ((wcslen(pProfileData->pszFullName) + 1) * sizeof(WCHAR)));
    pProfileData->pszProfilePath = ptr;
    wcscpy(pProfileData->pszProfilePath, szProfilePath);

    /* Add the profile and set its name */
    memset(&lvi, 0x00, sizeof(lvi));
    lvi.mask = LVIF_TEXT | LVIF_STATE | LVIF_PARAM;
    lvi.pszText = pProfileData->pszFullName;
    lvi.state = 0;
    lvi.lParam = (LPARAM)pProfileData;
    iItem = ListView_InsertItem(hwndListView, &lvi);

    /* Set the profile size */
    FormatProfileSize(szNameBuffer, (double)ullProfileSize);
    ListView_SetItemText(hwndListView, iItem, 1, szNameBuffer);

    /* Set the profile type */
    if (dwState & 0x0010) // PROFILE_UPDATE_CENTRAL
        nId = IDS_USERPROFILE_ROAMING;
    else
        nId = IDS_USERPROFILE_LOCAL;

    LoadStringW(hApplet, nId, szNameBuffer, ARRAYSIZE(szNameBuffer));

    ListView_SetItemText(hwndListView, iItem, 2, szNameBuffer);

    /* FIXME: Set the profile status */
    if (dwState & 0x0001) // PROFILE_MANDATORY
        nId = IDS_USERPROFILE_MANDATORY;
    else if (dwState & 0x0010) // PROFILE_UPDATE_CENTRAL
        nId = IDS_USERPROFILE_ROAMING;
    else
        nId = IDS_USERPROFILE_LOCAL;

    LoadStringW(hApplet, nId, szNameBuffer, ARRAYSIZE(szNameBuffer));

    ListView_SetItemText(hwndListView, iItem, 3, szNameBuffer);

    /* Set the profile modified time */
    FileTimeToSystemTime(&FindData.ftLastWriteTime,
                         &SystemTime);

    GetDateFormatW(LOCALE_USER_DEFAULT,
                   DATE_SHORTDATE,
                   &SystemTime,
                   NULL,
                   szNameBuffer,
                   ARRAYSIZE(szNameBuffer));

    ListView_SetItemText(hwndListView, iItem, 4, szNameBuffer);
}


static VOID
UpdateButtonState(
    _In_ HWND hwndDlg,
    _In_ HWND hwndListView)
{
    LVITEM Item;
    INT iSelected;
    BOOL bChange = FALSE;
    BOOL bCopy = FALSE;
    BOOL bDelete = FALSE;
    PPROFILEDATA pProfileData;

    if (ListView_GetSelectedCount(hwndListView) != 0)
    {
        iSelected = ListView_GetNextItem(hwndListView, -1, LVNI_SELECTED);
        if (iSelected != -1)
        {
            ZeroMemory(&Item, sizeof(LVITEM));
            Item.mask = LVIF_PARAM;
            Item.iItem = iSelected;
            if (ListView_GetItem(hwndListView, &Item))
            {
                if (Item.lParam != 0)
                {
                    pProfileData = (PPROFILEDATA)Item.lParam;

                    if (pProfileData->bUnknownProfile)
                    {
                        bDelete = TRUE;
                        bCopy = FALSE;
                    }
                    else
                    {
                        bDelete = (pProfileData->dwRefCount == 0);
                        bCopy = (pProfileData->dwRefCount == 0);
                    }
                }
            }

            bChange = TRUE;
        }
    }

    EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_CHANGE), bChange);
    EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_DELETE), bDelete);
    EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_COPY), bCopy);
}


static VOID
AddUserProfiles(
    _In_ HWND hwndDlg,
    _In_ HWND hwndListView,
    _In_ BOOL bAdmin)
{
    HKEY hKeyUserProfiles = INVALID_HANDLE_VALUE;
    HKEY hProfileKey;
    DWORD dwIndex;
    WCHAR szProfileSid[64];
    DWORD dwSidLength;
    FILETIME ftLastWrite;
    DWORD dwSize;
    HANDLE hToken = NULL;
    PTOKEN_USER pTokenUser = NULL;
    PSID pProfileSid;
    PWSTR pszProfileSid;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
        return;

    GetTokenInformation(hToken, TokenUser, NULL, 0, &dwSize);
    if (dwSize == 0)
        goto done;

    pTokenUser = HeapAlloc(GetProcessHeap(), 0, dwSize);
    if (pTokenUser == NULL)
        goto done;

    if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dwSize, &dwSize))
        goto done;

    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                      L"Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
                      0,
                      KEY_READ,
                      &hKeyUserProfiles))
        goto done;

    if (bAdmin)
    {
        for (dwIndex = 0; ; dwIndex++)
        {
            dwSidLength = ARRAYSIZE(szProfileSid);
            if (RegEnumKeyExW(hKeyUserProfiles,
                              dwIndex,
                              szProfileSid,
                              &dwSidLength,
                              NULL,
                              NULL,
                              NULL,
                              &ftLastWrite))
                break;

            if (RegOpenKeyExW(hKeyUserProfiles,
                              szProfileSid,
                              0,
                              KEY_READ,
                              &hProfileKey) == ERROR_SUCCESS)
            {
                if (ConvertStringSidToSid(szProfileSid, &pProfileSid))
                {
                    AddUserProfile(hwndListView,
                                   pProfileSid,
                                   hProfileKey);
                    LocalFree(pProfileSid);
                }

                RegCloseKey(hProfileKey);
            }
        }
    }
    else
    {
        if (ConvertSidToStringSidW(pTokenUser->User.Sid, &pszProfileSid))
        {
            if (RegOpenKeyExW(hKeyUserProfiles,
                              pszProfileSid,
                              0,
                              KEY_READ,
                              &hProfileKey) == ERROR_SUCCESS)
            {
                AddUserProfile(hwndListView,
                               pTokenUser->User.Sid,
                               hProfileKey);
                RegCloseKey(hProfileKey);
            }

            LocalFree(pszProfileSid);
        }
    }

    if (ListView_GetItemCount(hwndListView) != 0)
        ListView_SetItemState(hwndListView, 0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

    UpdateButtonState(hwndDlg, hwndListView);

done:
    if (hKeyUserProfiles != INVALID_HANDLE_VALUE)
        RegCloseKey(hKeyUserProfiles);

    if (pTokenUser != NULL)
        HeapFree(GetProcessHeap(), 0, pTokenUser);

    if (hToken != NULL)
        CloseHandle(hToken);
}


static VOID
OnInitUserProfileDialog(HWND hwndDlg)
{
    BOOL bAdmin;

    bAdmin = IsUserAdmin();

    /* Initialize the list view control */
    SetListViewColumns(GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST));

    /* Hide the delete and copy buttons for non-admins */
    ShowWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_DELETE), bAdmin ? SW_SHOW : SW_HIDE);
    ShowWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_COPY), bAdmin ? SW_SHOW : SW_HIDE);

    /* Add the profiles to the list view */
    AddUserProfiles(hwndDlg, GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST), bAdmin);
}

static
VOID
OnNotify(
    _In_ HWND hwndDlg,
    _In_ NMHDR *nmhdr)
{
    LPNMLISTVIEW pNMLV;
    
    if (nmhdr->idFrom == IDC_USERACCOUNT_LINK && nmhdr->code == NM_CLICK)
    {
        ShellExecuteW(hwndDlg, NULL, L"usrmgr.cpl", NULL, NULL, 0);
    }
    else if (nmhdr->idFrom == IDC_USERPROFILE_LIST)
    {
        switch(nmhdr->code)
        {
            case LVN_ITEMCHANGED:
                UpdateButtonState(hwndDlg, nmhdr->hwndFrom);            
                break;
            
            case NM_DBLCLK:
                ChangeUserProfileType(hwndDlg);
                break;
                
            case LVN_DELETEITEM:
                pNMLV = (LPNMLISTVIEW)nmhdr;            
                if (pNMLV->lParam != 0)
                    HeapFree(GetProcessHeap(), 0, (LPVOID)pNMLV->lParam);                
                break;
        }        
    }
}


/* Property page dialog callback */
INT_PTR CALLBACK
UserProfileDlgProc(HWND hwndDlg,
                   UINT uMsg,
                   WPARAM wParam,
                   LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
            OnInitUserProfileDialog(hwndDlg);
            return TRUE;
    
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                case IDCANCEL:
                    EndDialog(hwndDlg,
                              LOWORD(wParam));
                    return TRUE;

                case IDC_USERPROFILE_CHANGE:
                    ChangeUserProfileType(hwndDlg);
                    break;

                case IDC_USERPROFILE_DELETE:
                    DeleteUserProfile(hwndDlg);
                    break;

                case IDC_USERPROFILE_COPY:
                    CopyUserProfile(hwndDlg);
                    break;
            }
            break;

        case WM_NOTIFY:
            OnNotify(hwndDlg, (NMHDR *)lParam);
            break;
    }

    return FALSE;
}