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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
// structs
typedef const struct _BUILT_IN_VARIABLE_DECLARATION
{
LPCWSTR wzVariable;
PFN_INITIALIZEVARIABLE pfnInitialize;
DWORD_PTR dwpInitializeData;
BOOL fPersist;
BOOL fOverridable;
} BUILT_IN_VARIABLE_DECLARATION;
typedef const struct _WELL_KNOWN_VARIABLE_DECLARATION
{
LPCWSTR wzVariable;
BOOL fPersist;
} WELL_KNOWN_VARIABLE_DECLARATION;
// constants
const DWORD GROW_VARIABLE_ARRAY = 3;
enum OS_INFO_VARIABLE
{
OS_INFO_VARIABLE_NONE,
OS_INFO_VARIABLE_VersionNT,
OS_INFO_VARIABLE_VersionNT64,
OS_INFO_VARIABLE_ServicePackLevel,
OS_INFO_VARIABLE_NTProductType,
OS_INFO_VARIABLE_NTSuiteBackOffice,
OS_INFO_VARIABLE_NTSuiteDataCenter,
OS_INFO_VARIABLE_NTSuiteEnterprise,
OS_INFO_VARIABLE_NTSuitePersonal,
OS_INFO_VARIABLE_NTSuiteSmallBusiness,
OS_INFO_VARIABLE_NTSuiteSmallBusinessRestricted,
OS_INFO_VARIABLE_NTSuiteWebServer,
OS_INFO_VARIABLE_CompatibilityMode,
OS_INFO_VARIABLE_TerminalServer,
OS_INFO_VARIABLE_ProcessorArchitecture,
OS_INFO_VARIABLE_WindowsBuildNumber,
};
enum SET_VARIABLE
{
SET_VARIABLE_NOT_BUILTIN,
SET_VARIABLE_OVERRIDE_BUILTIN,
SET_VARIABLE_OVERRIDE_PERSISTED_BUILTINS,
SET_VARIABLE_ANY,
};
// internal function declarations
static HRESULT FormatString(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzIn,
__out_z_opt LPWSTR* psczOut,
__out_opt SIZE_T* pcchOut,
__in BOOL fObfuscateHiddenVariables,
__out BOOL* pfContainsHiddenVariable
);
static HRESULT GetFormatted(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out_z LPWSTR* psczValue,
__out BOOL* pfContainsHiddenVariable
);
static HRESULT AddBuiltInVariable(
__in BURN_VARIABLES* pVariables,
__in LPCWSTR wzVariable,
__in PFN_INITIALIZEVARIABLE pfnInitialize,
__in DWORD_PTR dwpInitializeData,
__in BOOL fPersist,
__in BOOL fOverridable
);
static HRESULT AddWellKnownVariable(
__in BURN_VARIABLES* pVariables,
__in LPCWSTR wzVariable,
__in BOOL fPersisted
);
static HRESULT GetVariable(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out BURN_VARIABLE** ppVariable
);
static HRESULT FindVariableIndexByName(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out DWORD* piVariable
);
static HRESULT InsertUserVariable(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in DWORD iPosition
);
static HRESULT InsertVariable(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in DWORD iPosition
);
static HRESULT SetVariableValue(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in BURN_VARIANT* pVariant,
__in SET_VARIABLE setBuiltin,
__in BOOL fLog
);
static HRESULT InitializeVariableVersionNT(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableNativeMachine(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableOsInfo(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableSystemInfo(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableComputerName(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableVersionMsi(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableCsidlFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableWindowsVolumeFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableTempFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableSystemFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariablePrivileged(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableProcessTokenPrivilege(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeSystemLanguageID(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeUserUILanguageID(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeUserLanguageID(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableString(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableNumeric(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariable6432Folder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableDate(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableInstallerName(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableInstallerVersion(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableInstallerInformationalVersion(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableVersion(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT InitializeVariableLogonUser(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
static HRESULT Get64bitFolderFromRegistry(
__in int nFolder,
__deref_out_z LPWSTR* psczPath
);
#if !defined(_WIN64)
static HRESULT InitializeVariableRegistryFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
);
#endif
// function definitions
extern "C" HRESULT VariableInitialize(
__in BURN_VARIABLES* pVariables
)
{
HRESULT hr = S_OK;
::InitializeCriticalSection(&pVariables->csAccess);
const BUILT_IN_VARIABLE_DECLARATION vrgBuiltInVariables[] = {
{L"AdminToolsFolder", InitializeVariableCsidlFolder, CSIDL_ADMINTOOLS},
{L"AppDataFolder", InitializeVariableCsidlFolder, CSIDL_APPDATA},
{L"CommonAppDataFolder", InitializeVariableCsidlFolder, CSIDL_COMMON_APPDATA},
#if defined(_WIN64)
{L"CommonFiles64Folder", InitializeVariableCsidlFolder, CSIDL_PROGRAM_FILES_COMMON},
{L"CommonFilesFolder", InitializeVariableCsidlFolder, CSIDL_PROGRAM_FILES_COMMONX86},
#else
{L"CommonFiles64Folder", InitializeVariableRegistryFolder, CSIDL_PROGRAM_FILES_COMMON},
{L"CommonFilesFolder", InitializeVariableCsidlFolder, CSIDL_PROGRAM_FILES_COMMON},
#endif
{L"CommonFiles6432Folder", InitializeVariable6432Folder, CSIDL_PROGRAM_FILES_COMMON},
{L"CompatibilityMode", InitializeVariableOsInfo, OS_INFO_VARIABLE_CompatibilityMode},
{VARIABLE_DATE, InitializeVariableDate, 0},
{L"ComputerName", InitializeVariableComputerName, 0},
{L"DesktopFolder", InitializeVariableCsidlFolder, CSIDL_DESKTOP},
{L"FavoritesFolder", InitializeVariableCsidlFolder, CSIDL_FAVORITES},
{L"FontsFolder", InitializeVariableCsidlFolder, CSIDL_FONTS},
{VARIABLE_INSTALLERNAME, InitializeVariableInstallerName, 0},
{VARIABLE_INSTALLERVERSION, InitializeVariableInstallerVersion, 0},
{VARIABLE_INSTALLERINFORMATIONALVERSION, InitializeVariableInstallerInformationalVersion, 0},
{L"LocalAppDataFolder", InitializeVariableCsidlFolder, CSIDL_LOCAL_APPDATA},
{VARIABLE_LOGONUSER, InitializeVariableLogonUser, 0},
{L"MyPicturesFolder", InitializeVariableCsidlFolder, CSIDL_MYPICTURES},
{L"NativeMachine", InitializeVariableNativeMachine, 0},
{L"NTProductType", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTProductType},
{L"NTSuiteBackOffice", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuiteBackOffice},
{L"NTSuiteDataCenter", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuiteDataCenter},
{L"NTSuiteEnterprise", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuiteEnterprise},
{L"NTSuitePersonal", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuitePersonal},
{L"NTSuiteSmallBusiness", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuiteSmallBusiness},
{L"NTSuiteSmallBusinessRestricted", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuiteSmallBusinessRestricted},
{L"NTSuiteWebServer", InitializeVariableOsInfo, OS_INFO_VARIABLE_NTSuiteWebServer},
{L"PersonalFolder", InitializeVariableCsidlFolder, CSIDL_PERSONAL},
{L"Privileged", InitializeVariablePrivileged, 0},
{L"ProcessorArchitecture", InitializeVariableSystemInfo, OS_INFO_VARIABLE_ProcessorArchitecture},
#if defined(_WIN64)
{L"ProgramFiles64Folder", InitializeVariableCsidlFolder, CSIDL_PROGRAM_FILES},
{L"ProgramFilesFolder", InitializeVariableCsidlFolder, CSIDL_PROGRAM_FILESX86},
#else
{L"ProgramFiles64Folder", InitializeVariableRegistryFolder, CSIDL_PROGRAM_FILES},
{L"ProgramFilesFolder", InitializeVariableCsidlFolder, CSIDL_PROGRAM_FILES},
#endif
{L"ProgramFiles6432Folder", InitializeVariable6432Folder, CSIDL_PROGRAM_FILES},
{L"ProgramMenuFolder", InitializeVariableCsidlFolder, CSIDL_PROGRAMS},
{VARIABLE_REBOOTPENDING, InitializeVariableNumeric, 0},
{L"SendToFolder", InitializeVariableCsidlFolder, CSIDL_SENDTO},
{L"ServicePackLevel", InitializeVariableVersionNT, OS_INFO_VARIABLE_ServicePackLevel},
{L"StartMenuFolder", InitializeVariableCsidlFolder, CSIDL_STARTMENU},
{L"StartupFolder", InitializeVariableCsidlFolder, CSIDL_STARTUP},
{L"SystemFolder", InitializeVariableSystemFolder, FALSE},
{L"System64Folder", InitializeVariableSystemFolder, TRUE},
{L"SystemLanguageID", InitializeSystemLanguageID, 0},
{L"TempFolder", InitializeVariableTempFolder, 0},
{L"TemplateFolder", InitializeVariableCsidlFolder, CSIDL_TEMPLATES},
{L"TerminalServer", InitializeVariableOsInfo, OS_INFO_VARIABLE_TerminalServer},
{L"UserUILanguageID", InitializeUserUILanguageID, 0},
{L"UserLanguageID", InitializeUserLanguageID, 0},
{L"VersionMsi", InitializeVariableVersionMsi, 0},
{L"VersionNT", InitializeVariableVersionNT, OS_INFO_VARIABLE_VersionNT},
{L"VersionNT64", InitializeVariableVersionNT, OS_INFO_VARIABLE_VersionNT64},
{L"WindowsBuildNumber", InitializeVariableVersionNT, OS_INFO_VARIABLE_WindowsBuildNumber},
{L"WindowsFolder", InitializeVariableCsidlFolder, CSIDL_WINDOWS},
{L"WindowsVolume", InitializeVariableWindowsVolumeFolder, 0},
{BURN_BUNDLE_ACTION, InitializeVariableNumeric, 0, FALSE, TRUE},
{L"WixCanRestart", InitializeVariableProcessTokenPrivilege, (DWORD_PTR)SE_SHUTDOWN_NAME},
{BURN_BUNDLE_COMMAND_LINE_ACTION, InitializeVariableNumeric, 0, FALSE, TRUE},
{BURN_BUNDLE_EXECUTE_PACKAGE_CACHE_FOLDER, InitializeVariableString, NULL, FALSE, TRUE},
{BURN_BUNDLE_EXECUTE_PACKAGE_ACTION, InitializeVariableString, NULL, FALSE, TRUE},
{BURN_BUNDLE_FORCED_RESTART_PACKAGE, InitializeVariableString, NULL, TRUE, TRUE},
{BURN_BUNDLE_INSTALLED, InitializeVariableNumeric, 0},
{BURN_BUNDLE_ELEVATED, InitializeVariableNumeric, 0, FALSE, TRUE},
{BURN_BUNDLE_ACTIVE_PARENT, InitializeVariableString, NULL, FALSE, TRUE},
{BURN_BUNDLE_PROVIDER_KEY, InitializeVariableString, (DWORD_PTR)L"", FALSE, TRUE},
{BURN_BUNDLE_TAG, InitializeVariableString, (DWORD_PTR)L"", FALSE, TRUE},
{BURN_BUNDLE_UILEVEL, InitializeVariableNumeric, 0, FALSE, TRUE},
{BURN_BUNDLE_VERSION, InitializeVariableVersion, (DWORD_PTR)L"0", FALSE, TRUE},
};
const WELL_KNOWN_VARIABLE_DECLARATION vrgWellKnownVariableNames[] =
{
{ BURN_BUNDLE_LAYOUT_DIRECTORY },
{ BURN_BUNDLE_NAME, TRUE },
{ BURN_BUNDLE_INPROGRESS_NAME, TRUE },
{ BURN_BUNDLE_LAST_USED_SOURCE, TRUE },
{ BURN_BUNDLE_MANUFACTURER, TRUE },
{ BURN_BUNDLE_ORIGINAL_SOURCE, TRUE },
{ BURN_BUNDLE_ORIGINAL_SOURCE_FOLDER, TRUE },
};
for (DWORD i = 0; i < countof(vrgBuiltInVariables); ++i)
{
BUILT_IN_VARIABLE_DECLARATION* pBuiltInVariable = &vrgBuiltInVariables[i];
hr = AddBuiltInVariable(pVariables, pBuiltInVariable->wzVariable, pBuiltInVariable->pfnInitialize, pBuiltInVariable->dwpInitializeData, pBuiltInVariable->fPersist, pBuiltInVariable->fOverridable);
ExitOnFailure(hr, "Failed to add built-in variable: %ls.", pBuiltInVariable->wzVariable);
}
for (DWORD i = 0; i < countof(vrgWellKnownVariableNames); ++i)
{
WELL_KNOWN_VARIABLE_DECLARATION* pWellKnownVariable = &vrgWellKnownVariableNames[i];
hr = AddWellKnownVariable(pVariables, pWellKnownVariable->wzVariable, pWellKnownVariable->fPersist);
ExitOnFailure(hr, "Failed to add well-known variable: %ls.", pWellKnownVariable->wzVariable);
}
LExit:
return hr;
}
extern "C" HRESULT VariablesParseFromXml(
__in BURN_VARIABLES* pVariables,
__in IXMLDOMNode* pixnBundle
)
{
HRESULT hr = S_OK;
BOOL fXmlFound = FALSE;
IXMLDOMNodeList* pixnNodes = NULL;
IXMLDOMNode* pixnNode = NULL;
DWORD cNodes = 0;
LPWSTR sczId = NULL;
LPWSTR scz = NULL;
BURN_VARIANT value = { };
BURN_VARIANT_TYPE valueType = BURN_VARIANT_TYPE_NONE;
BOOL fHidden = FALSE;
BOOL fPersisted = FALSE;
DWORD iVariable = 0;
::EnterCriticalSection(&pVariables->csAccess);
// select variable nodes
hr = XmlSelectNodes(pixnBundle, L"Variable", &pixnNodes);
ExitOnFailure(hr, "Failed to select variable nodes.");
// get variable node count
hr = pixnNodes->get_length((long*)&cNodes);
ExitOnFailure(hr, "Failed to get variable node count.");
// parse variable elements
for (DWORD i = 0; i < cNodes; ++i)
{
hr = XmlNextElement(pixnNodes, &pixnNode, NULL);
ExitOnFailure(hr, "Failed to get next node.");
// @Id
hr = XmlGetAttributeEx(pixnNode, L"Id", &sczId);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Id.");
// @Hidden
hr = XmlGetYesNoAttribute(pixnNode, L"Hidden", &fHidden);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Hidden.");
// @Persisted
hr = XmlGetYesNoAttribute(pixnNode, L"Persisted", &fPersisted);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Persisted.");
// @Value
hr = XmlGetAttributeEx(pixnNode, L"Value", &scz);
ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @Value.");
if (fXmlFound)
{
hr = BVariantSetString(&value, scz, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
// @Type
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"formatted", -1))
{
if (!fHidden)
{
LogStringLine(REPORT_STANDARD, "Initializing formatted variable '%ls' to value '%ls'", sczId, value.sczValue);
}
valueType = BURN_VARIANT_TYPE_FORMATTED;
}
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"numeric", -1))
{
if (!fHidden)
{
LogStringLine(REPORT_STANDARD, "Initializing numeric variable '%ls' to value '%ls'", sczId, value.sczValue);
}
valueType = BURN_VARIANT_TYPE_NUMERIC;
}
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"string", -1))
{
if (!fHidden)
{
LogStringLine(REPORT_STANDARD, "Initializing string variable '%ls' to value '%ls'", sczId, value.sczValue);
}
valueType = BURN_VARIANT_TYPE_STRING;
}
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"version", -1))
{
if (!fHidden)
{
LogStringLine(REPORT_STANDARD, "Initializing version variable '%ls' to value '%ls'", sczId, value.sczValue);
}
valueType = BURN_VARIANT_TYPE_VERSION;
}
else
{
ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
}
}
else
{
valueType = BURN_VARIANT_TYPE_NONE;
}
if (fHidden)
{
LogStringLine(REPORT_STANDARD, "Initializing hidden variable '%ls'", sczId);
}
// change value variant to correct type
hr = BVariantChangeType(&value, valueType);
ExitOnFailure(hr, "Failed to change variant type.");
if (BURN_VARIANT_TYPE_VERSION == valueType && value.pValue->fInvalid)
{
LogId(REPORT_WARNING, MSG_VARIABLE_INVALID_VERSION, sczId);
}
// find existing variable
hr = FindVariableIndexByName(pVariables, sczId, &iVariable);
ExitOnFailure(hr, "Failed to find variable value '%ls'.", sczId);
// insert element if not found
if (S_FALSE == hr)
{
hr = InsertUserVariable(pVariables, sczId, iVariable);
ExitOnFailure(hr, "Failed to insert variable '%ls'.", sczId);
}
else if (BURN_VARIABLE_INTERNAL_TYPE_NORMAL < pVariables->rgVariables[iVariable].internalType)
{
ExitWithRootFailure(hr, E_INVALIDARG, "Attempt to add built-in variable: %ls", sczId);
}
else
{
ExitWithRootFailure(hr, E_INVALIDARG, "Attempt to add variable again: %ls", sczId);
}
pVariables->rgVariables[iVariable].fHidden = fHidden;
pVariables->rgVariables[iVariable].fPersisted = fPersisted;
// update variable value
hr = BVariantSetValue(&pVariables->rgVariables[iVariable].Value, &value);
ExitOnFailure(hr, "Failed to set value of variable: %ls", sczId);
// prepare next iteration
ReleaseNullObject(pixnNode);
BVariantUninitialize(&value);
ReleaseNullStrSecure(scz);
}
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
ReleaseObject(pixnNodes);
ReleaseObject(pixnNode);
ReleaseStr(scz);
ReleaseStr(sczId);
BVariantUninitialize(&value);
return hr;
}
extern "C" void VariablesUninitialize(
__in BURN_VARIABLES* pVariables
)
{
::DeleteCriticalSection(&pVariables->csAccess);
if (pVariables->rgVariables)
{
for (DWORD i = 0; i < pVariables->cVariables; ++i)
{
BURN_VARIABLE* pVariable = &pVariables->rgVariables[i];
if (pVariable)
{
ReleaseStr(pVariable->sczName);
BVariantUninitialize(&pVariable->Value);
}
}
MemFree(pVariables->rgVariables);
}
}
extern "C" void VariablesDump(
__in BURN_VARIABLES* pVariables
)
{
HRESULT hr = S_OK;
LPWSTR sczValue = NULL;
for (DWORD i = 0; i < pVariables->cVariables; ++i)
{
BURN_VARIABLE* pVariable = &pVariables->rgVariables[i];
if (pVariable && BURN_VARIANT_TYPE_NONE != pVariable->Value.Type)
{
hr = StrAllocFormatted(&sczValue, L"%ls = [%ls]", pVariable->sczName, pVariable->sczName);
if (SUCCEEDED(hr))
{
if (pVariable->fHidden)
{
hr = VariableFormatStringObfuscated(pVariables, sczValue, &sczValue, NULL);
}
else
{
hr = VariableFormatString(pVariables, sczValue, &sczValue, NULL);
}
}
if (FAILED(hr))
{
// already logged; best-effort to dump the rest on our way out the door
continue;
}
LogId(REPORT_VERBOSE, MSG_VARIABLE_DUMP, sczValue);
ReleaseNullStrSecure(sczValue);
}
}
StrSecureZeroFreeString(sczValue);
}
extern "C" HRESULT VariableGetNumeric(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out LONGLONG* pllValue
)
{
HRESULT hr = S_OK;
BURN_VARIABLE* pVariable = NULL;
::EnterCriticalSection(&pVariables->csAccess);
hr = GetVariable(pVariables, wzVariable, &pVariable);
if (SUCCEEDED(hr) && BURN_VARIANT_TYPE_NONE == pVariable->Value.Type)
{
ExitFunction1(hr = E_NOTFOUND);
}
else if (E_NOTFOUND == hr)
{
ExitFunction();
}
ExitOnFailure(hr, "Failed to get value of variable: %ls", wzVariable);
hr = BVariantGetNumeric(&pVariable->Value, pllValue);
ExitOnFailure(hr, "Failed to get value as numeric for variable: %ls", wzVariable);
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
return hr;
}
extern "C" HRESULT VariableGetString(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out_z LPWSTR* psczValue
)
{
HRESULT hr = S_OK;
BURN_VARIABLE* pVariable = NULL;
::EnterCriticalSection(&pVariables->csAccess);
hr = GetVariable(pVariables, wzVariable, &pVariable);
if (SUCCEEDED(hr) && BURN_VARIANT_TYPE_NONE == pVariable->Value.Type)
{
ExitFunction1(hr = E_NOTFOUND);
}
else if (E_NOTFOUND == hr)
{
ExitFunction();
}
ExitOnFailure(hr, "Failed to get value of variable: %ls", wzVariable);
hr = BVariantGetString(&pVariable->Value, psczValue);
ExitOnFailure(hr, "Failed to get value as string for variable: %ls", wzVariable);
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
return hr;
}
extern "C" HRESULT VariableGetVersion(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in VERUTIL_VERSION** ppValue
)
{
HRESULT hr = S_OK;
BURN_VARIABLE* pVariable = NULL;
::EnterCriticalSection(&pVariables->csAccess);
hr = GetVariable(pVariables, wzVariable, &pVariable);
if (SUCCEEDED(hr) && BURN_VARIANT_TYPE_NONE == pVariable->Value.Type)
{
ExitFunction1(hr = E_NOTFOUND);
}
else if (E_NOTFOUND == hr)
{
ExitFunction();
}
ExitOnFailure(hr, "Failed to get value of variable: %ls", wzVariable);
hr = BVariantGetVersionHidden(&pVariable->Value, pVariable->fHidden, ppValue);
ExitOnFailure(hr, "Failed to get value as version for variable: %ls", wzVariable);
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
return hr;
}
extern "C" HRESULT VariableGetVariant(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
BURN_VARIABLE* pVariable = NULL;
::EnterCriticalSection(&pVariables->csAccess);
hr = GetVariable(pVariables, wzVariable, &pVariable);
if (E_NOTFOUND == hr)
{
ExitFunction();
}
ExitOnFailure(hr, "Failed to get value of variable: %ls", wzVariable);
hr = BVariantCopy(&pVariable->Value, pValue);
ExitOnFailure(hr, "Failed to copy value of variable: %ls", wzVariable);
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
return hr;
}
extern "C" HRESULT VariableGetFormatted(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out_z LPWSTR* psczValue,
__out BOOL* pfContainsHiddenVariable
)
{
HRESULT hr = S_OK;
if (pfContainsHiddenVariable)
{
*pfContainsHiddenVariable = FALSE;
}
hr = GetFormatted(pVariables, wzVariable, psczValue, pfContainsHiddenVariable);
return hr;
}
extern "C" HRESULT VariableSetNumeric(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in LONGLONG llValue,
__in BOOL fOverwriteBuiltIn
)
{
BURN_VARIANT variant = { };
variant.llValue = llValue;
variant.Type = BURN_VARIANT_TYPE_NUMERIC;
return SetVariableValue(pVariables, wzVariable, &variant, fOverwriteBuiltIn ? SET_VARIABLE_OVERRIDE_BUILTIN : SET_VARIABLE_NOT_BUILTIN, TRUE);
}
extern "C" HRESULT VariableSetString(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in_z_opt LPCWSTR wzValue,
__in BOOL fOverwriteBuiltIn,
__in BOOL fFormatted
)
{
BURN_VARIANT variant = { };
variant.sczValue = (LPWSTR)wzValue;
variant.Type = fFormatted ? BURN_VARIANT_TYPE_FORMATTED : BURN_VARIANT_TYPE_STRING;
return SetVariableValue(pVariables, wzVariable, &variant, fOverwriteBuiltIn ? SET_VARIABLE_OVERRIDE_BUILTIN : SET_VARIABLE_NOT_BUILTIN, TRUE);
}
extern "C" HRESULT VariableSetVersion(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in VERUTIL_VERSION* pValue,
__in BOOL fOverwriteBuiltIn
)
{
BURN_VARIANT variant = { };
variant.pValue = pValue;
variant.Type = BURN_VARIANT_TYPE_VERSION;
return SetVariableValue(pVariables, wzVariable, &variant, fOverwriteBuiltIn ? SET_VARIABLE_OVERRIDE_BUILTIN : SET_VARIABLE_NOT_BUILTIN, TRUE);
}
extern "C" HRESULT VariableSetVariant(
__in BURN_VARIABLES * pVariables,
__in_z LPCWSTR wzVariable,
__in BURN_VARIANT * pVariant
)
{
return SetVariableValue(pVariables, wzVariable, pVariant, SET_VARIABLE_NOT_BUILTIN, TRUE);
}
extern "C" HRESULT VariableFormatString(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzIn,
__out_z_opt LPWSTR* psczOut,
__out_opt SIZE_T* pcchOut
)
{
return FormatString(pVariables, wzIn, psczOut, pcchOut, FALSE, NULL);
}
extern "C" HRESULT VariableFormatStringObfuscated(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzIn,
__out_z_opt LPWSTR* psczOut,
__out_opt SIZE_T* pcchOut
)
{
return FormatString(pVariables, wzIn, psczOut, pcchOut, TRUE, NULL);
}
extern "C" HRESULT VariableEscapeString(
__in_z LPCWSTR wzIn,
__out_z LPWSTR* psczOut
)
{
HRESULT hr = S_OK;
LPCWSTR wzRead = NULL;
LPWSTR pwzEscaped = NULL;
LPWSTR pwz = NULL;
SIZE_T i = 0;
// allocate buffer for escaped string
hr = StrAlloc(&pwzEscaped, lstrlenW(wzIn) + 1);
ExitOnFailure(hr, "Failed to allocate buffer for escaped string.");
// read through string and move characters, inserting escapes as needed
wzRead = wzIn;
for (;;)
{
// find next character needing escaping
i = wcscspn(wzRead, L"[]{}");
// copy skipped characters
if (0 < i)
{
hr = StrAllocConcat(&pwzEscaped, wzRead, i);
ExitOnFailure(hr, "Failed to append characters.");
}
if (L'\0' == wzRead[i])
{
break; // end reached
}
// escape character
hr = StrAllocFormatted(&pwz, L"[\\%c]", wzRead[i]);
ExitOnFailure(hr, "Failed to format escape sequence.");
hr = StrAllocConcat(&pwzEscaped, pwz, 0);
ExitOnFailure(hr, "Failed to append escape sequence.");
// update read pointer
wzRead += i + 1;
}
// return value
hr = StrAllocString(psczOut, pwzEscaped, 0);
ExitOnFailure(hr, "Failed to copy string.");
LExit:
ReleaseStr(pwzEscaped);
ReleaseStr(pwz);
return hr;
}
extern "C" HRESULT VariableSerialize(
__in BURN_VARIABLES* pVariables,
__in BOOL fPersisting,
__inout BYTE** ppbBuffer,
__inout SIZE_T* piBuffer
)
{
HRESULT hr = S_OK;
BOOL fIncluded = FALSE;
LONGLONG ll = 0;
LPWSTR scz = NULL;
::EnterCriticalSection(&pVariables->csAccess);
// Write variable count.
hr = BuffWriteNumber(ppbBuffer, piBuffer, pVariables->cVariables);
ExitOnFailure(hr, "Failed to write variable count.");
// Write variables.
for (DWORD i = 0; i < pVariables->cVariables; ++i)
{
BURN_VARIABLE* pVariable = &pVariables->rgVariables[i];
// If we aren't persisting, include only variables that aren't rejected by the elevated process.
// If we are persisting, include only variables that should be persisted.
fIncluded = (!fPersisting && BURN_VARIABLE_INTERNAL_TYPE_BUILTIN != pVariable->internalType) ||
(fPersisting && pVariable->fPersisted);
// Write included flag.
hr = BuffWriteNumber(ppbBuffer, piBuffer, (DWORD)fIncluded);
ExitOnFailure(hr, "Failed to write included flag.");
if (!fIncluded)
{
continue;
}
// Write variable name.
hr = BuffWriteString(ppbBuffer, piBuffer, pVariable->sczName);
ExitOnFailure(hr, "Failed to write variable name.");
// Write variable value type.
hr = BuffWriteNumber(ppbBuffer, piBuffer, (DWORD)pVariable->Value.Type);
ExitOnFailure(hr, "Failed to write variable value type.");
// Write variable value.
switch (pVariable->Value.Type)
{
case BURN_VARIANT_TYPE_NONE:
break;
case BURN_VARIANT_TYPE_NUMERIC:
hr = BVariantGetNumeric(&pVariable->Value, &ll);
ExitOnFailure(hr, "Failed to get numeric.");
hr = BuffWriteNumber64(ppbBuffer, piBuffer, static_cast<DWORD64>(ll));
ExitOnFailure(hr, "Failed to write variable value as number.");
SecureZeroMemory(&ll, sizeof(ll));
break;
case BURN_VARIANT_TYPE_VERSION: __fallthrough;
case BURN_VARIANT_TYPE_FORMATTED: __fallthrough;
case BURN_VARIANT_TYPE_STRING:
hr = BVariantGetString(&pVariable->Value, &scz);
ExitOnFailure(hr, "Failed to get string.");
hr = BuffWriteString(ppbBuffer, piBuffer, scz);
ExitOnFailure(hr, "Failed to write variable value as string.");
ReleaseNullStrSecure(scz);
break;
default:
hr = E_INVALIDARG;
ExitOnFailure(hr, "Unsupported variable type.");
}
}
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
SecureZeroMemory(&ll, sizeof(ll));
StrSecureZeroFreeString(scz);
return hr;
}
extern "C" HRESULT VariableDeserialize(
__in BURN_VARIABLES* pVariables,
__in BOOL fWasPersisted,
__in_bcount(cbBuffer) BYTE* pbBuffer,
__in SIZE_T cbBuffer,
__inout SIZE_T* piBuffer
)
{
HRESULT hr = S_OK;
DWORD cVariables = 0;
LPWSTR sczName = NULL;
BOOL fIncluded = FALSE;
BURN_VARIANT value = { };
LPWSTR scz = NULL;
DWORD64 qw = 0;
VERUTIL_VERSION* pVersion = NULL;
::EnterCriticalSection(&pVariables->csAccess);
// Read variable count.
hr = BuffReadNumber(pbBuffer, cbBuffer, piBuffer, &cVariables);
ExitOnFailure(hr, "Failed to read variable count.");
// Read variables.
for (DWORD i = 0; i < cVariables; ++i)
{
// Read variable included flag.
hr = BuffReadNumber(pbBuffer, cbBuffer, piBuffer, (DWORD*)&fIncluded);
ExitOnFailure(hr, "Failed to read variable included flag.");
if (!fIncluded)
{
continue; // if variable is not included, skip.
}
// Read variable name.
hr = BuffReadString(pbBuffer, cbBuffer, piBuffer, &sczName);
ExitOnFailure(hr, "Failed to read variable name.");
// Read variable value type.
hr = BuffReadNumber(pbBuffer, cbBuffer, piBuffer, (DWORD*)&value.Type);
ExitOnFailure(hr, "Failed to read variable value type.");
// Read variable value.
switch (value.Type)
{
case BURN_VARIANT_TYPE_NONE:
break;
case BURN_VARIANT_TYPE_NUMERIC:
hr = BuffReadNumber64(pbBuffer, cbBuffer, piBuffer, &qw);
ExitOnFailure(hr, "Failed to read variable value as number.");
hr = BVariantSetNumeric(&value, static_cast<LONGLONG>(qw));
ExitOnFailure(hr, "Failed to set variable value.");
SecureZeroMemory(&qw, sizeof(qw));
break;
case BURN_VARIANT_TYPE_VERSION:
hr = BuffReadString(pbBuffer, cbBuffer, piBuffer, &scz);
ExitOnFailure(hr, "Failed to read variable value as string.");
hr = VerParseVersion(scz, 0, FALSE, &pVersion);
ExitOnFailure(hr, "Failed to parse variable value as version.");
hr = BVariantSetVersion(&value, pVersion);
ExitOnFailure(hr, "Failed to set variable value.");
SecureZeroMemory(&qw, sizeof(qw));
break;
case BURN_VARIANT_TYPE_FORMATTED: __fallthrough;
case BURN_VARIANT_TYPE_STRING:
hr = BuffReadString(pbBuffer, cbBuffer, piBuffer, &scz);
ExitOnFailure(hr, "Failed to read variable value as string.");
hr = BVariantSetString(&value, scz, NULL, BURN_VARIANT_TYPE_FORMATTED == value.Type);
ExitOnFailure(hr, "Failed to set variable value.");
ReleaseNullStrSecure(scz);
break;
default:
hr = E_INVALIDARG;
ExitOnFailure(hr, "Unsupported variable type.");
}
// Set variable.
hr = SetVariableValue(pVariables, sczName, &value, fWasPersisted ? SET_VARIABLE_OVERRIDE_PERSISTED_BUILTINS : SET_VARIABLE_ANY, FALSE);
ExitOnFailure(hr, "Failed to set variable.");
// Clean up.
BVariantUninitialize(&value);
}
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
ReleaseVerutilVersion(pVersion);
ReleaseStr(sczName);
BVariantUninitialize(&value);
SecureZeroMemory(&qw, sizeof(qw));
StrSecureZeroFreeString(scz);
return hr;
}
extern "C" HRESULT VariableStrAlloc(
__in BOOL fZeroOnRealloc,
__deref_out_ecount_part(cch, 0) LPWSTR* ppwz,
__in DWORD_PTR cch
)
{
HRESULT hr = S_OK;
if (fZeroOnRealloc)
{
hr = StrAllocSecure(ppwz, cch);
}
else
{
hr = StrAlloc(ppwz, cch);
}
return hr;
}
extern "C" HRESULT VariableStrAllocString(
__in BOOL fZeroOnRealloc,
__deref_out_ecount_z(cchSource + 1) LPWSTR* ppwz,
__in_z LPCWSTR wzSource,
__in DWORD_PTR cchSource
)
{
HRESULT hr = S_OK;
if (fZeroOnRealloc)
{
hr = StrAllocStringSecure(ppwz, wzSource, cchSource);
}
else
{
hr = StrAllocString(ppwz, wzSource, cchSource);
}
return hr;
}
extern "C" HRESULT VariableStrAllocConcat(
__in BOOL fZeroOnRealloc,
__deref_out_z LPWSTR* ppwz,
__in_z LPCWSTR wzSource,
__in DWORD_PTR cchSource
)
{
HRESULT hr = S_OK;
if (fZeroOnRealloc)
{
hr = StrAllocConcatSecure(ppwz, wzSource, cchSource);
}
else
{
hr = StrAllocConcat(ppwz, wzSource, cchSource);
}
return hr;
}
extern "C" HRESULT __cdecl VariableStrAllocFormatted(
__in BOOL fZeroOnRealloc,
__deref_out_z LPWSTR* ppwz,
__in __format_string LPCWSTR wzFormat,
...
)
{
HRESULT hr = S_OK;
va_list args;
va_start(args, wzFormat);
if (fZeroOnRealloc)
{
hr = StrAllocFormattedArgsSecure(ppwz, wzFormat, args);
}
else
{
hr = StrAllocFormattedArgs(ppwz, wzFormat, args);
}
va_end(args);
return hr;
}
extern "C" HRESULT VariableIsHidden(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out BOOL* pfHidden
)
{
HRESULT hr = S_OK;
BURN_VARIABLE* pVariable = NULL;
::EnterCriticalSection(&pVariables->csAccess);
hr = GetVariable(pVariables, wzVariable, &pVariable);
if (E_NOTFOUND == hr)
{
// A missing variable does not need its data hidden.
*pfHidden = FALSE;
ExitFunction1(hr = S_OK);
}
ExitOnFailure(hr, "Failed to get visibility of variable: %ls", wzVariable);
*pfHidden = pVariable->fHidden;
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
return hr;
}
extern "C" BOOL VariableIsHiddenCommandLine(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable
)
{
BURN_VARIABLE* pVariable = NULL;
BOOL fHidden = FALSE;
::EnterCriticalSection(&pVariables->csAccess);
for (DWORD i = 0; i < pVariables->cVariables; ++i)
{
pVariable = pVariables->rgVariables + i;
if (pVariable->fHidden && CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pVariable->sczName, -1, wzVariable, -1))
{
fHidden = TRUE;
break;
}
}
::LeaveCriticalSection(&pVariables->csAccess);
return fHidden;
}
// internal function definitions
static HRESULT FormatString(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzIn,
__out_z_opt LPWSTR* psczOut,
__out_opt SIZE_T* pcchOut,
__in BOOL fObfuscateHiddenVariables,
__out BOOL* pfContainsHiddenVariable
)
{
HRESULT hr = S_OK;
DWORD er = ERROR_SUCCESS;
LPWSTR sczUnformatted = NULL;
LPWSTR sczFormat = NULL;
LPCWSTR wzRead = NULL;
LPCWSTR wzOpen = NULL;
LPCWSTR wzClose = NULL;
LPWSTR scz = NULL;
LPWSTR* rgVariables = NULL;
DWORD cVariables = 0;
DWORD cch = 0;
size_t cchIn = 0;
BOOL fHidden = FALSE;
MSIHANDLE hRecord = NULL;
::EnterCriticalSection(&pVariables->csAccess);
// allocate buffer for format string
hr = ::StringCchLengthW(wzIn, STRSAFE_MAX_LENGTH, &cchIn);
ExitOnFailure(hr, "Failed to length of format string.");
hr = StrAlloc(&sczFormat, cchIn + 1);
ExitOnFailure(hr, "Failed to allocate buffer for format string.");
// read out variables from the unformatted string and build a format string
wzRead = wzIn;
for (;;)
{
// scan for opening '['
wzOpen = wcschr(wzRead, L'[');
if (!wzOpen)
{
// end reached, append the remainder of the string and end loop
hr = VariableStrAllocConcat(!fObfuscateHiddenVariables, &sczFormat, wzRead, 0);
ExitOnFailure(hr, "Failed to append string.");
break;
}
// scan for closing ']'
wzClose = wcschr(wzOpen + 1, L']');
if (!wzClose)
{
// end reached, treat unterminated expander as literal
hr = VariableStrAllocConcat(!fObfuscateHiddenVariables, &sczFormat, wzRead, 0);
ExitOnFailure(hr, "Failed to append string.");
break;
}
cch = (DWORD)(wzClose - wzOpen - 1);
if (0 == cch)
{
// blank, copy all text including the terminator
hr = VariableStrAllocConcat(!fObfuscateHiddenVariables, &sczFormat, wzRead, (DWORD_PTR)(wzClose - wzRead) + 1);
ExitOnFailure(hr, "Failed to append string.");
}
else
{
// append text preceding expander
if (wzOpen > wzRead)
{
hr = VariableStrAllocConcat(!fObfuscateHiddenVariables, &sczFormat, wzRead, (DWORD_PTR)(wzOpen - wzRead));
ExitOnFailure(hr, "Failed to append string.");
}
// get variable name
hr = VariableStrAllocString(!fObfuscateHiddenVariables, &scz, wzOpen + 1, cch);
ExitOnFailure(hr, "Failed to get variable name.");
// allocate space in variable array
if (rgVariables)
{
LPVOID pv = MemReAlloc(rgVariables, sizeof(LPWSTR) * (cVariables + 1), TRUE);
ExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to reallocate variable array.");
rgVariables = (LPWSTR*)pv;
}
else
{
rgVariables = (LPWSTR*)MemAlloc(sizeof(LPWSTR) * (cVariables + 1), TRUE);
ExitOnNull(rgVariables, hr, E_OUTOFMEMORY, "Failed to allocate variable array.");
}
// set variable value
if (2 <= cch && L'\\' == wzOpen[1])
{
// escape sequence, copy character
hr = VariableStrAllocString(!fObfuscateHiddenVariables, &rgVariables[cVariables], &wzOpen[2], 1);
}
else
{
hr = VariableIsHidden(pVariables, scz, &fHidden);
ExitOnFailure(hr, "Failed to determine variable visibility: '%ls'.", scz);
if (pfContainsHiddenVariable)
{
*pfContainsHiddenVariable |= fHidden;
}
if (fObfuscateHiddenVariables && fHidden)
{
hr = StrAllocString(&rgVariables[cVariables], L"*****", 0);
}
else
{
// get formatted variable value
hr = GetFormatted(pVariables, scz, &rgVariables[cVariables], pfContainsHiddenVariable);
if (E_NOTFOUND == hr) // variable not found
{
hr = StrAllocStringSecure(&rgVariables[cVariables], L"", 0);
}
}
}
ExitOnFailure(hr, "Failed to set variable value.");
++cVariables;
// append placeholder to format string
hr = VariableStrAllocFormatted(!fObfuscateHiddenVariables, &scz, L"[%d]", cVariables);
ExitOnFailure(hr, "Failed to format placeholder string.");
hr = VariableStrAllocConcat(!fObfuscateHiddenVariables, &sczFormat, scz, 0);
ExitOnFailure(hr, "Failed to append placeholder.");
}
// update read pointer
wzRead = wzClose + 1;
}
// create record
hRecord = ::MsiCreateRecord(cVariables);
ExitOnNull(hRecord, hr, E_OUTOFMEMORY, "Failed to allocate record.");
// set format string
er = ::MsiRecordSetStringW(hRecord, 0, sczFormat);
ExitOnWin32Error(er, hr, "Failed to set record format string.");
// copy record fields
for (DWORD i = 0; i < cVariables; ++i)
{
if (*rgVariables[i]) // not setting if blank
{
er = ::MsiRecordSetStringW(hRecord, i + 1, rgVariables[i]);
ExitOnWin32Error(er, hr, "Failed to set record string.");
}
}
// get formatted character count
cch = 0;
#pragma prefast(push)
#pragma prefast(disable:6298)
er = ::MsiFormatRecordW(NULL, hRecord, L"", &cch);
#pragma prefast(pop)
if (ERROR_MORE_DATA != er)
{
ExitOnWin32Error(er, hr, "Failed to get formatted length.");
}
// return formatted string
if (psczOut)
{
hr = VariableStrAlloc(!fObfuscateHiddenVariables, &scz, ++cch);
ExitOnFailure(hr, "Failed to allocate string.");
er = ::MsiFormatRecordW(NULL, hRecord, scz, &cch);
ExitOnWin32Error(er, hr, "Failed to format record.");
hr = VariableStrAllocString(!fObfuscateHiddenVariables, psczOut, scz, 0);
ExitOnFailure(hr, "Failed to copy string.");
}
// return character count
if (pcchOut)
{
*pcchOut = cch;
}
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
if (rgVariables)
{
for (DWORD i = 0; i < cVariables; ++i)
{
if (fObfuscateHiddenVariables)
{
ReleaseStr(rgVariables[i]);
}
else
{
StrSecureZeroFreeString(rgVariables[i]);
}
}
MemFree(rgVariables);
}
if (hRecord)
{
::MsiCloseHandle(hRecord);
}
if (fObfuscateHiddenVariables)
{
ReleaseStr(sczUnformatted);
ReleaseStr(sczFormat);
ReleaseStr(scz);
}
else
{
StrSecureZeroFreeString(sczUnformatted);
StrSecureZeroFreeString(sczFormat);
StrSecureZeroFreeString(scz);
}
return hr;
}
static HRESULT GetFormatted(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out_z LPWSTR* psczValue,
__out BOOL* pfContainsHiddenVariable
)
{
HRESULT hr = S_OK;
BURN_VARIABLE* pVariable = NULL;
LPWSTR scz = NULL;
::EnterCriticalSection(&pVariables->csAccess);
hr = GetVariable(pVariables, wzVariable, &pVariable);
if (SUCCEEDED(hr) && BURN_VARIANT_TYPE_NONE == pVariable->Value.Type)
{
ExitFunction1(hr = E_NOTFOUND);
}
else if (E_NOTFOUND == hr)
{
ExitFunction();
}
ExitOnFailure(hr, "Failed to get variable: %ls", wzVariable);
if (pfContainsHiddenVariable)
{
*pfContainsHiddenVariable |= pVariable->fHidden;
}
if (BURN_VARIANT_TYPE_FORMATTED == pVariable->Value.Type)
{
hr = BVariantGetString(&pVariable->Value, &scz);
ExitOnFailure(hr, "Failed to get unformatted string.");
hr = FormatString(pVariables, scz, psczValue, NULL, FALSE, pfContainsHiddenVariable);
ExitOnFailure(hr, "Failed to format value '%ls' of variable: %ls", pVariable->fHidden ? L"*****" : pVariable->Value.sczValue, wzVariable);
}
else
{
hr = BVariantGetString(&pVariable->Value, psczValue);
ExitOnFailure(hr, "Failed to get value as string for variable: %ls", wzVariable);
}
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
StrSecureZeroFreeString(scz);
return hr;
}
static HRESULT AddBuiltInVariable(
__in BURN_VARIABLES* pVariables,
__in LPCWSTR wzVariable,
__in PFN_INITIALIZEVARIABLE pfnInitialize,
__in DWORD_PTR dwpInitializeData,
__in BOOL fPersist,
__in BOOL fOverridable
)
{
HRESULT hr = S_OK;
DWORD iVariable = 0;
BURN_VARIABLE* pVariable = NULL;
hr = FindVariableIndexByName(pVariables, wzVariable, &iVariable);
ExitOnFailure(hr, "Failed to find variable value.");
// insert element if not found
if (S_FALSE == hr)
{
hr = InsertVariable(pVariables, wzVariable, iVariable);
ExitOnFailure(hr, "Failed to insert variable.");
}
else
{
ExitWithRootFailure(hr, E_INVALIDSTATE, "Attempted to add built-in variable again: %ls", wzVariable);
}
// set variable details
pVariable = &pVariables->rgVariables[iVariable];
pVariable->fPersisted = fPersist;
pVariable->internalType = fOverridable ? BURN_VARIABLE_INTERNAL_TYPE_OVERRIDABLE_BUILTIN : BURN_VARIABLE_INTERNAL_TYPE_BUILTIN;
pVariable->pfnInitialize = pfnInitialize;
pVariable->dwpInitializeData = dwpInitializeData;
LExit:
return hr;
}
static HRESULT AddWellKnownVariable(
__in BURN_VARIABLES* pVariables,
__in LPCWSTR wzVariable,
__in BOOL fPersisted
)
{
HRESULT hr = S_OK;
DWORD iVariable = 0;
BURN_VARIABLE* pVariable = NULL;
hr = FindVariableIndexByName(pVariables, wzVariable, &iVariable);
ExitOnFailure(hr, "Failed to find variable value.");
// insert element if not found
if (S_FALSE == hr)
{
hr = InsertVariable(pVariables, wzVariable, iVariable);
ExitOnFailure(hr, "Failed to insert variable.");
}
else if (BURN_VARIABLE_INTERNAL_TYPE_NORMAL != pVariables->rgVariables[iVariable].internalType)
{
ExitWithRootFailure(hr, E_INVALIDSTATE, "Attempted to add built-in variable as a well-known variable: %ls", wzVariable);
}
else
{
ExitWithRootFailure(hr, E_INVALIDSTATE, "Attempted to add well-known variable again: %ls", wzVariable);
}
// set variable details
pVariable = &pVariables->rgVariables[iVariable];
pVariable->fPersisted = fPersisted;
pVariable->internalType = BURN_VARIABLE_INTERNAL_TYPE_NORMAL;
LExit:
return hr;
}
static HRESULT GetVariable(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out BURN_VARIABLE** ppVariable
)
{
HRESULT hr = S_OK;
DWORD iVariable = 0;
BURN_VARIABLE* pVariable = NULL;
hr = FindVariableIndexByName(pVariables, wzVariable, &iVariable);
ExitOnFailure(hr, "Failed to find variable value '%ls'.", wzVariable);
if (S_FALSE == hr)
{
ExitFunction1(hr = E_NOTFOUND);
}
pVariable = &pVariables->rgVariables[iVariable];
// initialize built-in variable
if (BURN_VARIANT_TYPE_NONE == pVariable->Value.Type && BURN_VARIABLE_INTERNAL_TYPE_NORMAL < pVariable->internalType)
{
hr = pVariable->pfnInitialize(pVariable->dwpInitializeData, &pVariable->Value);
ExitOnFailure(hr, "Failed to initialize built-in variable value '%ls'.", wzVariable);
}
*ppVariable = pVariable;
LExit:
return hr;
}
static HRESULT FindVariableIndexByName(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__out DWORD* piVariable
)
{
HRESULT hr = S_OK;
DWORD iRangeFirst = 0;
DWORD cRangeLength = pVariables->cVariables;
while (cRangeLength)
{
// get variable in middle of range
DWORD iPosition = cRangeLength / 2;
BURN_VARIABLE* pVariable = &pVariables->rgVariables[iRangeFirst + iPosition];
switch (::CompareStringW(LOCALE_INVARIANT, SORT_STRINGSORT, wzVariable, -1, pVariable->sczName, -1))
{
case CSTR_LESS_THAN:
// restrict range to elements before the current
cRangeLength = iPosition;
break;
case CSTR_EQUAL:
// variable found
*piVariable = iRangeFirst + iPosition;
ExitFunction1(hr = S_OK);
case CSTR_GREATER_THAN:
// restrict range to elements after the current
iRangeFirst += iPosition + 1;
cRangeLength -= iPosition + 1;
break;
default:
ExitWithLastError(hr, "Failed to compare strings.");
}
}
*piVariable = iRangeFirst;
hr = S_FALSE; // variable not found
LExit:
return hr;
}
static HRESULT InsertUserVariable(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in DWORD iPosition
)
{
HRESULT hr = S_OK;
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, wzVariable, 3, L"Wix", 3))
{
ExitWithRootFailure(hr, E_INVALIDARG, "Attempted to insert variable with reserved prefix: %ls", wzVariable);
}
hr = InsertVariable(pVariables, wzVariable, iPosition);
LExit:
return hr;
}
static HRESULT InsertVariable(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in DWORD iPosition
)
{
HRESULT hr = S_OK;
size_t cbAllocSize = 0;
// ensure there is room in the variable array
if (pVariables->cVariables == pVariables->dwMaxVariables)
{
hr = ::DWordAdd(pVariables->dwMaxVariables, GROW_VARIABLE_ARRAY, &(pVariables->dwMaxVariables));
ExitOnRootFailure(hr, "Overflow while growing variable array size");
if (pVariables->rgVariables)
{
hr = ::SizeTMult(sizeof(BURN_VARIABLE), pVariables->dwMaxVariables, &cbAllocSize);
ExitOnRootFailure(hr, "Overflow while calculating size of variable array buffer");
LPVOID pv = MemReAlloc(pVariables->rgVariables, cbAllocSize, FALSE);
ExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to allocate room for more variables.");
// Prefast claims it's possible to hit this. Putting the check in just in case.
if (pVariables->dwMaxVariables < pVariables->cVariables)
{
hr = INTSAFE_E_ARITHMETIC_OVERFLOW;
ExitOnRootFailure(hr, "Overflow while dealing with variable array buffer allocation");
}
pVariables->rgVariables = (BURN_VARIABLE*)pv;
memset(&pVariables->rgVariables[pVariables->cVariables], 0, sizeof(BURN_VARIABLE) * (pVariables->dwMaxVariables - pVariables->cVariables));
}
else
{
pVariables->rgVariables = (BURN_VARIABLE*)MemAlloc(sizeof(BURN_VARIABLE) * pVariables->dwMaxVariables, TRUE);
ExitOnNull(pVariables->rgVariables, hr, E_OUTOFMEMORY, "Failed to allocate room for variables.");
}
}
// move variables
if (0 < pVariables->cVariables - iPosition)
{
memmove(&pVariables->rgVariables[iPosition + 1], &pVariables->rgVariables[iPosition], sizeof(BURN_VARIABLE) * (pVariables->cVariables - iPosition));
memset(&pVariables->rgVariables[iPosition], 0, sizeof(BURN_VARIABLE));
}
++pVariables->cVariables;
// allocate name
hr = StrAllocString(&pVariables->rgVariables[iPosition].sczName, wzVariable, 0);
ExitOnFailure(hr, "Failed to copy variable name.");
LExit:
return hr;
}
static HRESULT SetVariableValue(
__in BURN_VARIABLES* pVariables,
__in_z LPCWSTR wzVariable,
__in BURN_VARIANT* pVariant,
__in SET_VARIABLE setBuiltin,
__in BOOL fLog
)
{
HRESULT hr = S_OK;
DWORD iVariable = 0;
::EnterCriticalSection(&pVariables->csAccess);
hr = FindVariableIndexByName(pVariables, wzVariable, &iVariable);
ExitOnFailure(hr, "Failed to find variable value '%ls'.", wzVariable);
// Insert element if not found.
if (S_FALSE == hr)
{
// Not possible from external callers so just assert.
AssertSz(SET_VARIABLE_OVERRIDE_BUILTIN != setBuiltin, "Intent to set missing built-in variable.");
hr = InsertVariable(pVariables, wzVariable, iVariable);
ExitOnFailure(hr, "Failed to insert variable '%ls'.", wzVariable);
}
else if (BURN_VARIABLE_INTERNAL_TYPE_NORMAL < pVariables->rgVariables[iVariable].internalType) // built-in variables must be overridden.
{
if (SET_VARIABLE_OVERRIDE_BUILTIN == setBuiltin ||
(SET_VARIABLE_OVERRIDE_PERSISTED_BUILTINS == setBuiltin && pVariables->rgVariables[iVariable].fPersisted) ||
SET_VARIABLE_ANY == setBuiltin && BURN_VARIABLE_INTERNAL_TYPE_BUILTIN != pVariables->rgVariables[iVariable].internalType)
{
hr = S_OK;
}
else
{
hr = E_INVALIDARG;
ExitOnRootFailure(hr, "Attempt to set built-in variable value: %ls", wzVariable);
}
}
else // must *not* be a built-in variable so caller should not have tried to override it as a built-in.
{
// Not possible from external callers so just assert.
AssertSz(SET_VARIABLE_OVERRIDE_BUILTIN != setBuiltin, "Intent to overwrite non-built-in variable.");
}
// Log value when not overwriting a built-in variable.
if (fLog && BURN_VARIABLE_INTERNAL_TYPE_NORMAL == pVariables->rgVariables[iVariable].internalType)
{
if (pVariables->rgVariables[iVariable].fHidden)
{
LogStringLine(REPORT_STANDARD, "Setting hidden variable '%ls'", wzVariable);
}
else
{
switch (pVariant->Type)
{
case BURN_VARIANT_TYPE_NONE:
if (BURN_VARIANT_TYPE_NONE != pVariables->rgVariables[iVariable].Value.Type)
{
LogStringLine(REPORT_STANDARD, "Unsetting variable '%ls'", wzVariable);
}
break;
case BURN_VARIANT_TYPE_NUMERIC:
LogStringLine(REPORT_STANDARD, "Setting numeric variable '%ls' to value %lld", wzVariable, pVariant->llValue);
break;
case BURN_VARIANT_TYPE_FORMATTED: __fallthrough;
case BURN_VARIANT_TYPE_STRING:
if (!pVariant->sczValue)
{
LogStringLine(REPORT_STANDARD, "Unsetting variable '%ls'", wzVariable);
}
else
{
LogStringLine(REPORT_STANDARD, "Setting %ls variable '%ls' to value '%ls'", BURN_VARIANT_TYPE_FORMATTED == pVariant->Type ? L"formatted" : L"string", wzVariable, pVariant->sczValue);
}
break;
case BURN_VARIANT_TYPE_VERSION:
if (!pVariant->pValue)
{
LogStringLine(REPORT_STANDARD, "Unsetting variable '%ls'", wzVariable);
}
else
{
LogStringLine(REPORT_STANDARD, "Setting version variable '%ls' to value '%ls'", wzVariable, pVariant->pValue->sczVersion);
}
break;
default:
AssertSz(FALSE, "Unknown variant type.");
break;
}
}
if (BURN_VARIANT_TYPE_VERSION == pVariant->Type && pVariant->pValue && pVariant->pValue->fInvalid)
{
LogId(REPORT_WARNING, MSG_VARIABLE_INVALID_VERSION, wzVariable);
}
}
// Update variable value.
hr = BVariantSetValue(&pVariables->rgVariables[iVariable].Value, pVariant);
ExitOnFailure(hr, "Failed to set value of variable: %ls", wzVariable);
LExit:
::LeaveCriticalSection(&pVariables->csAccess);
if (FAILED(hr) && fLog)
{
LogStringLine(REPORT_STANDARD, "Setting variable failed: ID '%ls', HRESULT 0x%x", wzVariable, hr);
}
return hr;
}
static HRESULT InitializeVariableVersionNT(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
RTL_OSVERSIONINFOEXW ovix = { };
BURN_VARIANT value = { };
VERUTIL_VERSION* pVersion = NULL;
hr = OsRtlGetVersion(&ovix);
ExitOnFailure(hr, "Failed to get OS info.");
switch ((OS_INFO_VARIABLE)dwpData)
{
case OS_INFO_VARIABLE_ServicePackLevel:
if (0 != ovix.wServicePackMajor)
{
value.llValue = static_cast<LONGLONG>(ovix.wServicePackMajor);
value.Type = BURN_VARIANT_TYPE_NUMERIC;
}
break;
case OS_INFO_VARIABLE_VersionNT:
hr = VerVersionFromQword(MAKEQWORDVERSION(ovix.dwMajorVersion, ovix.dwMinorVersion, 0, 0), &pVersion);
ExitOnFailure(hr, "Failed to create VersionNT from QWORD.");
value.pValue = pVersion;
value.Type = BURN_VARIANT_TYPE_VERSION;
break;
case OS_INFO_VARIABLE_VersionNT64:
{
#if !defined(_WIN64)
BOOL fIsWow64 = FALSE;
ProcWow64(::GetCurrentProcess(), &fIsWow64);
if (fIsWow64)
#endif
{
hr = VerVersionFromQword(MAKEQWORDVERSION(ovix.dwMajorVersion, ovix.dwMinorVersion, 0, 0), &pVersion);
ExitOnFailure(hr, "Failed to create VersionNT64 from QWORD.");
value.pValue = pVersion;
value.Type = BURN_VARIANT_TYPE_VERSION;
}
}
break;
case OS_INFO_VARIABLE_WindowsBuildNumber:
value.llValue = static_cast<LONGLONG>(ovix.dwBuildNumber);
value.Type = BURN_VARIANT_TYPE_NUMERIC;
default:
AssertSz(FALSE, "Unknown OS info type.");
break;
}
hr = BVariantCopy(&value, pValue);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseVerutilVersion(pVersion);
return hr;
}
static HRESULT InitializeVariableOsInfo(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
RTL_OSVERSIONINFOEXW ovix = { };
BURN_VARIANT value = { };
hr = OsRtlGetVersion(&ovix);
ExitOnFailure(hr, "Failed to get OS info.");
switch ((OS_INFO_VARIABLE)dwpData)
{
case OS_INFO_VARIABLE_NTProductType:
value.llValue = ovix.wProductType;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuiteBackOffice:
value.llValue = VER_SUITE_BACKOFFICE & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuiteDataCenter:
value.llValue = VER_SUITE_DATACENTER & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuiteEnterprise:
value.llValue = VER_SUITE_ENTERPRISE & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuitePersonal:
value.llValue = VER_SUITE_PERSONAL & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuiteSmallBusiness:
value.llValue = VER_SUITE_SMALLBUSINESS & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuiteSmallBusinessRestricted:
value.llValue = VER_SUITE_SMALLBUSINESS_RESTRICTED & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_NTSuiteWebServer:
value.llValue = VER_SUITE_BLADE & ovix.wSuiteMask ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
case OS_INFO_VARIABLE_CompatibilityMode:
{
DWORDLONG dwlConditionMask = 0;
VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, VER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMINOR, VER_EQUAL);
value.llValue = ::VerifyVersionInfoW(&ovix, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
value.Type = BURN_VARIANT_TYPE_NUMERIC;
}
break;
case OS_INFO_VARIABLE_TerminalServer:
value.llValue = (VER_SUITE_TERMINAL == (ovix.wSuiteMask & VER_SUITE_TERMINAL)) && (VER_SUITE_SINGLEUSERTS != (ovix.wSuiteMask & VER_SUITE_SINGLEUSERTS)) ? 1 : 0;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
default:
AssertSz(FALSE, "Unknown OS info type.");
break;
}
hr = BVariantCopy(&value, pValue);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableSystemInfo(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
SYSTEM_INFO si = { };
BURN_VARIANT value = { };
::GetNativeSystemInfo(&si);
switch ((OS_INFO_VARIABLE)dwpData)
{
case OS_INFO_VARIABLE_ProcessorArchitecture:
value.llValue = si.wProcessorArchitecture;
value.Type = BURN_VARIANT_TYPE_NUMERIC;
break;
default:
AssertSz(FALSE, "Unknown OS info type.");
break;
}
hr = BVariantCopy(&value, pValue);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableNativeMachine(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
USHORT usNativeMachine = IMAGE_FILE_MACHINE_UNKNOWN;
hr = ProcNativeMachine(::GetCurrentProcess(), &usNativeMachine);
ExitOnFailure(hr, "Failed to get native machine value.");
if (S_FALSE != hr)
{
hr = BVariantSetNumeric(pValue, usNativeMachine);
ExitOnFailure(hr, "Failed to set variant value.");
}
LExit:
return hr;
}
static HRESULT InitializeVariableComputerName(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
WCHAR wzComputerName[MAX_COMPUTERNAME_LENGTH + 1] = { };
DWORD cchComputerName = countof(wzComputerName);
// get computer name
if (!::GetComputerNameW(wzComputerName, &cchComputerName))
{
ExitWithLastError(hr, "Failed to get computer name.");
}
// set value
hr = BVariantSetString(pValue, wzComputerName, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableVersionMsi(
__in DWORD_PTR /*dwpData*/,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
DLLGETVERSIONPROC pfnMsiDllGetVersion = NULL;
DLLVERSIONINFO msiVersionInfo = { };
VERUTIL_VERSION* pVersion = NULL;
// get DllGetVersion proc address
pfnMsiDllGetVersion = (DLLGETVERSIONPROC)::GetProcAddress(::GetModuleHandleW(L"msi"), "DllGetVersion");
ExitOnNullWithLastError(pfnMsiDllGetVersion, hr, "Failed to find DllGetVersion entry point in msi.dll.");
// get msi.dll version info
msiVersionInfo.cbSize = sizeof(DLLVERSIONINFO);
hr = pfnMsiDllGetVersion(&msiVersionInfo);
ExitOnFailure(hr, "Failed to get msi.dll version info.");
hr = VerVersionFromQword(MAKEQWORDVERSION(msiVersionInfo.dwMajorVersion, msiVersionInfo.dwMinorVersion, 0, 0), &pVersion);
ExitOnFailure(hr, "Failed to create msi.dll version from QWORD.");
hr = BVariantSetVersion(pValue, pVersion);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseVerutilVersion(pVersion);
return hr;
}
static HRESULT InitializeVariableCsidlFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
LPWSTR sczPath = NULL;
int nFolder = (int)dwpData;
// get folder path
hr = ShelGetFolder(&sczPath, nFolder);
ExitOnRootFailure(hr, "Failed to get shell folder.");
// set value
hr = BVariantSetString(pValue, sczPath, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczPath);
return hr;
}
static HRESULT InitializeVariableTempFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
LPWSTR sczPath = NULL;
hr = PathGetTempPath(&sczPath, NULL);
ExitOnFailure(hr, "Failed to get temp path.");
// set value
hr = BVariantSetString(pValue, sczPath, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczPath);
return hr;
}
static HRESULT InitializeVariableSystemFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
BOOL f64 = (BOOL)dwpData;
LPWSTR sczSystemFolder = NULL;
#if !defined(_WIN64)
BOOL fIsWow64 = FALSE;
ProcWow64(::GetCurrentProcess(), &fIsWow64);
if (fIsWow64)
{
if (f64)
{
hr = PathGetSystemDirectory(&sczSystemFolder);
ExitOnFailure(hr, "Failed to get 64-bit system folder.");
}
else
{
hr = PathGetSystemWow64Directory(&sczSystemFolder);
ExitOnFailure(hr, "Failed to get 32-bit system folder.");
}
}
else
{
if (!f64)
{
hr = PathGetSystemDirectory(&sczSystemFolder);
ExitOnFailure(hr, "Failed to get 32-bit system folder.");
}
}
#else
if (f64)
{
hr = PathGetSystemDirectory(&sczSystemFolder);
ExitOnFailure(hr, "Failed to get 64-bit system folder.");
}
else
{
hr = PathGetSystemWow64Directory(&sczSystemFolder);
ExitOnFailure(hr, "Failed to get 32-bit system folder.");
}
#endif
// set value
hr = BVariantSetString(pValue, sczSystemFolder, 0, FALSE);
ExitOnFailure(hr, "Failed to set system folder variant value.");
LExit:
ReleaseStr(sczSystemFolder);
return hr;
}
static HRESULT InitializeVariableWindowsVolumeFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
LPWSTR sczWindowsPath = NULL;
LPWSTR sczVolumePath = NULL;
// get windows directory
hr = PathSystemWindowsSubdirectory(NULL, &sczWindowsPath);
ExitOnFailure(hr, "Failed to get windows directory.");
// get volume path name
hr = PathGetVolumePathName(sczWindowsPath, &sczVolumePath);
ExitOnFailure(hr, "Failed to get volume path name.");
// set value
hr = BVariantSetString(pValue, sczVolumePath, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczWindowsPath);
ReleaseStr(sczVolumePath);
return hr;
}
static HRESULT InitializeVariablePrivileged(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
BOOL fPrivileged = FALSE;
// check if process could run privileged.
hr = OsCouldRunPrivileged(&fPrivileged);
ExitOnFailure(hr, "Failed to check if process could run privileged.");
// set value
hr = BVariantSetNumeric(pValue, fPrivileged);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableProcessTokenPrivilege(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
BOOL fHasPrivilege = FALSE;
LPCWSTR wzPrivilegeName = (LPCWSTR)dwpData;
hr = ProcHasPrivilege(::GetCurrentProcess(), wzPrivilegeName, &fHasPrivilege);
ExitOnFailure(hr, "Failed to check if process token has privilege: %ls.", wzPrivilegeName);
// set value
hr = BVariantSetNumeric(pValue, fHasPrivilege);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeSystemLanguageID(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
LANGID langid = ::GetSystemDefaultLangID();
hr = BVariantSetNumeric(pValue, langid);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeUserUILanguageID(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
LANGID langid = ::GetUserDefaultUILanguage();
hr = BVariantSetNumeric(pValue, langid);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeUserLanguageID(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
UNREFERENCED_PARAMETER(dwpData);
HRESULT hr = S_OK;
LANGID langid = ::GetUserDefaultLangID();
hr = BVariantSetNumeric(pValue, langid);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableString(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
LPCWSTR wzValue = (LPCWSTR)dwpData;
// set value
hr = BVariantSetString(pValue, wzValue, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableNumeric(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
LONGLONG llValue = (LONGLONG)dwpData;
// set value
hr = BVariantSetNumeric(pValue, llValue);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
#if !defined(_WIN64)
static HRESULT InitializeVariableRegistryFolder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
int nFolder = (int)dwpData;
LPWSTR sczPath = NULL;
BOOL fIsWow64 = FALSE;
ProcWow64(::GetCurrentProcess(), &fIsWow64);
if (!fIsWow64) // on 32-bit machines, variables aren't set
{
ExitFunction();
}
hr = Get64bitFolderFromRegistry(nFolder, &sczPath);
ExitOnFailure(hr, "Failed to get 64-bit folder.");
// set value
hr = BVariantSetString(pValue, sczPath, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczPath);
return hr;
}
#endif
static HRESULT InitializeVariable6432Folder(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
int nFolder = (int)dwpData;
LPWSTR sczPath = NULL;
#if !defined(_WIN64)
BOOL fIsWow64 = FALSE;
// If 32-bit use shell-folder.
ProcWow64(::GetCurrentProcess(), &fIsWow64);
if (!fIsWow64)
{
hr = ShelGetFolder(&sczPath, nFolder);
ExitOnRootFailure(hr, "Failed to get shell folder.");
}
else
#endif
{
hr = Get64bitFolderFromRegistry(nFolder, &sczPath);
ExitOnFailure(hr, "Failed to get 64-bit folder.");
}
// set value
hr = BVariantSetString(pValue, sczPath, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczPath);
return hr;
}
// Get the date in the same format as Windows Installer.
static HRESULT InitializeVariableDate(
__in DWORD_PTR /*dwpData*/,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
SYSTEMTIME systime = { };
LPWSTR sczDate = NULL;
int cchDate = 0;
::GetSystemTime(&systime);
cchDate = ::GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime, NULL, NULL, cchDate);
if (!cchDate)
{
ExitOnLastError(hr, "Failed to get the required buffer length for the Date.");
}
hr = StrAlloc(&sczDate, cchDate);
ExitOnFailure(hr, "Failed to allocate the buffer for the Date.");
if (!::GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime, NULL, sczDate, cchDate))
{
ExitOnLastError(hr, "Failed to get the Date.");
}
// set value
hr = BVariantSetString(pValue, sczDate, cchDate, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczDate);
return hr;
}
static HRESULT InitializeVariableInstallerName(
__in DWORD_PTR /*dwpData*/,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
// set value
hr = BVariantSetString(pValue, L"WiX Burn", 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT InitializeVariableInstallerVersion(
__in DWORD_PTR /*dwpData*/,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
LPWSTR sczVersion = NULL;
hr = StrAllocStringAnsi(&sczVersion, szVerMajorMinorBuild, 0, CP_ACP);
ExitOnFailure(hr, "Failed to copy the engine version: %hs", szVerMajorMinorBuild);
// set value
hr = BVariantSetString(pValue, sczVersion, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczVersion);
return hr;
}
static HRESULT InitializeVariableInstallerInformationalVersion(
__in DWORD_PTR /*dwpData*/,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
LPWSTR sczVersion = NULL;
hr = StrAllocStringAnsi(&sczVersion, szInformationalVersion, 0, CP_ACP);
ExitOnFailure(hr, "Failed to copy the engine informational version: %hs", szInformationalVersion);
// set value
hr = BVariantSetString(pValue, sczVersion, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseStr(sczVersion);
return hr;
}
static HRESULT InitializeVariableVersion(
__in DWORD_PTR dwpData,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
LPCWSTR wzValue = (LPCWSTR)dwpData;
VERUTIL_VERSION* pVersion = NULL;
hr = VerParseVersion(wzValue, 0, FALSE, &pVersion);
ExitOnFailure(hr, "Failed to initialize version.");
// set value
hr = BVariantSetVersion(pValue, pVersion);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
ReleaseVerutilVersion(pVersion);
return hr;
}
// Get the current user the same as Windows Installer.
static HRESULT InitializeVariableLogonUser(
__in DWORD_PTR /*dwpData*/,
__inout BURN_VARIANT* pValue
)
{
HRESULT hr = S_OK;
WCHAR wzUserName[UNLEN + 1];
DWORD cchUserName = countof(wzUserName);
if (!::GetUserNameW(wzUserName, &cchUserName))
{
ExitOnLastError(hr, "Failed to get the user name.");
}
// set value
hr = BVariantSetString(pValue, wzUserName, 0, FALSE);
ExitOnFailure(hr, "Failed to set variant value.");
LExit:
return hr;
}
static HRESULT Get64bitFolderFromRegistry(
__in int nFolder,
__deref_out_z LPWSTR* psczPath
)
{
HRESULT hr = S_OK;
HKEY hkFolders = NULL;
AssertSz(CSIDL_PROGRAM_FILES == nFolder || CSIDL_PROGRAM_FILES_COMMON == nFolder, "Unknown folder CSIDL.");
LPCWSTR wzFolderValue = CSIDL_PROGRAM_FILES_COMMON == nFolder ? L"CommonFilesDir" : L"ProgramFilesDir";
hr = RegOpenEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion", KEY_READ, REG_KEY_64BIT, &hkFolders);
ExitOnFailure(hr, "Failed to open Windows folder key.");
hr = RegReadString(hkFolders, wzFolderValue, psczPath);
ExitOnFailure(hr, "Failed to read folder path for '%ls'.", wzFolderValue);
hr = PathBackslashTerminate(psczPath);
ExitOnFailure(hr, "Failed to ensure path was backslash terminated.");
LExit:
ReleaseRegKey(hkFolders);
return hr;
}
|