-
Notifications
You must be signed in to change notification settings - Fork 77
/
Utils.cpp
2109 lines (1754 loc) · 65.6 KB
/
Utils.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
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************/
/* Utils.cpp Copyright (c) Ladislav Zezula 2004 */
/*---------------------------------------------------------------------------*/
/* Description: */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 05.01.04 1.00 Lad The first version of Utils.cpp */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// _tcstol replacement
static BYTE CharToValue[0x80] =
{
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
LPCTSTR HexaAlphabetUpper = _T("0123456789ABCDEF");
static bool IsPossibleFileId(LPCTSTR szFileName)
{
size_t nLength = _tcslen(szFileName);
// File ID has 15 characters, object ID has 32
if(nLength != 16 && nLength != 32)
return false;
// File ID can only contain hexadecimal letters
for(size_t i = 0; i < nLength; i++)
{
if(!isxdigit(szFileName[i]))
return false;
}
// All checks passed. It could be a file ID
return true;
}
static void ShowConversionError(HWND hWndParent, HWND hWndChild)
{
// Show the error to the user
MessageBoxRc(hWndParent, IDS_ERROR, IDS_E_CONVERT_VALUE);
// Select and focus the control
SendMessage(hWndChild, EM_SETSEL, 0, (LPARAM)-1);
SetFocus(hWndChild);
}
//-----------------------------------------------------------------------------
// Converts the string to long. This function was written because
// the builtin strtol does not work correctly (try to convert 0xAABBCCDD !!!)
DWORD StrToInt(LPCTSTR szString, LPTSTR * szEnd, int nRadix)
{
DWORD nSaveValue = 0;
DWORD nValue = 0;
int nDigit;
while((nDigit = szString[0]) != 0)
{
// If the character is not an hexa number, break
if(nDigit > 0x80 || CharToValue[nDigit] == 0xFF)
break;
// Convert to digit
nDigit = CharToValue[nDigit];
if(nDigit > (nRadix - 1))
break;
// Move the value to the next rank and add the digit
nSaveValue = nValue;
nValue *= nRadix;
nValue += nDigit;
szString++;
// Overflow check
if(nValue < nSaveValue)
break;
}
if(szEnd != NULL)
*szEnd = (LPTSTR)szString;
return nValue;
}
//-----------------------------------------------------------------------------
// bool values support
DWORD Text2Bool(LPCTSTR szText, bool * pValue)
{
bool bNewValue = false;
if(_tcsicmp(szText, _T("true")))
bNewValue = true;
else if(_tcsicmp(szText, _T("on")))
bNewValue = true;
else if(_tcsicmp(szText, _T("1")))
bNewValue = true;
pValue[0] = bNewValue;
return ERROR_SUCCESS;
}
//-----------------------------------------------------------------------------
// 32-bit values support
LPCTSTR SkipHexaPrefix(LPCTSTR szString)
{
return (szString[0] == _T('0') && (szString[1] == _T('x') || szString[1] == _T('X'))) ? szString + 2 : szString;
}
DWORD Text2Hex32(LPCTSTR szText, PDWORD pValue)
{
DWORD Value = 0;
int nDigit;
// Skip the C-style hexa prefix, if any
szText = SkipHexaPrefix(szText);
// Convert the value
while((nDigit = *szText++) != 0)
{
// If the character is not a hexa number, break
if(nDigit > 0x80 || CharToValue[nDigit] == 0xFF)
return ERROR_BAD_FORMAT;
// Convert to digit
nDigit = CharToValue[nDigit];
// Overflow check
if(Value & 0xF0000000)
return ERROR_ARITHMETIC_OVERFLOW;
Value = (Value << 0x04) + nDigit;
}
// Give the value to the caller
*pValue = Value;
return ERROR_SUCCESS;
}
DWORD DlgText2Hex32(HWND hDlg, UINT nIDCtrl, PDWORD pValue)
{
TCHAR szText[128];
HWND hWndChild = GetDlgItem(hDlg, nIDCtrl);
DWORD dwErrCode = ERROR_INVALID_PARAMETER;
// Perform the conversion of the child text to a 32-bit hexa value
if(hWndChild != NULL)
{
// Retrieve the window text
GetWindowText(hWndChild, szText, _countof(szText));
// Attempt to convert the value
dwErrCode = Text2Hex32(szText, pValue);
if(dwErrCode != ERROR_SUCCESS)
ShowConversionError(hDlg, hWndChild);
}
return dwErrCode;
}
void Hex2TextXX(ULONGLONG Value, LPTSTR szBuffer, size_t nSize)
{
// Get the number of hexa digits
nSize = nSize + nSize - 1;
// Convert the number to string
for(size_t i = 0; i <= nSize; i++)
{
szBuffer[nSize - i] = HexaAlphabetUpper[Value & 0x0F];
Value >>= 4;
}
// Terminate the string
szBuffer[nSize + 1] = 0;
}
void Hex2Text32(LPTSTR szBuffer, DWORD Value)
{
Hex2TextXX(Value, szBuffer, sizeof(DWORD));
}
void Hex2DlgText32(HWND hDlg, UINT nIDCtrl, DWORD Value)
{
TCHAR szText[128];
Hex2TextXX(Value, szText, sizeof(DWORD));
SetDlgItemText(hDlg, nIDCtrl, szText);
}
//-----------------------------------------------------------------------------
// Pointer support
DWORD Text2HexPtr(LPCTSTR szText, PDWORD_PTR pValue)
{
DWORD_PTR ValueMask = ((DWORD_PTR)0x0F << ((sizeof(DWORD_PTR) * 8) - 4));
DWORD_PTR Value = 0;
int nDigit;
// Skip the C-style hexa prefix, if any
szText = SkipHexaPrefix(szText);
// Convert the value
while((nDigit = *szText++) != 0)
{
// If the character is not a hexa number, break
if(nDigit > 0x80 || CharToValue[nDigit] == 0xFF)
return ERROR_BAD_FORMAT;
// Convert to digit
nDigit = CharToValue[nDigit];
// Overflow check
if(Value & ValueMask)
return ERROR_ARITHMETIC_OVERFLOW;
Value = (Value << 0x04) + nDigit;
}
// Give the value to the caller
*pValue = Value;
return ERROR_SUCCESS;
}
DWORD DlgText2HexPtr(HWND hDlg, UINT nIDCtrl, PDWORD_PTR pValue)
{
TCHAR szText[128];
HWND hWndChild = GetDlgItem(hDlg, nIDCtrl);
DWORD dwErrCode = ERROR_INVALID_PARAMETER;
// Perform the conversion of the child text to a 32-bit hexa value
if(hWndChild != NULL)
{
// Retrieve the window text
GetWindowText(hWndChild, szText, _countof(szText));
// Attempt to convert the value
dwErrCode = Text2HexPtr(szText, pValue);
if(dwErrCode != ERROR_SUCCESS)
ShowConversionError(hDlg, hWndChild);
}
return dwErrCode;
}
void Hex2TextPtr(LPTSTR szBuffer, DWORD_PTR Value)
{
Hex2TextXX(Value, szBuffer, sizeof(DWORD_PTR));
}
void Hex2DlgTextPtr(HWND hDlg, UINT nIDCtrl, DWORD_PTR Value)
{
TCHAR szText[128];
Hex2TextXX(Value, szText, sizeof(DWORD_PTR));
SetDlgItemText(hDlg, nIDCtrl, szText);
}
//-----------------------------------------------------------------------------
// 64-bit values support
DWORD Text2Hex64(LPCTSTR szText, PLONGLONG pValue)
{
ULONGLONG SaveValue = 0;
ULONGLONG Value = 0;
int nDigit;
// Skip the C-style hexa prefix, if any
szText = SkipHexaPrefix(szText);
// Convert the value
while((nDigit = *szText++) != 0)
{
// If the character is not a hexa number, break
if(nDigit > 0x80 || CharToValue[nDigit] == 0xFF)
return ERROR_BAD_FORMAT;
// Convert to digit
nDigit = CharToValue[nDigit];
// Move the value to the next rank and add the digit
SaveValue = Value;
Value = (Value << 0x04) + nDigit;
// Overflow check
if(Value < SaveValue)
return ERROR_ARITHMETIC_OVERFLOW;
}
// Give the value to the caller
*pValue = Value;
return ERROR_SUCCESS;
}
DWORD DlgText2Hex64(HWND hDlg, UINT nIDCtrl, PLONGLONG pValue)
{
TCHAR szText[128];
HWND hWndChild = GetDlgItem(hDlg, nIDCtrl);
DWORD dwErrCode = ERROR_INVALID_PARAMETER;
// Perform the conversion of the child text to a 32-bit hexa value
if(hWndChild != NULL)
{
// Retrieve the window text
GetWindowText(hWndChild, szText, _countof(szText));
// Attempt to convert the value
dwErrCode = Text2Hex64(szText, pValue);
if(dwErrCode != ERROR_SUCCESS)
ShowConversionError(hDlg, hWndChild);
}
return dwErrCode;
}
void Hex2Text64(LPTSTR szBuffer, LONGLONG Value)
{
Hex2TextXX(Value, szBuffer, sizeof(LONGLONG));
}
void Hex2DlgText64(HWND hDlg, UINT nIDCtrl, LONGLONG Value)
{
TCHAR szText[128];
Hex2TextXX(Value, szText, sizeof(LONGLONG));
SetDlgItemText(hDlg, nIDCtrl, szText);
}
//-----------------------------------------------------------------------------
// Clipboard manipulation
HGLOBAL Clipboard_AddText(HGLOBAL hMemory, LPCTSTR szText)
{
LPBYTE pbClipboard;
size_t cbTotalSize;
size_t cbPrevSize = 0;
size_t cbAddSize = _tcslen(szText) * sizeof(TCHAR);
// If we don't have any previousle allocated memory, then allocate new block
if(hMemory == NULL)
{
// Allocate text plus EOS
cbTotalSize = cbAddSize + sizeof(WCHAR);
hMemory = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, cbTotalSize);
}
else
{
// Find out the previous size of the memory
cbPrevSize = GlobalSize(hMemory);
cbTotalSize = cbPrevSize + cbAddSize;
hMemory = GlobalReAlloc(hMemory, cbTotalSize, GMEM_MOVEABLE | GMEM_ZEROINIT);
// Subtract the end-of-string
cbPrevSize -= sizeof(TCHAR);
}
// Now if we have a memory allocated, append the text there
if(hMemory != NULL)
{
pbClipboard = (LPBYTE)GlobalLock(hMemory);
if(pbClipboard != NULL)
{
memcpy(pbClipboard + cbPrevSize, szText, cbAddSize);
GlobalUnlock(hMemory);
}
}
// Return the allocated memory
return hMemory;
}
bool Clipboard_Finish(HWND hWnd, HGLOBAL hMemory)
{
if(hMemory != NULL)
{
// Insert text to the clipboard
if(OpenClipboard(hWnd))
{
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMemory);
CloseClipboard();
return true;
}
// If the opening clipboard failed, free the memory
GlobalFree(hMemory);
}
return false;
}
//-----------------------------------------------------------------------------
// Path manipulation
LPTSTR FindDirectoryPathPart(LPTSTR szFullPath)
{
LPTSTR szPathPart = szFullPath;
// Skip the initial complicated parts
// \\.\GlobalRoot\Device\Mup\Dir1\Dir2
// \\.\Device\Mup\Dir1\Dir2
while(szPathPart[0] != 0 && !isalpha(szPathPart[0]))
szPathPart++;
// If found, search fuhrter for well-known path parts
if(szPathPart[0] != 0)
{
// Is it "GlobalRoot" ?
if(!_tcsnicmp(szPathPart, _T("GlobalRoot\\"), 11))
szPathPart += 11;
if(!_tcsnicmp(szPathPart, _T("Device\\"), 7))
szPathPart += 11;
// Skip the Drive letter, if present
if(isalpha(szPathPart[0]) && szPathPart[1] == _T(':'))
szPathPart += 2;
// Skip slashes and backslashes
while(szPathPart[0] == _T('\\') || szPathPart[0] == _T('/'))
szPathPart++;
// Return what we got
return szPathPart;
}
// Not recognized, return NULL
return NULL;
}
LPTSTR FindNextPathSeparator(LPTSTR szPathPart)
{
// Skip slashes and backslashes
while(szPathPart[0] == _T('\\') || szPathPart[0] == _T('/'))
szPathPart++;
// Find next slash, backslash or end of string
while(szPathPart[0] != 0 && szPathPart[0] != _T('\\') && szPathPart[0] != _T('/'))
szPathPart++;
return szPathPart;
}
//-----------------------------------------------------------------------------
// Other
// Calculates the necessary length of PFILE_FULL_EA_INFORMATION structure
ULONG GetEaEntrySize(PFILE_FULL_EA_INFORMATION EaInfo)
{
ULONG EntrySize = FIELD_OFFSET(FILE_FULL_EA_INFORMATION, EaName[0]);
//
// The length of the item must be calculated very exactly.
// No additional bytes (except for default alignment of
// FILE_FULL_EA_INFORMATION structure) is allowed.
//
EntrySize += EaInfo->EaNameLength + 1; // Add the name length plus the ending zero character
EntrySize += EaInfo->EaValueLength; // Add the data length
EntrySize = ALIGN_INT32(EntrySize); // Align to 4-byte boundary
return EntrySize;
}
void TreeView_SetItemText(HWND hWndTree, HTREEITEM hItem, LPCTSTR szText)
{
TVITEM tvi = {TVIF_TEXT, hItem};
tvi.pszText = (LPTSTR)(szText);
TreeView_SetItem(hWndTree, &tvi);
}
DWORD TreeView_GetChildCount(HWND hWndTree, HTREEITEM hItem)
{
DWORD dwChildCount = 0;
// If both TreeView and item handle are valid
if(hWndTree != NULL && hItem != NULL)
{
// Count all children
hItem = TreeView_GetChild(hWndTree, hItem);
while(hItem != NULL)
{
dwChildCount++;
hItem = TreeView_GetNextSibling(hWndTree, hItem);
}
}
return dwChildCount;
}
LPARAM TreeView_GetItemParam(HWND hWndTree, HTREEITEM hItem)
{
TVITEM tvi;
// Retrieve the item param
tvi.mask = TVIF_PARAM;
tvi.hItem = hItem;
tvi.lParam = 0;
TreeView_GetItem(hWndTree, &tvi);
// Return the parameter
return tvi.lParam;
}
void TreeView_SetItemParam(HWND hWndTree, HTREEITEM hItem, LPARAM lParam)
{
TVITEM tvi = {TVIF_PARAM};
// Set the new item param
tvi.hItem = hItem;
tvi.lParam = lParam;
TreeView_SetItem(hWndTree, &tvi);
}
HTREEITEM TreeView_SetTreeItem(HWND hWndTree, HTREEITEM hItem, LPCTSTR szText, LPARAM lParam)
{
TVITEM tvi = {TVIF_TEXT | TVIF_PARAM};
// Prepare the TVITEM structure
tvi.hItem = hItem;
tvi.lParam = lParam;
tvi.pszText = (LPTSTR)szText;
// Set the text¶m to the tree item
return TreeView_SetItem(hWndTree, &tvi) ? hItem : NULL;
}
BOOL TreeView_EditLabel_ID(HWND hDlg, UINT nID)
{
HTREEITEM hItem = NULL;
HWND hWndTree = GetDlgItem(hDlg, nID);
BOOL bResult = FALSE;
// Only start editing if the proper tree view has focus
if(GetFocus() == hWndTree)
{
hItem = TreeView_GetSelection(hWndTree);
if(hItem != NULL)
{
TreeView_EditLabel(hWndTree, hItem);
bResult = TRUE;
}
}
return bResult;
}
HTREEITEM InsertTreeItem(HWND hWndTree, HTREEITEM hParent, HTREEITEM hInsertAfter, LPCTSTR szText, PVOID pParam)
{
TVINSERTSTRUCT tvis;
// Insert the item to the tree
tvis.hParent = (hParent != NULL) ? hParent : TVI_ROOT;
tvis.hInsertAfter = (hInsertAfter != NULL) ? hInsertAfter : TVI_LAST;
tvis.item.mask = TVIF_TEXT | TVIF_PARAM;
tvis.item.pszText = (LPTSTR)szText;
tvis.item.lParam = (LPARAM)pParam;
return TreeView_InsertItem(hWndTree, &tvis);
}
HTREEITEM InsertTreeItem(HWND hWndTree, HTREEITEM hParent, LPCTSTR szText, PVOID pParam)
{
return InsertTreeItem(hWndTree, hParent, NULL, szText, pParam);
}
HTREEITEM InsertTreeItem(HWND hWndTree, HTREEITEM hParent, LPCTSTR szText, LPARAM lParam)
{
return InsertTreeItem(hWndTree, hParent, TVI_LAST, szText, (PVOID)lParam);
}
HTREEITEM InsertTreeItem(HWND hWndTree, HTREEITEM hParent, LPARAM lParam, UINT nID, ...)
{
va_list argList;
TCHAR szItemText[512];
// Format the text
va_start(argList, nID);
rsvprintf(szItemText, _countof(szItemText), nID, argList);
va_end(argList);
// Insert the item text
return InsertTreeItem(hWndTree, hParent, TVI_LAST, szItemText, (PVOID)lParam);
}
void TreeView_DeleteChildren(HWND hWndTree, HTREEITEM hParent)
{
HTREEITEM hItem;
// Remove all children, if any
while((hItem = TreeView_GetChild(hWndTree, hParent)) != NULL)
TreeView_DeleteItem(hWndTree, hItem);
}
HGLOBAL TreeView_CopyToClipboard(HWND hWndTree, HTREEITEM hItem, HGLOBAL hGlobal, size_t nLevel)
{
HTREEITEM hChild;
TVITEMEX tvi;
TCHAR szBuffer[0x400] = _T("");
TCHAR szIndent[0x400];
size_t i;
// Prepare the item
ZeroMemory(&tvi, sizeof(TVITEM));
tvi.mask = TVIF_TEXT;
// Get all siblings
while(hItem != NULL)
{
// Get the item text
memset(szBuffer, 0, sizeof(szBuffer));
tvi.hItem = hItem;
tvi.pszText = szBuffer;
tvi.cchTextMax = _countof(szBuffer);
tvi.cchTextMax = _countof(szBuffer);
TreeView_GetItem(hWndTree, &tvi);
StringCchCat(szBuffer, _countof(szBuffer), _T("\r\n"));
// Put the text to clipboard
for(i = 0; i < nLevel * 4; i++)
szIndent[i] = ' ';
szIndent[i] = 0;
// Insert the indent
hGlobal = Clipboard_AddText(hGlobal, szIndent);
hGlobal = Clipboard_AddText(hGlobal, szBuffer);
// Are there any children?
if((hChild = TreeView_GetChild(hWndTree, hItem)) != NULL)
{
hGlobal = TreeView_CopyToClipboard(hWndTree, hChild, hGlobal, nLevel + 1);
}
// Get the next sibling
hItem = TreeView_GetNextSibling(hWndTree, hItem);
}
return hGlobal;
}
void TreeView_CopyToClipboard(HWND hWndTree)
{
HGLOBAL hGlobal = NULL;
hGlobal = TreeView_CopyToClipboard(hWndTree, TreeView_GetRoot(hWndTree), hGlobal, 0);
Clipboard_Finish(hWndTree, hGlobal);
}
int OnTVKeyDown_CopyToClipboard(HWND /* hDlg */, LPNMTVKEYDOWN pNMTVKeyDown)
{
// On Ctrl+C, copy the text to clipboard
if(pNMTVKeyDown->wVKey == 'C' && GetAsyncKeyState(VK_CONTROL) < 0)
{
TreeView_CopyToClipboard(pNMTVKeyDown->hdr.hwndFrom);
return TRUE;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Token operations
HANDLE OpenCurrentToken(DWORD dwDesiredAccess)
{
HANDLE hToken = NULL;
// Open process or thread token
if(!OpenThreadToken(GetCurrentThread(), dwDesiredAccess, TRUE, &hToken))
{
if(GetLastError() == ERROR_NO_TOKEN)
OpenProcessToken(GetCurrentProcess(), dwDesiredAccess, &hToken);
}
return hToken;
}
BOOL GetTokenElevation(PBOOL pbElevated)
{
TOKEN_ELEVATION Elevation;
HANDLE hToken;
DWORD dwLength = 0;
DWORD dwValue = 0;
BOOL bResult = FALSE;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
// Step1: Query the elevation type
bResult = GetTokenInformation(hToken, TokenElevationType, &dwValue, sizeof(dwValue), &dwLength);
if(bResult)
{
switch((TOKEN_ELEVATION_TYPE)dwValue)
{
case TokenElevationTypeDefault:
bResult = GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &dwLength);
if(bResult)
pbElevated[0] = Elevation.TokenIsElevated ? TRUE : FALSE;
break;
case TokenElevationTypeFull:
pbElevated[0] = TRUE;
break;
case TokenElevationTypeLimited:
pbElevated[0] = FALSE;
break;
}
}
CloseHandle(hToken);
}
return bResult;
}
BOOL GetTokenVirtualizationEnabled(PBOOL pbEnabled)
{
HANDLE hToken;
DWORD dwEnabled = 0;
DWORD dwLength = 0;
BOOL bResult = FALSE;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
bResult = GetTokenInformation(hToken, TokenVirtualizationEnabled, &dwEnabled, sizeof(dwEnabled), &dwLength);
if(bResult && pbEnabled)
pbEnabled[0] = dwEnabled ? TRUE : FALSE;
CloseHandle(hToken);
}
return bResult;
}
BOOL SetTokenVirtualizationEnabled(BOOL bEnabled)
{
HANDLE hToken;
DWORD dwEnabled = bEnabled;
BOOL bResult = FALSE;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_DEFAULT, &hToken))
{
bResult = SetTokenInformation(hToken,
TokenVirtualizationEnabled,
&dwEnabled,
sizeof(dwEnabled));
CloseHandle(hToken);
}
return bResult;
}
//-----------------------------------------------------------------------------
// Set result status
static void DeleteBlinkTimer(HWND hDlg)
{
TFileTestData * pData = GetDialogData(hDlg);
// If there is already a blinker timer, destroy it
if(pData->BlinkTimer != 0)
KillTimer(hDlg, pData->BlinkTimer);
pData->BlinkTimer = 0;
// If there is a blink window already, destroy it
if(pData->hWndBlink != NULL)
DestroyWindow(pData->hWndBlink);
pData->hWndBlink = NULL;
}
static VOID CALLBACK BlinkTimerProc(HWND hDlg, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
UNREFERENCED_PARAMETER(dwTime);
UNREFERENCED_PARAMETER(uMsg);
if(idEvent == WM_TIMER_BLINK)
{
DeleteBlinkTimer(hDlg);
}
}
HWND AttachIconToEdit(HWND hDlg, HWND hWndChild, LPTSTR szIDIcon)
{
HINSTANCE hInst = g_hInst;
HICON hIcon;
POINT pt;
HWND hWndBlink = NULL;
RECT rect;
int cx = 16; // GetSystemMetrics(SM_CXSMICON);
int cy = 16; // GetSystemMetrics(SM_CYSMICON);
// Load OEM icons if it's their ID
if(IDI_APPLICATION <= szIDIcon && szIDIcon <= IDI_SHIELD)
hInst = NULL;
// Load the icon, 16x16
hIcon = (HICON)LoadImage(hInst, szIDIcon, IMAGE_ICON, cx, cy, LR_SHARED);
if(hIcon != NULL)
{
// Get the position of the edit box window
GetWindowRect(hWndChild, &rect);
pt.x = rect.left;
pt.y = rect.top;
ScreenToClient(hDlg, &pt);
pt.x = pt.x - cx - 4;
pt.y = pt.y + (rect.bottom - rect.top - cx) / 2;
// Create the window for icon
hWndBlink = CreateWindowEx(WS_EX_NOPARENTNOTIFY,
WC_STATIC,
NULL,
WS_CHILD | WS_VISIBLE | SS_ICON,
pt.x, pt.y, cx, cx,
hDlg,
NULL,
g_hInst,
NULL);
if(hWndBlink != NULL)
{
// Apply the icon to the icon window
SendMessage(hWndBlink, STM_SETICON, (WPARAM)hIcon, 0);
}
}
return hWndBlink;
}
static void CreateBlinkingIcon(HWND hDlg, HWND hWndChild, int nSeverity)
{
TFileTestData * pData = GetDialogData(hDlg);
// LPTSTR IconSet[] = { IDI_INFORMATION, IDI_ERROR, MAKEINTRESOURCE(IDI_ICON_WAIT)};
HWND hWndBlink;
UINT IconSet[] = {IDI_ICON_INFORMATION, IDI_ICON_ERROR, IDI_ICON_WAIT};
// If there is already a blinker timer, destroy it
DeleteBlinkTimer(hDlg);
// Create the icon, left-attached to the edit field
// Note that I tried the system icons, but they look like shit
// when reduced to small icon.
hWndBlink = AttachIconToEdit(hDlg, hWndChild, MAKEINTRESOURCE(IconSet[nSeverity]));
// Create timer for hiding the window
pData->BlinkTimer = SetTimer(hDlg, WM_TIMER_BLINK, 800, BlinkTimerProc);
pData->hWndBlink = hWndBlink;
}
void SetWindowEnumText(HWND hWndChild, LPCTSTR TextArray[], size_t nArraySize, size_t nIndex)
{
LPCTSTR szTextToSet = _T("");
// Sanity check
assert(hWndChild != NULL);
// Get the proper text message
if(nIndex < nArraySize)
szTextToSet = TextArray[nIndex];
// Set the text message
SetWindowText(hWndChild, szTextToSet);
}
int GetLastErrorSeverity(DWORD dwErrCode)
{
switch(dwErrCode)
{
case ERROR_SUCCESS:
return SEVERITY_SUCCESS;
case ERROR_IO_PENDING:
return SEVERITY_PENDING;
default:
return SEVERITY_ERROR;
}
}
void SetResultInfo(HWND hDlg, DWORD dwFlags, ...)
{
TCHAR szText[256];
HWND hWndChild;
va_list argList;
// Start the argument list
va_start(argList, dwFlags);
//
// 1) RSI_LAST_ERROR: DWORD dwErrCode
//
if(dwFlags & RSI_LAST_ERROR)
{
LPTSTR szError;
DWORD dwErrCode = va_arg(argList, DWORD);
// Set the title
if((hWndChild = GetDlgItem(hDlg, IDC_ERROR_CODE_TITLE)) != NULL)
SetWindowText(hWndChild, _T("GetLastError:"));
// Set the error text and the blinking icon
if((hWndChild = GetDlgItem(hDlg, IDC_ERROR_CODE)) != NULL)
{
// Create the error text (code + text)
szError = GetErrorText(dwErrCode);
if(szError != NULL)
{
StringCchPrintf(szText, _countof(szText), _T("(0x%08lX) %s"), dwErrCode, szError);
SetWindowText(hWndChild, szText);
delete [] szError;
}
// Create the blinking icon
CreateBlinkingIcon(hDlg, hWndChild, GetLastErrorSeverity(dwErrCode));
}
}
//
// 2) RSI_NTSTATUS: NTSTATUS Status
//
if(dwFlags & RSI_NTSTATUS)
{
LPCTSTR szStatus;
NTSTATUS Status = va_arg(argList, NTSTATUS);
int nSeverity;
// Set the title
if((hWndChild = GetDlgItem(hDlg, IDC_ERROR_CODE_TITLE)) != NULL)
SetWindowText(hWndChild, _T("Status:"));
// Set the error text and the blinking icon
if((hWndChild = GetDlgItem(hDlg, IDC_ERROR_CODE)) != NULL)
{
switch(Status)
{
case STATUS_PENDING:
szStatus = NtStatus2Text(Status);
nSeverity = SEVERITY_PENDING;
break;
case STATUS_INVALID_DATA_FORMAT:
szStatus = _T("The entered value has bad format.");
nSeverity = SEVERITY_ERROR;
break;
case STATUS_AUTO_CALCULATED:
szStatus = _T("This item is auto-calculated.");
nSeverity = SEVERITY_ERROR;
break;
case STATUS_CANNOT_EDIT_THIS:
szStatus = _T("This item is not editable.");
nSeverity = SEVERITY_ERROR;
break;
case STATUS_FILE_ID_CONVERSION:
szStatus = _T("Skipped conversion of File ID to an NT name.");
nSeverity = SEVERITY_SUCCESS;
break;
case STATUS_COPIED_TO_CLIPBOARD:
szStatus = _T("Item value copied to clipboard.");
nSeverity = SEVERITY_SUCCESS;
break;
default:
szStatus = NtStatus2Text(Status);
nSeverity = NT_SUCCESS(Status) ? SEVERITY_SUCCESS : SEVERITY_ERROR;
break;
}
// Set the status text and blinking icon
SetWindowText(hWndChild, szStatus);
CreateBlinkingIcon(hDlg, hWndChild, nSeverity);
}
}
//
// 3) RSI_HANDLE: HANDLE hHandle
//
if(dwFlags & RSI_HANDLE)
{
HANDLE hHandle = va_arg(argList, HANDLE);
BOOL bEnable = TRUE;
// Enable/disable the hint for close handle, if any
if((hWndChild = GetDlgItem(hDlg, IDC_CLOSE_HANDLE_HINT)) != NULL)
{
if(IsHandleInvalid(hHandle))