-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregistry.c
868 lines (733 loc) · 23.6 KB
/
registry.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
//? Marius Negrutiu (marius.negrutiu@protonmail.com) :: 2015/06/13
#include "main.h"
//++ [not exported] RegistryParsePath
BOOLEAN RegistryParsePath(
__in LPCTSTR szFullPath, /// ex: "HKLM\Software\Microsoft\Whatever"
__out PHKEY phRoot, /// Receives HKLM
__out LPCTSTR *ppszPath /// Receives "Software\Microsoft\Whatever"
)
{
BOOLEAN bRet = TRUE;
if (szFullPath && *szFullPath && ppszPath) {
*phRoot = NULL;
*ppszPath = NULL;
if (CompareString( 0, NORM_IGNORECASE, szFullPath, 5, _T( "HKLM\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_LOCAL_MACHINE;
*ppszPath = szFullPath + 5;
} else if (CompareString( 0, NORM_IGNORECASE, szFullPath, 5, _T( "HKCU\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_CURRENT_USER;
*ppszPath = szFullPath + 5;
} else if (CompareString( 0, NORM_IGNORECASE, szFullPath, 4, _T( "HKU\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_USERS;
*ppszPath = szFullPath + 4;
} else if (CompareString( 0, NORM_IGNORECASE, szFullPath, 5, _T( "HKCR\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_CLASSES_ROOT;
*ppszPath = szFullPath + 5;
} else if (CompareString( 0, NORM_IGNORECASE, szFullPath, 5, _T( "HKCC\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_CURRENT_CONFIG;
*ppszPath = szFullPath + 5;
} else if (CompareString( 0, NORM_IGNORECASE, szFullPath, 5, _T( "HKDD\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_DYN_DATA;
*ppszPath = szFullPath + 5;
} else if (CompareString( 0, NORM_IGNORECASE, szFullPath, 5, _T( "HKPD\\" ), -1 ) == CSTR_EQUAL) {
*phRoot = HKEY_PERFORMANCE_DATA;
*ppszPath = szFullPath + 5;
} else {
/// Invalid root key
bRet = FALSE;
}
} else {
bRet = FALSE;
}
return bRet;
}
#define MULTISZ_ACTION_NONE 0
#define MULTISZ_ACTION_INSERT_BEFORE 1
#define MULTISZ_ACTION_INSERT_AFTER 2
typedef int( *MultiSzCallback )(__in int iSubstrIndex, __in LPCTSTR pszSubstr, __in PVOID pParam);
int CallbackMultiSzInsertBefore( __in int iSubstrIndex, __in LPCTSTR pszSubstr, __in PVOID pParam )
{
LPCTSTR pszInsertAfter = (LPCTSTR)pParam;
if (pszInsertAfter && *pszInsertAfter && CompareString( 0, NORM_IGNORECASE, pszSubstr, -1, pszInsertAfter, -1 ) == CSTR_EQUAL)
return MULTISZ_ACTION_INSERT_BEFORE;
return MULTISZ_ACTION_NONE;
}
int CallbackMultiSzInsertAfter( __in int iSubstrIndex, __in LPCTSTR pszSubstr, __in PVOID pParam )
{
LPCTSTR pszInsertAfter = (LPCTSTR)pParam;
if (pszInsertAfter && *pszInsertAfter && CompareString( 0, NORM_IGNORECASE, pszSubstr, -1, pszInsertAfter, -1 ) == CSTR_EQUAL)
return MULTISZ_ACTION_INSERT_AFTER;
return MULTISZ_ACTION_NONE;
}
int CallbackMultiSzInsertAtIndex( __in int iSubstrIndex, __in LPCTSTR pszSubstr, __in PVOID pParam )
{
int iInsertAtIndex = PtrToInt( pParam );
if (iSubstrIndex == iInsertAtIndex)
return MULTISZ_ACTION_INSERT_BEFORE;
return MULTISZ_ACTION_NONE;
}
//++ [not exported] RegMultiSzInsertImpl
DWORD RegMultiSzInsertImpl(
__in LPCTSTR szRegKey,
__in_opt LPCTSTR szRegValue,
__in_opt DWORD dwKeyOpenFlags,
__in LPCTSTR szInsert,
__in_opt MultiSzCallback fnInsertCallback,
__in_opt PVOID pInsertParam
)
{
DWORD err = ERROR_SUCCESS;
if (szRegKey && *szRegKey && szInsert && *szInsert) {
LPTSTR pszPath;
HKEY hRoot, hKey;
if (RegistryParsePath( szRegKey, &hRoot, (LPCTSTR*)&pszPath )) {
err = RegCreateKeyEx( hRoot, pszPath, 0, NULL, 0, KEY_WRITE | KEY_READ | dwKeyOpenFlags, NULL, &hKey, NULL );
if (err == ERROR_SUCCESS) {
int iAddLen = lstrlen( szInsert );
DWORD iType, iOldLen = 0;
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, NULL, &iOldLen );
if (err == ERROR_SUCCESS && iType == REG_MULTI_SZ && iOldLen > 2) {
// Existing registry value
int iNewLen = iOldLen / sizeof( TCHAR ) + iAddLen + 1;
LPTSTR psz = (LPTSTR)HeapAlloc( GetProcessHeap(), 0, iNewLen * sizeof( TCHAR ) );
if (psz) {
iOldLen = iNewLen * sizeof( TCHAR );
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, (LPBYTE)psz, &iOldLen );
if (err == ERROR_SUCCESS) {
BOOLEAN bFound;
TCHAR *p;
iOldLen /= sizeof( TCHAR ); /// Bytes -> TCHAR
/// Enumerate all substrings and decide where to insert
if (fnInsertCallback) {
int i, iAction;
for (p = psz, bFound = FALSE, i = 0; (*p && !bFound); i++) {
iAction = fnInsertCallback( i, p, pInsertParam );
switch (iAction) {
case MULTISZ_ACTION_NONE:
for (; *p; p++); /// Skip current substring
p++; /// Skip NULL terminator
break;
case MULTISZ_ACTION_INSERT_BEFORE:
bFound = TRUE;
break;
case MULTISZ_ACTION_INSERT_AFTER:
bFound = TRUE;
for (; *p; p++); /// Skip current substring
p++; /// Skip NULL terminator
break;
default:;
///assert( !"Invalid action" );
}
}
} else {
p = psz + iOldLen - 1; /// End of string
}
/// Insert substring
if (TRUE) {
int iMoveLen = iOldLen - (int)(p - psz);
memmove( p + iAddLen + 1, p, iMoveLen * sizeof( TCHAR ) );
lstrcpy( p, szInsert );
}
err = RegSetValueEx( hKey, szRegValue, 0, REG_MULTI_SZ, (LPBYTE)psz, iNewLen * sizeof( TCHAR ) );
}
HeapFree( GetProcessHeap(), 0, psz );
} else {
err = ERROR_OUTOFMEMORY;
}
} else {
// New registry value
int iNewLen = iAddLen + 2;
LPTSTR psz = (LPTSTR)HeapAlloc( GetProcessHeap(), 0, iNewLen * sizeof( TCHAR ) );
if (psz) {
lstrcpy( psz, szInsert );
psz[iNewLen - 1] = _T( '\0' );
psz[iNewLen - 2] = _T( '\0' );
err = RegSetValueEx( hKey, szRegValue, 0, REG_MULTI_SZ, (LPBYTE)psz, iNewLen * sizeof( TCHAR ) );
HeapFree( GetProcessHeap(), 0, psz );
} else {
err = ERROR_OUTOFMEMORY;
}
}
RegCloseKey( hKey );
}
} else {
err = ERROR_PATH_NOT_FOUND; /// RegistryParsePath
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ [not exported] RegMultiSzDeleteImpl
DWORD RegMultiSzDeleteImpl(
__in LPCTSTR szRegKey,
__in_opt LPCTSTR szRegValue,
__in_opt DWORD dwKeyOpenFlags,
__in LPCTSTR szDelete,
__in BOOL bRemoveEmptyValue
)
{
DWORD err = ERROR_SUCCESS;
if (szRegKey && *szRegKey && szDelete && *szDelete) {
LPTSTR pszPath;
HKEY hRoot, hKey;
if (RegistryParsePath( szRegKey, &hRoot, (LPCTSTR*)&pszPath )) {
err = RegCreateKeyEx( hRoot, pszPath, 0, NULL, 0, KEY_WRITE | KEY_READ | dwKeyOpenFlags, NULL, &hKey, NULL );
if (err == ERROR_SUCCESS) {
DWORD iType, iOldLen = 0;
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, NULL, &iOldLen );
if (err == ERROR_SUCCESS && iType == REG_MULTI_SZ) {
LPTSTR psz = (LPTSTR)HeapAlloc( GetProcessHeap(), 0, iOldLen * sizeof( TCHAR ) );
if (psz) {
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, (LPBYTE)psz, &iOldLen );
if (err == ERROR_SUCCESS) {
TCHAR *p;
DWORD iNewLen;
iOldLen /= sizeof( TCHAR ); /// Bytes -> TCHAR
iNewLen = iOldLen;
/// Enumerate all substrings
for (p = psz; *p;) {
if (CompareString( 0, NORM_IGNORECASE, p, -1, szDelete, -1 ) == CSTR_EQUAL) {
/// Delete substring
int iDeleteLen = lstrlen( p ) + 1;
int iMoveLen = iOldLen - iDeleteLen - (int)(p - psz);
memmove( p, p + iDeleteLen, iMoveLen * sizeof( TCHAR ) );
iNewLen -= iDeleteLen;
} else {
for (; *p; p++); /// Skip current substring
p++; /// Skip NULL terminator
}
}
/// Set the registry value
if (iNewLen != iOldLen) {
if (!bRemoveEmptyValue || (iNewLen > 2)) {
err = RegSetValueEx( hKey, szRegValue, 0, REG_MULTI_SZ, (LPBYTE)psz, iNewLen * sizeof( TCHAR ) );
} else {
err = RegDeleteValue( hKey, szRegValue );
}
}
}
HeapFree( GetProcessHeap(), 0, psz );
} else {
err = ERROR_OUTOFMEMORY;
}
}
RegCloseKey( hKey );
}
} else {
err = ERROR_PATH_NOT_FOUND; /// RegistryParsePath
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ [not exported] RegMultiSzReadImpl
DWORD RegMultiSzReadImpl(
__in LPCTSTR szRegKey,
__in_opt LPCTSTR szRegValue,
__in_opt DWORD dwKeyOpenFlags,
__in int iIndex,
__out_opt LPTSTR pszOutString,
__in_opt ULONG iOutStringLen
)
{
DWORD err = ERROR_SUCCESS;
if (szRegKey && *szRegKey) {
LPTSTR pszPath;
HKEY hRoot, hKey;
if (RegistryParsePath( szRegKey, &hRoot, (LPCTSTR*)&pszPath )) {
err = RegCreateKeyEx( hRoot, pszPath, 0, NULL, 0, KEY_READ | dwKeyOpenFlags, NULL, &hKey, NULL );
if (err == ERROR_SUCCESS) {
DWORD iType, iLen = 0;
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, NULL, &iLen );
if (err == ERROR_SUCCESS && iType == REG_MULTI_SZ) {
LPTSTR psz = (LPTSTR)HeapAlloc( GetProcessHeap(), 0, iLen * sizeof( TCHAR ) );
if (psz) {
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, (LPBYTE)psz, &iLen );
if (err == ERROR_SUCCESS) {
TCHAR *p;
int i;
iLen /= sizeof( TCHAR ); /// Bytes -> TCHAR
err = ERROR_INVALID_INDEX;
for (p = psz, i = 0; *p; i++) {
if (i == iIndex)
if (pszOutString && iOutStringLen) {
lstrcpyn( pszOutString, p, iOutStringLen );
err = ERROR_SUCCESS;
break;
}
for (; *p; p++); /// Skip current substring
p++; /// Skip NULL terminator
}
}
HeapFree( GetProcessHeap(), 0, psz );
} else {
err = ERROR_OUTOFMEMORY;
}
} else {
if (iType != REG_MULTI_SZ)
err = ERROR_DATATYPE_MISMATCH;
}
RegCloseKey( hKey );
}
} else {
err = ERROR_PATH_NOT_FOUND; /// RegistryParsePath
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ [exported] RegMultiSzInsertAfter
// ----------------------------------------------------------------------
//+ Input:
// [Stack] Registry key (ex: "HKCU\Software\MyCompany")
// [Stack] Registry value. If empty, the default (unnamed) value is set
// [Stack] Additional key flags (ex: KEY_WOW64_64KEY)
// [Stack] Substring to insert
// [Stack] Substring to insert after (optional)
//+ Output:
// [Stack] Win32 error
//+ Example:
// NSutils::RegMultiSzInsertAfter /NOUNLOAD "HKCU\Software\MyCompany" "MyValue" ${KEY_WOW64_64KEY} "Line 4" "Line 3"
// Pop $0 ; Win32 error
__declspec(dllexport)
void RegMultiSzInsertAfter(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
DWORD err = ERROR_SUCCESS;
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store any NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof( TCHAR ) * string_size );
if (pszBuf) {
TCHAR szRegKey[512], szRegValue[128], szInsert[255], szInsertAfter[255];
DWORD dwFlags;
err = ERROR_INVALID_PARAMETER;
/// Param1: Registry key
if (popstring( pszBuf ) == 0) {
lstrcpyn( szRegKey, pszBuf, ARRAYSIZE( szRegKey ) );
/// Param2: Registry value
if (popstring( pszBuf ) == 0) {
lstrcpyn( szRegValue, pszBuf, ARRAYSIZE( szRegValue ) );
/// Param3: Additional key flags
dwFlags = popint();
/// Param4: Substring to insert
if (popstring( pszBuf ) == 0) {
lstrcpyn( szInsert, pszBuf, ARRAYSIZE( szInsert ) );
/// Param5: Substring to insert after
if (popstring( pszBuf ) == 0) {
lstrcpyn( szInsertAfter, pszBuf, ARRAYSIZE( szInsertAfter ) );
// Insert substring
err = RegMultiSzInsertImpl( szRegKey, szRegValue, dwFlags, szInsert, CallbackMultiSzInsertAfter, szInsertAfter );
}
}
}
}
/// Free memory
GlobalFree( pszBuf );
} else {
err = ERROR_OUTOFMEMORY;
}
/// Return value
pushintptr( err );
}
//++ [exported] RegMultiSzInsertBefore
// ----------------------------------------------------------------------
//+ Input:
// [Stack] Registry key (ex: "HKCU\Software\MyCompany")
// [Stack] Registry value. If empty, the default (unnamed) value is set
// [Stack] Additional key flags (ex: KEY_WOW64_64KEY)
// [Stack] Substring to insert
// [Stack] Substring to insert before (optional)
//+ Output:
// [Stack] Win32 error
//+ Example:
// NSutils::RegMultiSzInsertBefore /NOUNLOAD "HKCU\Software\MyCompany" "MyValue" ${KEY_WOW64_64KEY} "Line 4" "Line 5"
// Pop $0 ; Win32 error
__declspec(dllexport)
void RegMultiSzInsertBefore(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
DWORD err = ERROR_SUCCESS;
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store any NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof( TCHAR ) * string_size );
if (pszBuf) {
TCHAR szRegKey[512], szRegValue[128], szInsert[255], szInsertBefore[255];
DWORD dwFlags;
err = ERROR_INVALID_PARAMETER;
/// Param1: Registry key
if (popstring( pszBuf ) == 0) {
lstrcpyn( szRegKey, pszBuf, ARRAYSIZE( szRegKey ) );
/// Param2: Registry value
if (popstring( pszBuf ) == 0) {
lstrcpyn( szRegValue, pszBuf, ARRAYSIZE( szRegValue ) );
/// Param3: Additional key flags
dwFlags = popint();
/// Param4: Substring to insert
if (popstring( pszBuf ) == 0) {
lstrcpyn( szInsert, pszBuf, ARRAYSIZE( szInsert ) );
/// Param5: Substring to insert before
if (popstring( pszBuf ) == 0) {
lstrcpyn( szInsertBefore, pszBuf, ARRAYSIZE( szInsertBefore ) );
// Insert substring
err = RegMultiSzInsertImpl( szRegKey, szRegValue, dwFlags, szInsert, CallbackMultiSzInsertBefore, szInsertBefore );
}
}
}
}
/// Free memory
GlobalFree( pszBuf );
} else {
err = ERROR_OUTOFMEMORY;
}
/// Return value
pushintptr( err );
}
//++ [exported] RegMultiSzInsertAtIndex
// ----------------------------------------------------------------------
//+ Input:
// [Stack] Registry key (ex: "HKLM\Software\MyCompany")
// [Stack] Registry value. If empty, the default (unnamed) value is set
// [Stack] Additional key flags (ex: KEY_WOW64_64KEY)
// [Stack] Substring to insert
// [Stack] Zero based index (optional)
//+ Output:
// [Stack] Win32 error
//+ Example:
// NSutils::RegMultiSzInsertBefore [/NOUNLOAD] "HKLM\Software\MyCompany" "MyValue" ${KEY_WOW64_64KEY} "Line 4" 3
// Pop $0 ; Win32 error
__declspec(dllexport)
void RegMultiSzInsertAtIndex(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
DWORD err = ERROR_SUCCESS;
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store any NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof( TCHAR ) * string_size );
if (pszBuf) {
TCHAR szRegKey[512], szRegValue[128], szInsert[255];
int iIndex;
DWORD dwFlags;
err = ERROR_INVALID_PARAMETER;
/// Param1: Registry key
if (popstring( pszBuf ) == 0) {
lstrcpyn( szRegKey, pszBuf, ARRAYSIZE( szRegKey ) );
/// Param2: Registry value
if (popstring( pszBuf ) == 0) {
lstrcpyn( szRegValue, pszBuf, ARRAYSIZE( szRegValue ) );
/// Param3: Additional key flags
dwFlags = popint();
/// Param4: Substring to insert
if (popstring( pszBuf ) == 0) {
lstrcpyn( szInsert, pszBuf, ARRAYSIZE( szInsert ) );
/// Param5: Index (zero based)
iIndex = popint();
// Insert substring
err = RegMultiSzInsertImpl( szRegKey, szRegValue, dwFlags, szInsert, CallbackMultiSzInsertAtIndex, ULongToPtr( iIndex ) );
}
}
}
/// Free memory
GlobalFree( pszBuf );
} else {
err = ERROR_OUTOFMEMORY;
}
/// Return value
pushintptr( err );
}
//++ [exported] RegMultiSzDelete
// ----------------------------------------------------------------------
//+ Input:
// [Stack] Registry key (ex: "HKCU\Software\MyCompany")
// [Stack] Registry value. If empty, the default (unnamed) value is set
// [Stack] Additional key flags (ex: KEY_WOW64_64KEY)
// [Stack] Substring to delete
// [Stack] Remove the registry value if it becomes empty (BOOL)
//+ Output:
// [Stack] Win32 error
//+ Example:
// NSutils::RegMultiSzDelete /NOUNLOAD "HKCU\Software\MyCompany" "MyValue" ${KEY_WOW64_64KEY} "Line 4" ${TRUE}
// Pop $0 ; Win32 error
__declspec(dllexport)
void RegMultiSzDelete(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
DWORD err = ERROR_SUCCESS;
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store any NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof( TCHAR ) * string_size );
if (pszBuf) {
TCHAR szKey[512], szValue[128], szDelStr[255];
BOOL bRemoveEmptyValue;
DWORD dwFlags;
err = ERROR_INVALID_PARAMETER;
/// Param1: Registry key
if (popstring( pszBuf ) == 0) {
lstrcpyn( szKey, pszBuf, ARRAYSIZE( szKey ) );
/// Param2: Registry value
if (popstring( pszBuf ) == 0) {
lstrcpyn( szValue, pszBuf, ARRAYSIZE( szValue ) );
/// Param3: Additional key flags
dwFlags = popint();
/// Param4: Substring to delete
if (popstring( pszBuf ) == 0) {
lstrcpyn( szDelStr, pszBuf, ARRAYSIZE( szDelStr ) );
/// Param5: Remove empty value (BOOL)
bRemoveEmptyValue = (BOOL)popint();
// Delete substring(s)
err = RegMultiSzDeleteImpl( szKey, szValue, dwFlags, szDelStr, bRemoveEmptyValue );
}
}
}
/// Free memory
GlobalFree( pszBuf );
} else {
err = ERROR_OUTOFMEMORY;
}
/// Return value
pushintptr( err );
}
//++ [exported] RegMultiSzRead
// ----------------------------------------------------------------------
//+ Input:
// [Stack] Registry key (ex: "HKCU\Software\MyCompany")
// [Stack] Registry value. If empty, the default (unnamed) value is set
// [Stack] Additional key flags (ex: KEY_WOW64_64KEY)
// [Stack] Index (substring to read)
//+ Output:
// [Stack] Win32 error
// [Stack] The substring at specified index, or an empty string
//+ Example:
// NSutils::RegMultiSzRead /NOUNLOAD "HKCU\Software\MyCompany" "MyValue" ${KEY_WOW64_64KEY} 2
// Pop $0 ; Win32 error
// Pop $1 ; Substring at index 2
__declspec(dllexport)
void RegMultiSzRead(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
DWORD err = ERROR_SUCCESS;
LPTSTR pszBuf = NULL;
TCHAR szSubstr[512];
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Init
szSubstr[0] = 0;
// Retrieve NSIS parameters
/// Allocate memory large enough to store any NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof( TCHAR ) * string_size );
if (pszBuf) {
TCHAR szKey[512], szValue[128];
int iIndex;
DWORD dwFlags;
err = ERROR_INVALID_PARAMETER;
/// Param1: Registry key
if (popstring( pszBuf ) == 0) {
lstrcpyn( szKey, pszBuf, ARRAYSIZE( szKey ) );
/// Param2: Registry value
if (popstring( pszBuf ) == 0) {
lstrcpyn( szValue, pszBuf, ARRAYSIZE( szValue ) );
/// Param3: Additional key flags
dwFlags = popint();
/// Param4: Substring index
iIndex = popint();
// Read substring
err = RegMultiSzReadImpl( szKey, szValue, dwFlags, iIndex, szSubstr, ARRAYSIZE( szSubstr ) );
}
}
/// Free memory
GlobalFree( pszBuf );
} else {
err = ERROR_OUTOFMEMORY;
}
/// Return value
pushstring( szSubstr );
pushintptr( err );
}
//++ [not exported] RegBinaryWriteBufImpl
DWORD RegBinaryWriteBufImpl(
__in LPCTSTR szRegKey,
__in_opt LPCTSTR szRegValue,
__in_opt DWORD dwKeyOpenFlags,
__in int iOffset,
__in LPVOID pBuf,
__in ULONG iBufSize
)
{
DWORD err = ERROR_SUCCESS;
if (szRegKey && *szRegKey && pBuf && iBufSize) {
LPTSTR pszPath;
HKEY hRoot, hKey;
if (RegistryParsePath( szRegKey, &hRoot, (LPCTSTR*)&pszPath )) {
err = RegCreateKeyEx( hRoot, pszPath, 0, NULL, 0, KEY_READ | KEY_WRITE | dwKeyOpenFlags, NULL, &hKey, NULL );
if (err == ERROR_SUCCESS) {
BOOL bFatalError = FALSE;
LPBYTE pValBuf = NULL;
ULONG iValBufSize = 0;
// Read existing value
DWORD iType, iSize = 0;
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, NULL, &iSize );
if (err == ERROR_SUCCESS) {
if (iType == REG_BINARY) {
iValBufSize = iSize;
iValBufSize = __max( iValBufSize, iOffset + iBufSize ); /// Extend the buffer if necessary
pValBuf = (LPBYTE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, iValBufSize );
if (pValBuf) {
err = RegQueryValueEx( hKey, szRegValue, NULL, &iType, pValBuf, &iSize );
} else {
err = ERROR_OUTOFMEMORY;
bFatalError = TRUE; /// We can't continue
}
} else {
err = ERROR_DATATYPE_MISMATCH;
bFatalError = TRUE; /// We can't continue
}
}
if (!bFatalError) {
if (err != ERROR_SUCCESS) {
// New value
iValBufSize = iOffset + iBufSize;
pValBuf = (LPBYTE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, iValBufSize );
err = pValBuf ? ERROR_SUCCESS : ERROR_OUTOFMEMORY;
}
if (err == ERROR_SUCCESS) {
// Modify
memmove( pValBuf + iOffset, pBuf, iBufSize );
// Write
err = RegSetValueEx( hKey, szRegValue, 0, REG_BINARY, pValBuf, iValBufSize );
}
}
// Cleanup
if (pValBuf)
HeapFree( GetProcessHeap(), 0, pValBuf );
RegCloseKey( hKey );
}
} else {
err = ERROR_PATH_NOT_FOUND; /// RegistryParsePath
}
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ [exported] RegBinaryInsertString
// ----------------------------------------------------------------------
//+ Input:
// [Stack] Registry key (ex: "HKCU\Software\MyCompany")
// [Stack] Registry value. If empty, the default (unnamed) value is set
// [Stack] Additional key flags (ex: KEY_WOW64_64KEY)
// [Stack] Offset (bytes)
// [Stack] The WCS (wide-character string)
//+ Output:
// [Stack] Win32 error
//+ Example:
// NSutils::RegBinaryInsertString /NOUNLOAD "HKCU\Software\MyCompany" "MyValue" ${KEY_WOW64_64KEY} 100 "My string"
// Pop $0; Win32 error
__declspec(dllexport)
void RegBinaryInsertString(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
DWORD err = ERROR_SUCCESS;
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store any NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof( TCHAR ) * string_size );
if (pszBuf) {
TCHAR szKey[512], szValue[128];
int iOffset;
DWORD dwFlags;
err = ERROR_INVALID_PARAMETER;
/// Param1: Registry key
if (popstring( pszBuf ) == 0) {
lstrcpyn( szKey, pszBuf, ARRAYSIZE( szKey ) );
/// Param2: Registry value
if (popstring( pszBuf ) == 0) {
lstrcpyn( szValue, pszBuf, ARRAYSIZE( szValue ) );
/// Param3: Additional key flags
dwFlags = popint();
/// Param4: Byte offset
iOffset = popint();
/// Param5: String
if (popstring( pszBuf ) == 0) {
// Write
#ifdef _UNICODE
err = RegBinaryWriteBufImpl( szKey, szValue, dwFlags, iOffset, pszBuf, lstrlen( pszBuf ) * sizeof( WCHAR ) );
#else
/// MBCS -> Unicode
ULONG iLen = lstrlen( pszBuf );
LPWSTR pszBufW = (LPWSTR)GlobalAlloc( GPTR, (iLen + 1) * sizeof( WCHAR ) );
if (pszBuf) {
iLen = MultiByteToWideChar( CP_ACP, 0, pszBuf, -1, pszBufW, iLen + 1 );
if (iLen > 0) {
/// Write
err = RegBinaryWriteBufImpl( szKey, szValue, dwFlags, iOffset, pszBufW, (iLen - 1)*sizeof( WCHAR ) );
} else {
err = GetLastError();
}
GlobalFree( pszBufW );
} else {
err = GetLastError();
}
#endif
}
}
}
/// Free memory
GlobalFree( pszBuf );
} else {
err = ERROR_OUTOFMEMORY;
}
/// Return value
pushintptr( err );
}