-
Notifications
You must be signed in to change notification settings - Fork 5
/
pmgseg.c
3410 lines (2790 loc) · 91.2 KB
/
pmgseg.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
/****************************************************************************/
/* */
/* PMGSEG.C - */
/* */
/* Program Manager Group Handling Routines */
/* */
/****************************************************************************/
#include <Shlwapi.h>
#include "progman.h"
#include "dde.h"
#include "convgrp.h"
#include "pmanfunc.h"
#define WORD_MIN -32767
#define WORD_MAX 32767
#ifndef ORGCODE
// #include "fcntl.h"
// #include "io.h"
// #include "stdio.h"
// #include <tchar.h>
#define S_IREAD 0000400 /* read permission, owner */
#define S_IWRITE 0000200 /* write permission, owner */
#endif
BOOL fFirstLoad = FALSE;
extern BOOL bHandleProgramGroupsEvent;
#if 0
// DOS apps are no longer set to fullscreen by default in progman
// 5-3-93 johannec (bug 8343)
#ifdef i386
BOOL IsDOSApplication(LPTSTR lpPath);
BOOL SetDOSApplicationToFullScreen(LPTSTR lpTitle);
#endif
#endif
void NEAR PASCAL RemoveItemFromList(PGROUP pGroup, PITEM pItem)
// Removes a PITEM from the list.
{
PITEM *ppItem;
/* Cause it to be repainted later. */
InvalidateIcon(pGroup, pItem);
if (pItem == pGroup->pItems) {
/*
* first one in list, must invalidate next one so it paints an active
* title bar.
*/
InvalidateIcon(pGroup,pItem->pNext);
}
/* Remove it from the list. */
for (ppItem = &pGroup->pItems;*ppItem != pItem;
ppItem = &((*ppItem)->pNext));
*ppItem = pItem->pNext;
/* Lastly free up the memory. */
LocalFree((HANDLE)pItem);
}
#ifdef DEBUG
void NEAR PASCAL CheckBeforeReAlloc(HANDLE h)
{
TCHAR buf[100];
if ((BYTE)GlobalFlags(h)) {
wsprintf(buf, TEXT("LockCount before realloc %d\r\n"), (BYTE)GlobalFlags(h));
OutputDebugString(buf);
DbgBreakPoint();
}
}
#else
#define CheckBeforeReAlloc(h)
#endif
#ifdef PARANOID
/*--------------------------------------------------------------------------*/
/* */
/* CheckRange() - */
/* */
/*--------------------------------------------------------------------------*/
void PASCAL CheckRange(
LPGROUPDEF lpgd,
LPTSTR lp1,
WORD *lpw1,
WORD cb1,
LPTSTR lp2,
WORD w2,
WORD cb2,
LPTSTR lpThing)
{
WORD w1 = *lpw1;
WORD e1, e2;
if (!w1 || (w1 == w2)) {
return;
}
if (!cb1) {
cb1 = (WORD)lstrlen((LPTSTR) PTR(lpgd, *lpw1));
}
e1 = w1 + cb1;
e2 = w2 + cb2;
if ((w1 < e2) && (w2 < e1)) {
KdPrint(("ERROR: %s overlaps %s in %s!!!!\r\n",lp2,lp1,lpThing));
}
}
/*--------------------------------------------------------------------------*/
/* */
/* CheckPointer() - */
/* */
/*--------------------------------------------------------------------------*/
void PASCAL CheckPointer(
LPGROUPDEF lpgd,
LPTSTR lp,
WORD *lpw,
WORD cb,
WORD limit)
{
LPITEMDEF lpid;
int i;
if (lpw == NULL || !*lpw) {
KdPrint(("Warning: %s is NULL\r\n", lp));
DebugBreak();
}
if (!cb) {
cb = lstrlen((LPTSTR) PTR(lpgd, *lpw));
}
if (*lpw + cb > limit) {
KdPrint(("ERROR: %s runs off end of group\r\n", lp));
return;
}
}
/*--------------------------------------------------------------------------*/
/* */
/* VerifyGroup() - */
/* */
/*--------------------------------------------------------------------------*/
void PASCAL VerifyGroup(
LPGROUPDEF lpgd)
{
int i;
LPITEMDEF lpid;
DWORD limit = lpgd->cbGroup;
KdPrint(("\r\nChecking Group %s\r\n",(LPTSTR) PTR(lpgd, lpgd->pName)));
CheckPointer(lpgd, TEXT("Group Name"), &lpgd->pName, 0, limit);
for (i = 0; i < (int)lpgd->cItems; i++) {
if (!lpgd->rgiItems[i]) {
continue;
}
lpid = ITEM(lpgd, i);
KdPrint(("Checking item %d at %4.4X (%s):\r\n", i, lpgd->rgiItems[i],
(LPTSTR) PTR(lpgd, lpid->pName)));
CheckPointer(lpgd, TEXT("Itemdef"), lpgd->rgiItems + i, sizeof(ITEMDEF), limit);
CheckPointer(lpgd, TEXT("Item name"), &lpid->pName, 0, limit);
CheckPointer(lpgd, TEXT("item command"), &lpid->pCommand, 0, limit);
CheckPointer(lpgd, TEXT("item icon path"), &lpid->pIconPath, 0, limit);
}
}
#endif
/*--------------------------------------------------------------------------*/
/* */
/* IsGroupReadOnly() - */
/* */
/*--------------------------------------------------------------------------*/
BOOL FAR PASCAL IsGroupReadOnly(LPTSTR szGroupKey, BOOL bCommonGroup)
{
HKEY hkey;
HKEY hkeyGroups;
if (bCommonGroup)
hkeyGroups = hkeyCommonGroups;
else if (bUseANSIGroups)
hkeyGroups = hkeyAnsiProgramGroups;
else
hkeyGroups = hkeyProgramGroups;
if (!hkeyGroups)
return(FALSE);
if (!RegOpenKeyEx(hkeyGroups, szGroupKey, 0, DELETE | KEY_READ | KEY_WRITE, &hkey)){
RegCloseKey(hkey);
return(FALSE);
}
return(TRUE);
}
/*--------------------------------------------------------------------------*/
/* */
/* GroupCheck() - */
/* */
/*--------------------------------------------------------------------------*/
BOOL FAR PASCAL GroupCheck(PGROUP pGroup)
{
if (!fExiting && IsGroupReadOnly(pGroup->lpKey, pGroup->fCommon)) {
pGroup->fRO = TRUE;
return FALSE;
}
pGroup->fRO = FALSE;
return TRUE;
}
/*--------------------------------------------------------------------------*/
/* */
/* MyDwordAlign() - */
/* */
/*--------------------------------------------------------------------------*/
INT MyDwordAlign(INT wStrLen)
{
return ((wStrLen + 3) & ~3);
}
/*--------------------------------------------------------------------------*/
/* */
/* SizeofGroup() - */
/* */
/*--------------------------------------------------------------------------*/
DWORD PASCAL SizeofGroup(LPGROUPDEF lpgd)
{
LPPMTAG lptag;
DWORD cbSeg;
DWORD cb;
cbSeg = (DWORD)GlobalSize(lpgd);
lptag = (LPPMTAG)((LPSTR)lpgd+lpgd->cbGroup);
if ((DWORD)((PCHAR)lptag - (PCHAR)lpgd +MyDwordAlign(sizeof(PMTAG))-MyDwordAlign(sizeof(lptag->rgb))+4) <= cbSeg
&& lptag->wID == ID_MAGIC
&& lptag->wItem == (int)0xFFFF
&& lptag->cb == (WORD)(MyDwordAlign(sizeof(PMTAG))-MyDwordAlign(sizeof(lptag->rgb)) + 4)
&& *(PLONG)lptag->rgb == PMTAG_MAGIC)
{
while ((cb = (DWORD)((PCHAR)lptag - (PCHAR)lpgd + MyDwordAlign(sizeof(PMTAG))-MyDwordAlign(sizeof(lptag->rgb)))) <= cbSeg)
{
if (lptag->wID == ID_LASTTAG)
return cb;
(LPSTR)lptag += lptag->cb;
}
}
return lpgd->cbGroup;
}
/*--------------------------------------------------------------------------*/
/* */
/* LockGroup() - */
/* */
/*--------------------------------------------------------------------------*/
/* Given the handle to the group's window, lock the group segment and return
* a pointer thereto. Reloads the group segment if it is not in memory.
*/
LPGROUPDEF FAR PASCAL LockGroup(HWND hwndGroup)
{
PGROUP pGroup;
LPGROUPDEF lpgd;
WORD status;
LPTSTR lpszKey;
HKEY hKey = NULL;
LONG err;
DWORD cbMaxValueLen = 0;
FILETIME ft;
TCHAR szClass[64];
DWORD dummy = 64;
DWORD cbSecDesc;
HKEY hkeyGroups;
BOOL bCommonGroup;
wLockError = 0; // No errors.
/* Find the handle and try to lock it. */
pGroup = (PGROUP)GetWindowLongPtr(hwndGroup, GWLP_PGROUP);
lpgd = (LPGROUPDEF)GlobalLock(pGroup->hGroup);
/* If we got a non-NULL selector, return the pointer. */
if (pGroup->fLoaded)
return(lpgd);
if (lpgd) {
GlobalUnlock(pGroup->hGroup);
}
NukeIconBitmap(pGroup); // invalidate the bitmap
/* The group has been discarded, must reread the file... */
lpszKey = pGroup->lpKey;
pGroup->fRO = FALSE;
bCommonGroup = pGroup->fCommon;
if (bCommonGroup)
hkeyGroups = hkeyCommonGroups;
else if (bUseANSIGroups)
hkeyGroups = hkeyAnsiProgramGroups;
else
hkeyGroups = hkeyProgramGroups;
if (!hkeyGroups)
goto RegError;
/* Try to open the group key. */
if (err = RegOpenKeyEx(hkeyGroups, lpszKey, 0,
DELETE | KEY_READ | KEY_WRITE,
&hKey)) {
/* Try read-only access */
if (err = RegOpenKeyEx(hkeyGroups, lpszKey, 0,
KEY_READ, &hKey) || !hKey) {
status = IDS_NOGRPFILE;
goto LGError1;
}
if (!bUseANSIGroups) {
pGroup->fRO = TRUE;
}
}
if (!(err = RegQueryInfoKey(hKey,
szClass,
&dummy, // cbClass
NULL, // Title index
&dummy, // cbSubKeys
&dummy, // cb Max subkey length
&dummy, // max class len
&dummy, // values count
&dummy, // max value name length
&cbMaxValueLen,
&cbSecDesc, // cb Security Descriptor
&ft))) {
if (!pGroup->ftLastWriteTime.dwLowDateTime &&
!pGroup->ftLastWriteTime.dwHighDateTime)
pGroup->ftLastWriteTime = ft;
else if (pGroup->ftLastWriteTime.dwLowDateTime != ft.dwLowDateTime ||
pGroup->ftLastWriteTime.dwHighDateTime != ft.dwHighDateTime ) {
wLockError = LOCK_FILECHANGED;
status = IDS_GRPHASCHANGED;
if (!fExiting) // Don't reload changed groups on exit.
PostMessage(hwndProgman,WM_RELOADGROUP,(WPARAM)pGroup,0L);
goto LGError2;
}
}
/* Find the size of the file by seeking to the end. */
if (cbMaxValueLen < sizeof(GROUPDEF)) {
status = IDS_BADFILE;
goto LGError2;
}
/* Allocate some memory for the thing. */
CheckBeforeReAlloc(pGroup->hGroup);
if (!GlobalReAlloc(pGroup->hGroup, (DWORD)cbMaxValueLen, GMEM_MOVEABLE)) {
wLockError = LOCK_LOWMEM;
status = IDS_LOWMEM;
lpszKey = NULL;
goto LGError2;
}
pGroup->fLoaded = TRUE;
lpgd = (LPGROUPDEF)GlobalLock(pGroup->hGroup);
/* Read the whole group data into memory. */
status = IDS_BADFILE;
if (err = RegQueryValueEx(hKey, NULL, 0, 0, (LPBYTE)lpgd, &cbMaxValueLen)) {
goto LGError3;
}
//
// If we start out from the ANSI groups, we need the security description
// to copy the entire information to the UNICODE groups
//
if (bUseANSIGroups) {
pGroup->pSecDesc = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, cbSecDesc);
RegGetKeySecurity(hKey, DACL_SECURITY_INFORMATION, pGroup->pSecDesc, &cbSecDesc);
}
else {
pGroup->pSecDesc = NULL;
}
//
// If we loaded an old format ANSI group, then convert it to the
// UNICODE format and save it back in the registry.
//
if (lpgd->dwMagic == GROUP_MAGIC) {
HANDLE hUNIGroup;
if (cbMaxValueLen = ConvertToUnicodeGroup((LPGROUPDEF_A)lpgd, &hUNIGroup)) {
UnlockGroup(hwndGroup);
/* Free the ANSI group. */
GlobalFree(pGroup->hGroup);
pGroup->hGroup = hUNIGroup;
lpgd = (LPGROUPDEF)GlobalLock(pGroup->hGroup);
}
else {
goto LGError3;
}
}
if (lpgd->dwMagic != GROUP_UNICODE)
goto LGError3;
if (lpgd->cbGroup > cbMaxValueLen)
goto LGError3;
/* Now return the pointer. */
RegCloseKey(hKey);
return(lpgd);
LGError3:
GlobalUnlock(pGroup->hGroup);
GlobalDiscard(pGroup->hGroup);
pGroup->fLoaded = FALSE;
LGError2:
RegCloseKey(hKey);
LGError1:
if (status != IDS_LOWMEM && status != IDS_GRPHASCHANGED && status != IDS_NOGRPFILE) {
MyMessageBox(hwndProgman, IDS_GROUPFILEERR, status, pGroup->lpKey,
MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);
if (status == IDS_BADFILE) {
//
// stop handling of Program Groups key changes.
//
bHandleProgramGroupsEvent = FALSE;
RegDeleteKey(hkeyGroups, lpszKey);
//
// reset handling of Program Groups key changes.
//
ResetProgramGroupsEvent(bCommonGroup);
bHandleProgramGroupsEvent = TRUE;
}
return(NULL);
}
/*
* Special case the group not being found so we can delete it's entry...
*/
if (status == IDS_NOGRPFILE) {
/*
* If no restrictions then we can fixup progman.ini...
*/
if (!fNoSave && dwEditLevel < 1) {
TCHAR szGroup[10];
if (MyMessageBox(hwndProgman,IDS_GROUPFILEERR,IDS_NOGRPFILE2,lpszKey, MB_YESNO | MB_ICONEXCLAMATION | MB_DEFBUTTON1 | MB_SYSTEMMODAL) == IDNO) {
wsprintf(szGroup,TEXT("Group%d"),pGroup->wIndex);
//
// stop handling of Program Groups key changes.
//
bHandleProgramGroupsEvent = FALSE;
RegDeleteKey(hkeyProgramGroups, lpszKey);
//
// reset handling of Program Groups key changes.
//
ResetProgramGroupsEvent(bCommonGroup);
bHandleProgramGroupsEvent = TRUE;
RegDeleteValue(hkeyPMGroups, szGroup);
if (!fFirstLoad)
PostMessage(hwndProgman,WM_UNLOADGROUP,(WPARAM)hwndGroup,0L);
}
}
else {
RegError:
/*
* Restrictions mean that the user can only OK this error...
*/
MyMessageBox(hwndProgman, IDS_GROUPFILEERR, IDS_NOGRPFILE, lpszKey,
MB_OK | MB_ICONEXCLAMATION | MB_SYSTEMMODAL);
}
}
ShowWindow(hwndGroup, SW_SHOWMINNOACTIVE);
return(NULL);
}
/*--------------------------------------------------------------------------*/
/* */
/* UnlockGroup() - */
/* */
/*--------------------------------------------------------------------------*/
void FAR PASCAL UnlockGroup(register HWND hwndGroup)
{
GlobalUnlock(((PGROUP)GetWindowLongPtr(hwndGroup,GWLP_PGROUP))->hGroup);
}
/*--------------------------------------------------------------------------*/
/* */
/* LockItem() - */
/* */
/*--------------------------------------------------------------------------*/
LPITEMDEF FAR PASCAL LockItem(PGROUP pGroup, PITEM pItem)
{
LPGROUPDEF lpgd;
lpgd = LockGroup(pGroup->hwnd);
if (!lpgd)
return((LPITEMDEF)NULL);
return ITEM(lpgd,pItem->iItem);
}
/*--------------------------------------------------------------------------*/
/* */
/* KeepGroupAround() - */
/* */
/*--------------------------------------------------------------------------*/
/*
* Sets or unsets the discardable flag for the given group file. If setting
* to non-discard, forces the group to be in memory.
*/
HANDLE PASCAL KeepGroupAround(HWND hwndGroup, BOOL fKeep)
{
PGROUP pGroup;
UNREFERENCED_PARAMETER(fKeep);
pGroup = (PGROUP)GetWindowLongPtr(hwndGroup, GWLP_PGROUP);
return pGroup->hGroup;
#ifdef ORGCODE
PGROUP pGroup;
WORD flag;
pGroup = (PGROUP)GetWindowLongPtr(hwndGroup, GWLP_PGROUP);
if (fKeep) {
if (LockGroup(hwndGroup)) {
UnlockGroup(hwndGroup); // it is still in memory
} else {
return NULL; // failure
}
flag = GMEM_MODIFY | GMEM_MOVEABLE; // make non discardable
} else {
flag = GMEM_MODIFY | GMEM_MOVEABLE | GMEM_DISCARDABLE; // discardable
}
return GlobalReAlloc(pGroup->hGroup, 0, flag);
#endif
}
/*--------------------------------------------------------------------------*/
/* */
/* SaveGroup() - */
/* */
/*--------------------------------------------------------------------------*/
/*
* Writes out a group file. It must already be in memory or the operation
* is meaningless.
*/
BOOL APIENTRY SaveGroup(
HWND hwndGroup, BOOL bDiscard
)
{
LPGROUPDEF lpgd;
HKEY hKey;
PGROUP pGroup;
WORD status = 0;
DWORD cb;
LONG err;
HKEY hkeyGroups;
BOOL bCommonGroup;
DWORD dwDisposition;
pGroup = (PGROUP)GetWindowLongPtr(hwndGroup, GWLP_PGROUP);
bCommonGroup = pGroup->fCommon;
if (!bUseANSIGroups && IsGroupReadOnly(pGroup->lpKey, bCommonGroup)) {
// Don't produce an error message for RO groups.
return FALSE;
}
lpgd = (LPGROUPDEF)GlobalLock(pGroup->hGroup);
if (!lpgd) {
return FALSE;
}
if (bCommonGroup)
hkeyGroups = hkeyCommonGroups;
else
hkeyGroups = hkeyProgramGroups;
if (!hkeyGroups) {
goto Exit1;
}
// it may already exist
if (err = RegCreateKeyEx(hkeyGroups, pGroup->lpKey, 0, 0, 0,
DELETE | KEY_READ | KEY_WRITE | WRITE_DAC,
pSecurityAttributes, &hKey, &dwDisposition)) {
//if (err = RegOpenKeyEx(hkeyGroups, pGroup->lpKey, 0,
// KEY_SET_VALUE, &hKey)) {
/*
* We can't open output group key.
*/
if (err = RegOpenKeyEx(hkeyGroups, pGroup->lpKey, 0,
KEY_READ, &hKey)) {
status = IDS_NOGRPFILE;
} else {
// status = IDS_GRPISRO;
RegCloseKey(hKey);
}
goto Exit1;
}
else {
if (dwDisposition == REG_CREATED_NEW_KEY && bUseANSIGroups) {
RegSetKeySecurity(hKey, DACL_SECURITY_INFORMATION, pGroup->pSecDesc);
LocalFree(pGroup->pSecDesc);
pGroup->pSecDesc = NULL;
}
}
//
// stop handling Program Groups key changes for a SAveGroup.
//
bHandleProgramGroupsEvent = FALSE;
cb = SizeofGroup(lpgd);
if (err = RegSetValueEx(hKey, NULL, 0, REG_BINARY, (LPBYTE)lpgd, cb)) {
status = IDS_CANTWRITEGRP;
}
RegFlushKey(hKey);
RegCloseKey(hKey);
pGroup->ftLastWriteTime.dwLowDateTime = 0; // update file time stamp if we need to reload
pGroup->ftLastWriteTime.dwHighDateTime = 0;
Exit1:
GlobalUnlock(pGroup->hGroup);
if (status && !fExiting) {
MyMessageBox(hwndProgman, IDS_GROUPFILEERR, status, pGroup->lpKey,
MB_OK | MB_ICONEXCLAMATION);
/*
* Force the group to be reset.
*/
if (bDiscard) {
GlobalDiscard(pGroup->hGroup);
pGroup->fLoaded = FALSE;
InvalidateRect(pGroup->hwnd, NULL, TRUE);
}
}
//
// reset handling of Program Groups key changes.
//
ResetProgramGroupsEvent(bCommonGroup);
bHandleProgramGroupsEvent = TRUE;
return (status == 0);
}
/*--------------------------------------------------------------------------*/
/* */
/* AdjustPointers() - */
/* */
/*--------------------------------------------------------------------------*/
/*
* Adjusts pointers in the segment after a section is moved up or down.
*/
void PASCAL AdjustPointers(LPGROUPDEF lpgd, DWORD iFirst, DWORD di)
{
WORD i;
LPITEMDEF lpid;
if (lpgd->pName >= iFirst) {
lpgd->pName += di;
}
for (i = 0; i < lpgd->cItems; i++) {
if (!lpgd->rgiItems[i]) {
continue;
}
if (lpgd->rgiItems[i] >= iFirst) {
lpgd->rgiItems[i] += di;
}
lpid = ITEM(lpgd, i);
if (lpid->pIconRes >= iFirst)
lpid->pIconRes += di;
if (lpid->pName >= iFirst)
lpid->pName += di;
if (lpid->pCommand >= iFirst)
lpid->pCommand += di;
if (lpid->pIconPath >= iFirst)
lpid->pIconPath += di;
}
}
/*--------------------------------------------------------------------------*/
/* */
/* FindFreeItemIndex() - */
/* */
/* Returns the index of a free slot in the item offset array. If necessary,*/
/* moves stuff around. */
/* */
/*--------------------------------------------------------------------------*/
WORD PASCAL FindFreeItemIndex(HWND hwndGroup)
{
LPGROUPDEF lpgd;
PGROUP pGroup;
WORD i;
LPTSTR lp1;
LPTSTR lp2;
DWORD cb;
lpgd = LockGroup(hwndGroup);
if (!lpgd) {
return(0xFFFF);
}
for (i = 0; i < lpgd->cItems; i++) {
if (!lpgd->rgiItems[i]) {
UnlockGroup(hwndGroup);
return(i);
}
}
/*
* Didn't find an empty slot... make some new ones.
*/
pGroup = (PGROUP)GetWindowLongPtr(hwndGroup, GWLP_PGROUP);
// Current groups+tags size.
cb = SizeofGroup(lpgd);
// Increase space reserved item info.
lpgd->cbGroup += NSLOTS*sizeof(DWORD);
// Increase size of whole group.
cb += NSLOTS*sizeof(DWORD);
UnlockGroup(hwndGroup);
CheckBeforeReAlloc(pGroup->hGroup);
if (!GlobalReAlloc(pGroup->hGroup, cb, GMEM_MOVEABLE)) {
return 0xFFFF;
}
lpgd = (LPGROUPDEF)GlobalLock(pGroup->hGroup);
/*
* Copy tags junk (which starts at the end of the rgiItems array)
* up a bit to make room for the bigger array..
*/
lp1 = (LPTSTR)&(lpgd->rgiItems[lpgd->cItems]);
lp2 = (LPTSTR)&(lpgd->rgiItems[lpgd->cItems + NSLOTS]);
/*
* Copy everything down in the segment.
*/
RtlMoveMemory(lp2, lp1, (WORD)(cb - (DWORD)((LPSTR)lp2 - (LPSTR)lpgd)));
/*
* Zero out the new offsets.
*/
for (i = (WORD)lpgd->cItems; i < (WORD)(lpgd->cItems + NSLOTS); i++) {
lpgd->rgiItems[i] = 0;
}
i = lpgd->cItems;
/* Record that we now have more slots */
lpgd->cItems += NSLOTS;
/*
* Fix up all the offsets in the segment. Since the rgiItems array is
* part of the group header, all the pointers will change.
*/
AdjustPointers(lpgd, (WORD)1, NSLOTS * sizeof(DWORD));
GlobalUnlock(pGroup->hGroup);
return i;
}
/*--------------------------------------------------------------------------*/
/* */
/* DeleteThing() - */
/* */
/* */
/* Removes a part of the group segment. Updates everything in the segment */
/* but does not realloc. */
/* */
/*--------------------------------------------------------------------------*/
void NEAR PASCAL DeleteThing(LPGROUPDEF lpgd, LPDWORD lpiThing, WORD cbThing)
{
DWORD dwThingOffset;
LPTSTR lp1;
LPTSTR lp2;
INT cb;
WORD cbThingSize;
if (cbThing == 0xFFFF) {
return;
}
dwThingOffset = *lpiThing;
if (!dwThingOffset)
return;
*lpiThing = 0;
lp1 = (LPTSTR) PTR(lpgd, dwThingOffset);
/* If its a string we're removing, the caller can pass 0 as the length
* and have it calculated!!!
*/
if (!cbThing) {
cbThing = (WORD)sizeof(TCHAR)*(1 + lstrlen(lp1));
}
cbThingSize = (WORD)MyDwordAlign((int)cbThing);
lp2 = (LPTSTR)((LPBYTE)lp1 + cbThingSize);
cb = (int)SizeofGroup(lpgd);
RtlMoveMemory(lp1, lp2, (cb - (DWORD)((LPSTR)lp2 - (LPSTR)lpgd)));
lpgd->cbGroup -= cbThingSize;
AdjustPointers(lpgd, dwThingOffset, -cbThingSize);
}
/*--------------------------------------------------------------------------*/
/* */
/* AddThing() - */
/* */
/* in: */
/* hGroup group handle, must not be discardable */
/* lpStuff pointer to data or NULL to init data to zero */
/* cbStuff count of item (may be 0) if lpStuff is a string */
/* */
/* Adds an object to the group segment and returns its offset. Will */
/* reallocate the segment if necessary. */
/* */
/* Handle passed in must not be discardable */
/* */
/* returns: */
/* 0 failure */
/* > 0 offset to thing in the segment */
/* */
/*--------------------------------------------------------------------------*/
DWORD PASCAL AddThing(HANDLE hGroup, LPTSTR lpStuff, DWORD cbStuff)
{
DWORD cb;
LPGROUPDEF lpgd;
DWORD offset;
LPTSTR lpT;
DWORD cbStuffSize;
DWORD cbGroupSize;
DWORD myOffset;
if (cbStuff == 0xFFFFFFFF) {
return 0xFFFFFFFF;
}
if (!cbStuff) {
cbStuff = sizeof(TCHAR)*(DWORD)(1 + lstrlen(lpStuff));
}
cbStuffSize = MyDwordAlign((int)cbStuff);
lpgd = (LPGROUPDEF)GlobalLock(hGroup);
cb = SizeofGroup(lpgd);
cbGroupSize = MyDwordAlign((int)cb);
offset = lpgd->cbGroup;
myOffset = (DWORD)MyDwordAlign((int)offset);
GlobalUnlock(hGroup);
CheckBeforeReAlloc(hGroup);
if (!GlobalReAlloc(hGroup,(DWORD)(cbGroupSize + cbStuffSize), GMEM_MOVEABLE))
return 0;
lpgd = (LPGROUPDEF)GlobalLock(hGroup);
/*
* Slide the tags up
*/
RtlMoveMemory((LPSTR)lpgd + myOffset + cbStuffSize, (LPSTR)lpgd + myOffset,
(cbGroupSize - myOffset));
lpgd->cbGroup += cbStuffSize;
lpT = (LPTSTR)((LPSTR)lpgd + myOffset);
if (lpStuff) {
RtlMoveMemory(lpT, lpStuff, cbStuff);
} else {
/*
* Zero it
*/
while (cbStuffSize--) {
*((LPSTR)lpT)++ = 0;
}
}
GlobalUnlock(hGroup);
return myOffset;
}
/*--------------------------------------------------------------------------*/
/* */
/* FindTag() - */
/* */
/*--------------------------------------------------------------------------*/
LPPMTAG NEAR PASCAL FindTag(LPGROUPDEF lpgd, int item, WORD id)
{
LPPMTAG lptag;
int cbSeg;
int cb;
cbSeg = (DWORD)GlobalSize(lpgd);
lptag = (LPPMTAG)((LPSTR)lpgd+lpgd->cbGroup);
if ((PCHAR)lptag - (PCHAR)lpgd + MyDwordAlign(sizeof(PMTAG))-MyDwordAlign(sizeof(lptag->rgb)) + 4 <= cbSeg
&& lptag->wID == ID_MAGIC
&& lptag->wItem == (int)0xFFFF
&& lptag->cb == (WORD)(MyDwordAlign(sizeof(PMTAG))-MyDwordAlign(sizeof(lptag->rgb)) +4)
&& *(LONG FAR *)lptag->rgb == PMTAG_MAGIC) {
while ((cb = (int)((PCHAR)lptag - (PCHAR)lpgd + MyDwordAlign(sizeof(PMTAG))-MyDwordAlign(sizeof(lptag->rgb)))) <= cbSeg)
{
if ((item == lptag->wItem)
&& (id == 0 || id == lptag->wID)) {
return lptag;
}
if (lptag->wID == ID_LASTTAG)
return NULL;
(LPSTR)lptag += lptag->cb;
}
}
return NULL;
}
/*--------------------------------------------------------------------------*/