-
Notifications
You must be signed in to change notification settings - Fork 0
/
framewnd.c
1436 lines (1283 loc) · 44.2 KB
/
framewnd.c
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
/*
* Regedit frame window
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "regedit.h"
#include <commdlg.h>
#include <cderr.h>
#include <objsel.h>
/********************************************************************************
* Global and Local Variables:
*/
#define FAVORITES_MENU_POSITION 3
static WCHAR s_szFavoritesRegKey[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit\\Favorites";
static BOOL bInMenuLoop = FALSE; /* Tells us if we are in the menu loop */
extern WCHAR Suggestions[256];
/*******************************************************************************
* Local module support methods
*/
static void resize_frame_rect(HWND hWnd, PRECT prect)
{
if (IsWindowVisible(hStatusBar))
{
RECT rt;
SetupStatusBar(hWnd, TRUE);
GetWindowRect(hStatusBar, &rt);
prect->bottom -= rt.bottom - rt.top;
}
MoveWindow(g_pChildWnd->hWnd, prect->left, prect->top, prect->right, prect->bottom, TRUE);
}
static void resize_frame_client(HWND hWnd)
{
RECT rect;
GetClientRect(hWnd, &rect);
resize_frame_rect(hWnd, &rect);
}
/********************************************************************************/
static void OnInitMenu(HWND hWnd)
{
LONG lResult;
HKEY hKey = NULL;
DWORD dwIndex, cbValueName, cbValueData, dwType;
WCHAR szValueName[256];
BYTE abValueData[256];
static int s_nFavoriteMenuSubPos = -1;
HMENU hMenu;
BOOL bDisplayedAny = FALSE;
/* Find Favorites menu and clear it out */
hMenu = GetSubMenu(GetMenu(hWnd), FAVORITES_MENU_POSITION);
if (!hMenu)
goto done;
if (s_nFavoriteMenuSubPos < 0)
{
s_nFavoriteMenuSubPos = GetMenuItemCount(hMenu);
}
else
{
while(RemoveMenu(hMenu, s_nFavoriteMenuSubPos, MF_BYPOSITION)) ;
}
lResult = RegOpenKeyW(HKEY_CURRENT_USER, s_szFavoritesRegKey, &hKey);
if (lResult != ERROR_SUCCESS)
goto done;
dwIndex = 0;
do
{
cbValueName = ARRAY_SIZE(szValueName);
cbValueData = sizeof(abValueData);
lResult = RegEnumValueW(hKey, dwIndex, szValueName, &cbValueName, NULL, &dwType, abValueData, &cbValueData);
if ((lResult == ERROR_SUCCESS) && (dwType == REG_SZ))
{
if (!bDisplayedAny)
{
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
bDisplayedAny = TRUE;
}
AppendMenu(hMenu, 0, ID_FAVORITES_MIN + GetMenuItemCount(hMenu), szValueName);
}
dwIndex++;
}
while(lResult == ERROR_SUCCESS);
done:
if (hKey)
RegCloseKey(hKey);
}
static void OnEnterMenuLoop(HWND hWnd)
{
int nParts;
UNREFERENCED_PARAMETER(hWnd);
/* Update the status bar pane sizes */
nParts = -1;
SendMessageW(hStatusBar, SB_SETPARTS, 1, (LPARAM)&nParts);
bInMenuLoop = TRUE;
SendMessageW(hStatusBar, SB_SETTEXTW, (WPARAM)0, (LPARAM)L"");
}
static void OnExitMenuLoop(HWND hWnd)
{
bInMenuLoop = FALSE;
/* Update the status bar pane sizes*/
SetupStatusBar(hWnd, TRUE);
UpdateStatusBar();
}
static void OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
{
WCHAR str[100];
str[0] = UNICODE_NULL;
if (nFlags & MF_POPUP)
{
if (hSysMenu != GetMenu(hWnd))
{
if (nItemID == 2) nItemID = 5;
}
}
if (LoadStringW(hInst, nItemID, str, 100))
{
/* load appropriate string*/
LPWSTR lpsz = str;
/* first newline terminates actual string*/
lpsz = wcschr(lpsz, L'\n');
if (lpsz != NULL)
*lpsz = L'\0';
}
SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)str);
}
void SetupStatusBar(HWND hWnd, BOOL bResize)
{
RECT rc;
int nParts;
GetClientRect(hWnd, &rc);
nParts = rc.right;
/* nParts = -1;*/
if (bResize)
SendMessageW(hStatusBar, WM_SIZE, 0, 0);
SendMessageW(hStatusBar, SB_SETPARTS, 1, (LPARAM)&nParts);
}
void UpdateStatusBar(void)
{
HKEY hKeyRoot;
LPCWSTR pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)pszKeyPath);
}
static void toggle_child(HWND hWnd, UINT cmd, HWND hchild)
{
BOOL vis = IsWindowVisible(hchild);
HMENU hMenuView = GetSubMenu(hMenuFrame, ID_VIEW_MENU);
CheckMenuItem(hMenuView, cmd, vis?MF_BYCOMMAND:MF_BYCOMMAND|MF_CHECKED);
ShowWindow(hchild, vis?SW_HIDE:SW_SHOW);
resize_frame_client(hWnd);
}
static BOOL CheckCommDlgError(HWND hWnd)
{
DWORD dwErrorCode = CommDlgExtendedError();
UNREFERENCED_PARAMETER(hWnd);
switch (dwErrorCode)
{
case CDERR_DIALOGFAILURE:
break;
case CDERR_FINDRESFAILURE:
break;
case CDERR_NOHINSTANCE:
break;
case CDERR_INITIALIZATION:
break;
case CDERR_NOHOOK:
break;
case CDERR_LOCKRESFAILURE:
break;
case CDERR_NOTEMPLATE:
break;
case CDERR_LOADRESFAILURE:
break;
case CDERR_STRUCTSIZE:
break;
case CDERR_LOADSTRFAILURE:
break;
case FNERR_BUFFERTOOSMALL:
break;
case CDERR_MEMALLOCFAILURE:
break;
case FNERR_INVALIDFILENAME:
break;
case CDERR_MEMLOCKFAILURE:
break;
case FNERR_SUBCLASSFAILURE:
break;
default:
break;
}
return TRUE;
}
WCHAR FileNameBuffer[MAX_PATH];
typedef struct
{
UINT DisplayID;
UINT FilterID;
} FILTERPAIR, *PFILTERPAIR;
void
BuildFilterStrings(WCHAR *Filter, PFILTERPAIR Pairs, int PairCount)
{
int i, c;
c = 0;
for(i = 0; i < PairCount; i++)
{
c += LoadStringW(hInst, Pairs[i].DisplayID, &Filter[c], 255);
Filter[++c] = L'\0';
c += LoadStringW(hInst, Pairs[i].FilterID, &Filter[c], 255);
Filter[++c] = L'\0';
}
Filter[++c] = L'\0';
}
static BOOL InitOpenFileName(HWND hWnd, OPENFILENAME* pofn, BOOL bSave)
{
FILTERPAIR FilterPairs[5];
static WCHAR Filter[1024];
memset(pofn, 0, sizeof(OPENFILENAME));
pofn->lStructSize = sizeof(OPENFILENAME);
pofn->hwndOwner = hWnd;
pofn->hInstance = hInst;
/* create filter string */
FilterPairs[0].DisplayID = IDS_FLT_REGFILES;
FilterPairs[0].FilterID = IDS_FLT_REGFILES_FLT;
FilterPairs[1].DisplayID = IDS_FLT_HIVFILES;
FilterPairs[1].FilterID = IDS_FLT_HIVFILES_FLT;
FilterPairs[2].DisplayID = IDS_FLT_REGEDIT4;
FilterPairs[2].FilterID = IDS_FLT_REGEDIT4_FLT;
if (bSave)
{
FilterPairs[3].DisplayID = IDS_FLT_TXTFILES;
FilterPairs[3].FilterID = IDS_FLT_TXTFILES_FLT;
FilterPairs[4].DisplayID = IDS_FLT_ALLFILES;
FilterPairs[4].FilterID = IDS_FLT_ALLFILES_FLT;
}
else
{
FilterPairs[3].DisplayID = IDS_FLT_ALLFILES;
FilterPairs[3].FilterID = IDS_FLT_ALLFILES_FLT;
}
BuildFilterStrings(Filter, FilterPairs, ARRAY_SIZE(FilterPairs) - !bSave);
pofn->lpstrFilter = Filter;
pofn->lpstrFile = FileNameBuffer;
pofn->nMaxFile = _countof(FileNameBuffer);
pofn->Flags = OFN_EXPLORER | OFN_HIDEREADONLY;
pofn->lpstrDefExt = L"reg";
if (bSave)
pofn->Flags |= OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
else
pofn->Flags |= OFN_FILEMUSTEXIST;
return TRUE;
}
#define LOADHIVE_KEYNAMELENGTH 128
static INT_PTR CALLBACK LoadHive_KeyNameInHookProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static LPWSTR sKey = NULL;
switch(uMsg)
{
case WM_INITDIALOG:
sKey = (LPWSTR)lParam;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
if(GetDlgItemTextW(hWndDlg, IDC_EDIT_KEY, sKey, LOADHIVE_KEYNAMELENGTH))
return EndDialog(hWndDlg, -1);
else
return EndDialog(hWndDlg, 0);
case IDCANCEL:
return EndDialog(hWndDlg, 0);
}
break;
}
return FALSE;
}
static BOOL EnablePrivilege(LPCWSTR lpszPrivilegeName, LPCWSTR lpszSystemName, BOOL bEnablePrivilege)
{
BOOL bRet = FALSE;
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
if (LookupPrivilegeValueW(lpszSystemName,
lpszPrivilegeName,
&tp.Privileges[0].Luid))
{
bRet = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
bRet = FALSE;
}
CloseHandle(hToken);
}
return bRet;
}
static BOOL LoadHive(HWND hWnd)
{
OPENFILENAME ofn;
WCHAR Caption[128];
LPCWSTR pszKeyPath;
WCHAR xPath[LOADHIVE_KEYNAMELENGTH];
HKEY hRootKey;
WCHAR Filter[1024];
FILTERPAIR filter;
/* get the item key to load the hive in */
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
/* initialize the "open file" dialog */
InitOpenFileName(hWnd, &ofn, FALSE);
/* build the "All Files" filter up */
filter.DisplayID = IDS_FLT_ALLFILES;
filter.FilterID = IDS_FLT_ALLFILES_FLT;
BuildFilterStrings(Filter, &filter, 1);
ofn.lpstrFilter = Filter;
/* load and set the caption and flags for dialog */
LoadStringW(hInst, IDS_LOAD_HIVE, Caption, ARRAY_SIZE(Caption));
ofn.lpstrTitle = Caption;
ofn.Flags |= OFN_ENABLESIZING;
/* now load the hive */
if (GetOpenFileName(&ofn))
{
if (DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_LOADHIVE), hWnd,
&LoadHive_KeyNameInHookProc, (LPARAM)xPath))
{
LONG regLoadResult;
/* Enable the 'restore' privilege, load the hive, disable the privilege */
EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
regLoadResult = RegLoadKeyW(hRootKey, xPath, ofn.lpstrFile);
EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);
if(regLoadResult == ERROR_SUCCESS)
{
/* refresh tree and list views */
RefreshTreeView(g_pChildWnd->hTreeWnd);
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath, TRUE);
}
else
{
ErrorMessageBox(hWnd, Caption, regLoadResult);
return FALSE;
}
}
}
else
{
CheckCommDlgError(hWnd);
}
return TRUE;
}
static BOOL UnloadHive(HWND hWnd)
{
WCHAR Caption[128];
LPCWSTR pszKeyPath;
HKEY hRootKey;
LONG regUnloadResult;
/* get the item key to unload */
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
/* load and set the caption and flags for dialog */
LoadStringW(hInst, IDS_UNLOAD_HIVE, Caption, ARRAY_SIZE(Caption));
/* Enable the 'restore' privilege, unload the hive, disable the privilege */
EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
regUnloadResult = RegUnLoadKeyW(hRootKey, pszKeyPath);
EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);
if(regUnloadResult == ERROR_SUCCESS)
{
/* refresh tree and list views */
RefreshTreeView(g_pChildWnd->hTreeWnd);
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath, TRUE);
}
else
{
ErrorMessageBox(hWnd, Caption, regUnloadResult);
return FALSE;
}
return TRUE;
}
static BOOL ImportRegistryFile(HWND hWnd)
{
BOOL bRet = FALSE;
OPENFILENAME ofn;
WCHAR Caption[128], szTitle[512], szText[512];
HKEY hKeyRoot;
LPCWSTR pszKeyPath;
/* Figure out in which key path we are importing */
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
InitOpenFileName(hWnd, &ofn, FALSE);
LoadStringW(hInst, IDS_IMPORT_REG_FILE, Caption, ARRAY_SIZE(Caption));
ofn.lpstrTitle = Caption;
ofn.Flags |= OFN_ENABLESIZING;
if (GetOpenFileName(&ofn))
{
/* Look at the extension of the file to determine its type */
if (ofn.nFileExtension >= 1 &&
_wcsicmp(ofn.lpstrFile + ofn.nFileExtension, L"reg") == 0) /* REGEDIT4 or Windows Registry Editor Version 5.00 */
{
/* Open the file */
FILE* fp = _wfopen(ofn.lpstrFile, L"rb");
/* Import it */
if (fp == NULL || !import_registry_file(fp))
{
/* Error opening the file */
LoadStringW(hInst, IDS_APP_TITLE, szTitle, ARRAY_SIZE(szTitle));
LoadStringW(hInst, IDS_IMPORT_ERROR, szText, ARRAY_SIZE(szText));
InfoMessageBox(hWnd, MB_OK | MB_ICONERROR, szTitle, szText, ofn.lpstrFile);
bRet = FALSE;
}
else
{
/* Show successful import */
LoadStringW(hInst, IDS_APP_TITLE, szTitle, ARRAY_SIZE(szTitle));
LoadStringW(hInst, IDS_IMPORT_OK, szText, ARRAY_SIZE(szText));
InfoMessageBox(hWnd, MB_OK | MB_ICONINFORMATION, szTitle, szText, ofn.lpstrFile);
bRet = TRUE;
}
/* Close the file */
if (fp) fclose(fp);
}
else /* Registry Hive Files */
{
LoadStringW(hInst, IDS_QUERY_IMPORT_HIVE_CAPTION, szTitle, ARRAY_SIZE(szTitle));
LoadStringW(hInst, IDS_QUERY_IMPORT_HIVE_MSG, szText, ARRAY_SIZE(szText));
/* Display a confirmation message */
if (MessageBoxW(g_pChildWnd->hWnd, szText, szTitle, MB_ICONWARNING | MB_YESNO) == IDYES)
{
LONG lResult;
HKEY hSubKey;
/* Open the subkey */
lResult = RegOpenKeyExW(hKeyRoot, pszKeyPath, 0, KEY_WRITE, &hSubKey);
if (lResult == ERROR_SUCCESS)
{
/* Enable the 'restore' privilege, restore the hive then disable the privilege */
EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
lResult = RegRestoreKey(hSubKey, ofn.lpstrFile, REG_FORCE_RESTORE);
EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);
/* Flush the subkey and close it */
RegFlushKey(hSubKey);
RegCloseKey(hSubKey);
}
/* Set the return value */
bRet = (lResult == ERROR_SUCCESS);
/* Display error, if any */
if (!bRet) ErrorMessageBox(hWnd, Caption, lResult);
}
}
}
else
{
CheckCommDlgError(hWnd);
}
/* refresh tree and list views */
RefreshTreeView(g_pChildWnd->hTreeWnd);
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
RefreshListView(g_pChildWnd->hListWnd, hKeyRoot, pszKeyPath, TRUE);
return bRet;
}
static UINT_PTR CALLBACK ExportRegistryFile_OFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
HWND hwndExportAll;
HWND hwndExportBranch;
HWND hwndExportBranchText;
UINT_PTR iResult = 0;
OPENFILENAME *pOfn;
LPWSTR pszSelectedKey;
OFNOTIFY *pOfnNotify;
UNREFERENCED_PARAMETER(wParam);
switch(uiMsg)
{
case WM_INITDIALOG:
pOfn = (OPENFILENAME *) lParam;
pszSelectedKey = (LPWSTR) pOfn->lCustData;
hwndExportAll = GetDlgItem(hdlg, IDC_EXPORT_ALL);
if (hwndExportAll)
SendMessageW(hwndExportAll, BM_SETCHECK, pszSelectedKey ? BST_UNCHECKED : BST_CHECKED, 0);
hwndExportBranch = GetDlgItem(hdlg, IDC_EXPORT_BRANCH);
if (hwndExportBranch)
SendMessageW(hwndExportBranch, BM_SETCHECK, pszSelectedKey ? BST_CHECKED : BST_UNCHECKED, 0);
hwndExportBranchText = GetDlgItem(hdlg, IDC_EXPORT_BRANCH_TEXT);
if (hwndExportBranchText)
SetWindowTextW(hwndExportBranchText, pszSelectedKey);
break;
case WM_NOTIFY:
if (((NMHDR *) lParam)->code == CDN_FILEOK)
{
pOfnNotify = (OFNOTIFY *) lParam;
pszSelectedKey = (LPWSTR) pOfnNotify->lpOFN->lCustData;
hwndExportBranch = GetDlgItem(hdlg, IDC_EXPORT_BRANCH);
hwndExportBranchText = GetDlgItem(hdlg, IDC_EXPORT_BRANCH_TEXT);
if (hwndExportBranch && hwndExportBranchText
&& (SendMessageW(hwndExportBranch, BM_GETCHECK, 0, 0) == BST_CHECKED))
{
GetWindowTextW(hwndExportBranchText, pszSelectedKey, _MAX_PATH);
}
else if (pszSelectedKey)
{
pszSelectedKey[0] = L'\0';
}
}
break;
}
return iResult;
}
BOOL ExportRegistryFile(HWND hWnd)
{
BOOL bRet = FALSE;
OPENFILENAME ofn;
WCHAR ExportKeyPath[_MAX_PATH] = {0};
WCHAR Caption[128], szTitle[512], szText[512];
HKEY hKeyRoot;
LPCWSTR pszKeyPath;
/* Figure out which key path we are exporting */
pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
GetKeyName(ExportKeyPath, ARRAY_SIZE(ExportKeyPath), hKeyRoot, pszKeyPath);
InitOpenFileName(hWnd, &ofn, TRUE);
LoadStringW(hInst, IDS_EXPORT_REG_FILE, Caption, ARRAY_SIZE(Caption));
ofn.lpstrTitle = Caption;
/* Only set the path if a key (not the root node) is selected */
if (hKeyRoot != 0)
{
ofn.lCustData = (LPARAM) ExportKeyPath;
}
ofn.Flags |= OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
ofn.lpfnHook = ExportRegistryFile_OFNHookProc;
ofn.lpTemplateName = MAKEINTRESOURCEW(IDD_EXPORTRANGE);
if (GetSaveFileName(&ofn))
{
switch (ofn.nFilterIndex)
{
case 2: /* Registry Hive Files */
{
LONG lResult;
HKEY hSubKey;
/* Open the subkey */
lResult = RegOpenKeyExW(hKeyRoot, pszKeyPath, 0, KEY_READ, &hSubKey);
if (lResult == ERROR_SUCCESS)
{
/* Enable the 'backup' privilege, save the hive then disable the privilege */
EnablePrivilege(SE_BACKUP_NAME, NULL, TRUE);
lResult = RegSaveKeyW(hSubKey, ofn.lpstrFile, NULL);
if (lResult == ERROR_ALREADY_EXISTS)
{
/*
* We are here, that means that we already said "yes" to the confirmation dialog.
* So we absolutely want to replace the hive file.
*/
if (DeleteFileW(ofn.lpstrFile))
{
/* Try again */
lResult = RegSaveKeyW(hSubKey, ofn.lpstrFile, NULL);
}
}
EnablePrivilege(SE_BACKUP_NAME, NULL, FALSE);
if (lResult != ERROR_SUCCESS)
{
/*
* If we are here, it's because RegSaveKeyW has failed for any reason.
* The problem is that even if it has failed, it has created or
* replaced the exported hive file with a new empty file. We don't
* want to keep this file, so we delete it.
*/
DeleteFileW(ofn.lpstrFile);
}
/* Close the subkey */
RegCloseKey(hSubKey);
}
/* Set the return value */
bRet = (lResult == ERROR_SUCCESS);
/* Display error, if any */
if (!bRet) ErrorMessageBox(hWnd, Caption, lResult);
break;
}
case 1: /* Windows Registry Editor Version 5.00 */
case 3: /* REGEDIT4 */
default: /* All files ==> use Windows Registry Editor Version 5.00 */
{
if (!export_registry_key(ofn.lpstrFile, ExportKeyPath,
(ofn.nFilterIndex == 3 ? REG_FORMAT_4
: REG_FORMAT_5)))
{
/* Error creating the file */
LoadStringW(hInst, IDS_APP_TITLE, szTitle, ARRAY_SIZE(szTitle));
LoadStringW(hInst, IDS_EXPORT_ERROR, szText, ARRAY_SIZE(szText));
InfoMessageBox(hWnd, MB_OK | MB_ICONERROR, szTitle, szText, ofn.lpstrFile);
bRet = FALSE;
}
else
{
bRet = TRUE;
}
break;
}
case 4: /* Text File */
{
bRet = txt_export_registry_key(ofn.lpstrFile, ExportKeyPath);
if (!bRet)
{
/* Error creating the file */
LoadStringW(hInst, IDS_APP_TITLE, szTitle, ARRAY_SIZE(szTitle));
LoadStringW(hInst, IDS_EXPORT_ERROR, szText, ARRAY_SIZE(szText));
InfoMessageBox(hWnd, MB_OK | MB_ICONERROR, szTitle, szText, ofn.lpstrFile);
}
break;
}
}
}
else
{
CheckCommDlgError(hWnd);
}
return bRet;
}
BOOL PrintRegistryHive(HWND hWnd, LPWSTR path)
{
#if 1
PRINTDLG pd;
UNREFERENCED_PARAMETER(path);
ZeroMemory(&pd, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hWnd;
pd.hDevMode = NULL; /* Don't forget to free or store hDevMode*/
pd.hDevNames = NULL; /* Don't forget to free or store hDevNames*/
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
if (PrintDlg(&pd))
{
/* GDI calls to render output. */
DeleteDC(pd.hDC); /* Delete DC when done.*/
}
#else
HRESULT hResult;
PRINTDLGEX pd;
hResult = PrintDlgEx(&pd);
if (hResult == S_OK)
{
switch (pd.dwResultAction)
{
case PD_RESULT_APPLY:
/*The user clicked the Apply button and later clicked the Cancel button. This indicates that the user wants to apply the changes made in the property sheet, but does not yet want to print. The PRINTDLGEX structure contains the information specified by the user at the time the Apply button was clicked. */
break;
case PD_RESULT_CANCEL:
/*The user clicked the Cancel button. The information in the PRINTDLGEX structure is unchanged. */
break;
case PD_RESULT_PRINT:
/*The user clicked the Print button. The PRINTDLGEX structure contains the information specified by the user. */
break;
default:
break;
}
}
else
{
switch (hResult)
{
case E_OUTOFMEMORY:
/*Insufficient memory. */
break;
case E_INVALIDARG:
/* One or more arguments are invalid. */
break;
case E_POINTER:
/*Invalid pointer. */
break;
case E_HANDLE:
/*Invalid handle. */
break;
case E_FAIL:
/*Unspecified error. */
break;
default:
break;
}
return FALSE;
}
#endif
return TRUE;
}
static void ChooseFavorite(LPCWSTR pszFavorite)
{
HKEY hKey = NULL;
WCHAR szFavoritePath[512];
DWORD cbData, dwType;
if (RegOpenKeyExW(HKEY_CURRENT_USER, s_szFavoritesRegKey, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
goto done;
cbData = sizeof(szFavoritePath);
memset(szFavoritePath, 0, sizeof(szFavoritePath));
if (RegQueryValueExW(hKey, pszFavorite, NULL, &dwType, (LPBYTE) szFavoritePath, &cbData) != ERROR_SUCCESS)
goto done;
if (dwType == REG_SZ)
SelectNode(g_pChildWnd->hTreeWnd, szFavoritePath);
done:
if (hKey)
RegCloseKey(hKey);
}
BOOL CopyKeyName(HWND hWnd, HKEY hRootKey, LPCWSTR keyName)
{
BOOL bClipboardOpened = FALSE;
BOOL bSuccess = FALSE;
WCHAR szBuffer[512];
HGLOBAL hGlobal;
LPWSTR s;
SIZE_T cbGlobal;
if (!OpenClipboard(hWnd))
goto done;
bClipboardOpened = TRUE;
if (!EmptyClipboard())
goto done;
if (!GetKeyName(szBuffer, ARRAY_SIZE(szBuffer), hRootKey, keyName))
goto done;
cbGlobal = (wcslen(szBuffer) + 1) * sizeof(WCHAR);
hGlobal = GlobalAlloc(GMEM_MOVEABLE, cbGlobal);
if (!hGlobal)
goto done;
s = GlobalLock(hGlobal);
StringCbCopyW(s, cbGlobal, szBuffer);
GlobalUnlock(hGlobal);
SetClipboardData(CF_UNICODETEXT, hGlobal);
bSuccess = TRUE;
done:
if (bClipboardOpened)
CloseClipboard();
return bSuccess;
}
static BOOL CreateNewValue(HKEY hRootKey, LPCWSTR pszKeyPath, DWORD dwType)
{
WCHAR szNewValueFormat[128];
WCHAR szNewValue[128];
int iIndex = 1;
BYTE data[128];
DWORD dwExistingType, cbData;
LONG lResult;
HKEY hKey;
LVFINDINFO lvfi;
if (RegOpenKeyExW(hRootKey, pszKeyPath, 0, KEY_QUERY_VALUE | KEY_SET_VALUE,
&hKey) != ERROR_SUCCESS)
return FALSE;
LoadStringW(hInst, IDS_NEW_VALUE, szNewValueFormat, ARRAY_SIZE(szNewValueFormat));
do
{
wsprintf(szNewValue, szNewValueFormat, iIndex++);
cbData = sizeof(data);
lResult = RegQueryValueExW(hKey, szNewValue, NULL, &dwExistingType, data, &cbData);
}
while(lResult == ERROR_SUCCESS);
switch(dwType)
{
case REG_DWORD:
cbData = sizeof(DWORD);
break;
case REG_SZ:
case REG_EXPAND_SZ:
cbData = sizeof(WCHAR);
break;
case REG_MULTI_SZ:
/*
* WARNING: An empty multi-string has only one null char.
* Indeed, multi-strings are built in the following form:
* str1\0str2\0...strN\0\0
* where each strI\0 is a null-terminated string, and it
* ends with a terminating empty string.
* Therefore an empty multi-string contains only the terminating
* empty string, that is, one null char.
*/
cbData = sizeof(WCHAR);
break;
case REG_QWORD: /* REG_QWORD_LITTLE_ENDIAN */
cbData = sizeof(DWORDLONG); // == sizeof(DWORD) * 2;
break;
default:
cbData = 0;
break;
}
memset(data, 0, cbData);
lResult = RegSetValueExW(hKey, szNewValue, 0, dwType, data, cbData);
RegCloseKey(hKey);
if (lResult != ERROR_SUCCESS)
{
return FALSE;
}
RefreshListView(g_pChildWnd->hListWnd, hRootKey, pszKeyPath, TRUE);
/* locate the newly added value, and get ready to rename it */
memset(&lvfi, 0, sizeof(lvfi));
lvfi.flags = LVFI_STRING;
lvfi.psz = szNewValue;
iIndex = ListView_FindItem(g_pChildWnd->hListWnd, -1, &lvfi);
if (iIndex >= 0)
{
ListView_SetItemState(g_pChildWnd->hListWnd, iIndex,
LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
ListView_EnsureVisible(g_pChildWnd->hListWnd, iIndex, FALSE);
(void)ListView_EditLabel(g_pChildWnd->hListWnd, iIndex);
}
return TRUE;
}
static HRESULT
InitializeRemoteRegistryPicker(OUT IDsObjectPicker **pDsObjectPicker)
{
HRESULT hRet;
*pDsObjectPicker = NULL;
hRet = CoCreateInstance(&CLSID_DsObjectPicker,
NULL,
CLSCTX_INPROC_SERVER,
&IID_IDsObjectPicker,
(LPVOID*)pDsObjectPicker);
if (SUCCEEDED(hRet))
{
DSOP_INIT_INFO InitInfo;
static DSOP_SCOPE_INIT_INFO Scopes[] =
{
{
sizeof(DSOP_SCOPE_INIT_INFO),
DSOP_SCOPE_TYPE_USER_ENTERED_UPLEVEL_SCOPE | DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE |
DSOP_SCOPE_TYPE_GLOBAL_CATALOG | DSOP_SCOPE_TYPE_EXTERNAL_UPLEVEL_DOMAIN |
DSOP_SCOPE_TYPE_EXTERNAL_DOWNLEVEL_DOMAIN | DSOP_SCOPE_TYPE_WORKGROUP |
DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN,
0,
{
{
DSOP_FILTER_COMPUTERS,
0,
0
},
DSOP_DOWNLEVEL_FILTER_COMPUTERS
},
NULL,
NULL,
S_OK
},
};
InitInfo.cbSize = sizeof(InitInfo);
InitInfo.pwzTargetComputer = NULL;
InitInfo.cDsScopeInfos = ARRAY_SIZE(Scopes);
InitInfo.aDsScopeInfos = Scopes;
InitInfo.flOptions = 0;
InitInfo.cAttributesToFetch = 0;
InitInfo.apwzAttributeNames = NULL;
hRet = (*pDsObjectPicker)->lpVtbl->Initialize(*pDsObjectPicker,
&InitInfo);
if (FAILED(hRet))
{
/* delete the object picker in case initialization failed! */
(*pDsObjectPicker)->lpVtbl->Release(*pDsObjectPicker);
}
}
return hRet;
}
static HRESULT
InvokeRemoteRegistryPickerDialog(IN IDsObjectPicker *pDsObjectPicker,
IN HWND hwndParent OPTIONAL,
OUT LPWSTR lpBuffer,
IN UINT uSize)
{
IDataObject *pdo = NULL;
HRESULT hRet;
hRet = pDsObjectPicker->lpVtbl->InvokeDialog(pDsObjectPicker,
hwndParent,
&pdo);
if (hRet == S_OK)
{
STGMEDIUM stm;
FORMATETC fe;