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
|
// 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"
/********************************************************************
WcaProcessMessage() - sends a message from the CustomAction
********************************************************************/
extern "C" UINT WIXAPI WcaProcessMessage(
__in INSTALLMESSAGE eMessageType,
__in MSIHANDLE hRecord
)
{
UINT er = ::MsiProcessMessage(WcaGetInstallHandle(), eMessageType, hRecord);
if (ERROR_INSTALL_USEREXIT == er || IDCANCEL == er)
{
WcaSetReturnValue(ERROR_INSTALL_USEREXIT);
}
return er;
}
/********************************************************************
WcaErrorMessage() - sends an error message from the CustomAction using
the Error table
NOTE: Any and all var_args (...) must be WCHAR*
If you pass -1 to cArgs, the count will be determined by
looking for a trailing NULL argment. If you omit a terminating
NULL, the results are undefined and probably crashy.
********************************************************************/
extern "C" UINT __cdecl WcaErrorMessage(
__in int iError,
__in HRESULT hrError,
__in UINT uiType,
__in INT cArgs,
...
)
{
UINT er;
MSIHANDLE hRec = NULL;
va_list args = NULL;
if (-1 == cArgs)
{
LPCWSTR wzArg = NULL;
va_list iter = NULL;
va_start(iter, cArgs);
cArgs = 0;
while (NULL != (wzArg = va_arg(iter, WCHAR*)) && L'\0' != *wzArg)
{
++cArgs;
}
va_end(iter);
}
uiType |= INSTALLMESSAGE_ERROR; // ensure error type is set
hRec = ::MsiCreateRecord(cArgs + 2);
if (!hRec)
{
er = ERROR_OUTOFMEMORY;
ExitOnFailure(HRESULT_FROM_WIN32(er), "failed to create record when sending error message");
}
er = ::MsiRecordSetInteger(hRec, 1, iError);
ExitOnFailure(HRESULT_FROM_WIN32(er), "failed to set error code into error message");
er = ::MsiRecordSetInteger(hRec, 2, hrError);
ExitOnFailure(HRESULT_FROM_WIN32(er), "failed to set hresult code into error message");
va_start(args, cArgs);
for (INT i = 0; i < cArgs; i++)
{
er = ::MsiRecordSetStringW(hRec, i + 3, va_arg(args, WCHAR*));
ExitOnFailure(HRESULT_FROM_WIN32(er), "failed to set string string into error message");
}
va_end(args);
er = WcaProcessMessage(static_cast<INSTALLMESSAGE>(uiType), hRec);
LExit:
if (args)
{
va_end(args);
}
if (hRec)
{
::MsiCloseHandle(hRec);
}
return er;
}
/********************************************************************
WcaProgressMessage() - extends the progress bar or sends a progress
update from the CustomAction
********************************************************************/
extern "C" HRESULT WIXAPI WcaProgressMessage(
__in UINT uiCost,
__in BOOL fExtendProgressBar
)
{
static BOOL fExplicitProgressMessages = FALSE;
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
MSIHANDLE hRec = ::MsiCreateRecord(3);
// if aren't extending the progress bar and we haven't switched into explicit message mode
if (!fExtendProgressBar && !fExplicitProgressMessages)
{
AssertSz(::MsiGetMode(WcaGetInstallHandle(), MSIRUNMODE_SCHEDULED) ||
::MsiGetMode(WcaGetInstallHandle(), MSIRUNMODE_COMMIT) ||
::MsiGetMode(WcaGetInstallHandle(), MSIRUNMODE_ROLLBACK), "can only send progress bar messages in a deferred CustomAction");
// tell Darwin to use explicit progress messages
::MsiRecordSetInteger(hRec, 1, 1);
::MsiRecordSetInteger(hRec, 2, 1);
::MsiRecordSetInteger(hRec, 3, 0);
er = WcaProcessMessage(INSTALLMESSAGE_PROGRESS, hRec);
if (0 == er || IDOK == er || IDYES == er)
{
hr = S_OK;
}
else if (IDABORT == er || IDCANCEL == er)
{
WcaSetReturnValue(ERROR_INSTALL_USEREXIT); // note that the user said exit
ExitFunction1(hr = S_FALSE);
}
else
{
hr = E_UNEXPECTED;
}
ExitOnFailure(hr, "failed to tell Darwin to use explicit progress messages");
fExplicitProgressMessages = TRUE;
}
#if DEBUG
else if (fExtendProgressBar) // if we are extending the progress bar, make sure we're not deferred
{
AssertSz(!::MsiGetMode(WcaGetInstallHandle(), MSIRUNMODE_SCHEDULED), "cannot add ticks to progress bar length from deferred CustomAction");
}
#endif
// send the progress message
::MsiRecordSetInteger(hRec, 1, (fExtendProgressBar) ? 3 : 2);
::MsiRecordSetInteger(hRec, 2, uiCost);
::MsiRecordSetInteger(hRec, 3, 0);
er = WcaProcessMessage(INSTALLMESSAGE_PROGRESS, hRec);
if (0 == er || IDOK == er || IDYES == er)
{
hr = S_OK;
}
else if (IDABORT == er || IDCANCEL == er)
{
WcaSetReturnValue(ERROR_INSTALL_USEREXIT); // note that the user said exit
hr = S_FALSE;
}
else
{
hr = E_UNEXPECTED;
}
LExit:
if (hRec)
{
::MsiCloseHandle(hRec);
}
return hr;
}
/********************************************************************
WcaIsInstalling() - determines if a pair of installstates means install
********************************************************************/
extern "C" BOOL WIXAPI WcaIsInstalling(
__in INSTALLSTATE isInstalled,
__in INSTALLSTATE isAction
)
{
return (INSTALLSTATE_LOCAL == isAction ||
INSTALLSTATE_SOURCE == isAction ||
(INSTALLSTATE_DEFAULT == isAction &&
(INSTALLSTATE_LOCAL == isInstalled ||
INSTALLSTATE_SOURCE == isInstalled)));
}
/********************************************************************
WcaIsReInstalling() - determines if a pair of installstates means reinstall
********************************************************************/
extern "C" BOOL WIXAPI WcaIsReInstalling(
__in INSTALLSTATE isInstalled,
__in INSTALLSTATE isAction
)
{
return ((INSTALLSTATE_LOCAL == isAction ||
INSTALLSTATE_SOURCE == isAction ||
INSTALLSTATE_DEFAULT == isAction) &&
(INSTALLSTATE_LOCAL == isInstalled ||
INSTALLSTATE_SOURCE == isInstalled));
}
/********************************************************************
WcaIsUninstalling() - determines if a pair of installstates means uninstall
********************************************************************/
extern "C" BOOL WIXAPI WcaIsUninstalling(
__in INSTALLSTATE isInstalled,
__in INSTALLSTATE isAction
)
{
return ((INSTALLSTATE_ABSENT == isAction ||
INSTALLSTATE_REMOVED == isAction) &&
(INSTALLSTATE_LOCAL == isInstalled ||
INSTALLSTATE_SOURCE == isInstalled));
}
/********************************************************************
WcaGetComponentToDo() - gets a component's install states and
determines if they mean install, uninstall, or reinstall.
********************************************************************/
extern "C" WCA_TODO WIXAPI WcaGetComponentToDo(
__in_z LPCWSTR wzComponentId
)
{
INSTALLSTATE isInstalled = INSTALLSTATE_UNKNOWN;
INSTALLSTATE isAction = INSTALLSTATE_UNKNOWN;
if (ERROR_SUCCESS != ::MsiGetComponentStateW(WcaGetInstallHandle(), wzComponentId, &isInstalled, &isAction))
{
return WCA_TODO_UNKNOWN;
}
if (WcaIsReInstalling(isInstalled, isAction))
{
return WCA_TODO_REINSTALL;
}
else if (WcaIsUninstalling(isInstalled, isAction))
{
return WCA_TODO_UNINSTALL;
}
else if (WcaIsInstalling(isInstalled, isAction))
{
return WCA_TODO_INSTALL;
}
else
{
return WCA_TODO_UNKNOWN;
}
}
/********************************************************************
WcaSetComponentState() - sets the install state of a Component
********************************************************************/
extern "C" HRESULT WIXAPI WcaSetComponentState(
__in_z LPCWSTR wzComponent,
__in INSTALLSTATE isState
)
{
UINT er = ::MsiSetComponentStateW(WcaGetInstallHandle(), wzComponent, isState);
if (ERROR_INSTALL_USEREXIT == er)
{
WcaSetReturnValue(er);
}
return HRESULT_FROM_WIN32(er);
}
/********************************************************************
WcaTableExists() - determines if installing database contains a table
********************************************************************/
extern "C" HRESULT WIXAPI WcaTableExists(
__in_z LPCWSTR wzTable
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
// NOTE: The following line of commented out code should work in a
// CustomAction but does not in Windows Installer v1.1
// er = ::MsiDatabaseIsTablePersistentW(hDatabase, wzTable);
// a "most elegant" workaround a Darwin v1.1 bug
PMSIHANDLE hRec;
er = ::MsiDatabaseGetPrimaryKeysW(WcaGetDatabaseHandle(), wzTable, &hRec);
if (ERROR_SUCCESS == er)
{
hr = S_OK;
}
else if (ERROR_INVALID_TABLE == er)
{
hr = S_FALSE;
}
else
{
hr = E_FAIL;
}
Assert(SUCCEEDED(hr));
return hr;
}
/********************************************************************
WcaOpenView() - opens a view on the installing database
********************************************************************/
extern "C" HRESULT WIXAPI WcaOpenView(
__in_z LPCWSTR wzSql,
__out MSIHANDLE* phView
)
{
if (!wzSql || !*wzSql|| !phView)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er = ::MsiDatabaseOpenViewW(WcaGetDatabaseHandle(), wzSql, phView);
ExitOnWin32Error(er, hr, "failed to open view on database with SQL: %ls", wzSql);
LExit:
return hr;
}
/********************************************************************
WcaExecuteView() - executes a parameterized open view on the installing database
********************************************************************/
extern "C" HRESULT WIXAPI WcaExecuteView(
__in MSIHANDLE hView,
__in MSIHANDLE hRec
)
{
if (!hView)
{
return E_INVALIDARG;
}
AssertSz(hRec, "Use WcaOpenExecuteView() if you don't need to pass in a record");
HRESULT hr = S_OK;
UINT er = ::MsiViewExecute(hView, hRec);
ExitOnWin32Error(er, hr, "failed to execute view");
LExit:
return hr;
}
/********************************************************************
WcaOpenExecuteView() - opens and executes a view on the installing database
********************************************************************/
extern "C" HRESULT WIXAPI WcaOpenExecuteView(
__in_z LPCWSTR wzSql,
__out MSIHANDLE* phView
)
{
if (!wzSql || !*wzSql|| !phView)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er = ::MsiDatabaseOpenViewW(WcaGetDatabaseHandle(), wzSql, phView);
ExitOnWin32Error(er, hr, "failed to open view on database");
er = ::MsiViewExecute(*phView, NULL);
ExitOnWin32Error(er, hr, "failed to execute view");
LExit:
return hr;
}
/********************************************************************
WcaFetchRecord() - gets the next record from a view on the installing database
********************************************************************/
extern "C" HRESULT WIXAPI WcaFetchRecord(
__in MSIHANDLE hView,
__out MSIHANDLE* phRec
)
{
if (!hView|| !phRec)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er = ::MsiViewFetch(hView, phRec);
hr = HRESULT_FROM_WIN32(er);
if (FAILED(hr) && E_NOMOREITEMS != hr)
{
ExitOnFailure(hr, "failed to fetch record from view");
}
LExit:
return hr;
}
/********************************************************************
WcaFetchSingleRecord() - gets a single record from a view on the installing database
********************************************************************/
extern "C" HRESULT WIXAPI WcaFetchSingleRecord(
__in MSIHANDLE hView,
__out MSIHANDLE* phRec
)
{
if (!hView|| !phRec)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er = ::MsiViewFetch(hView, phRec);
if (ERROR_NO_MORE_ITEMS == er)
{
hr = S_FALSE;
}
else
{
hr = HRESULT_FROM_WIN32(er);
}
ExitOnFailure(hr, "failed to fetch single record from view");
#ifdef DEBUG // only do this in debug to verify that a single record was returned
MSIHANDLE hRecTest;
er = ::MsiViewFetch(hView, &hRecTest);
AssertSz(ERROR_NO_MORE_ITEMS == er && NULL == hRecTest, "WcaSingleFetch() did not fetch a single record");
::MsiCloseHandle(hRecTest);
#endif
LExit:
return hr;
}
/********************************************************************
WcaGetProperty - gets a string property value from the active install
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetProperty(
__in_z LPCWSTR wzProperty,
__inout LPWSTR* ppwzData
)
{
if (!wzProperty || !*wzProperty || !ppwzData)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
DWORD cch = 0;
SIZE_T cchMax = 0;
if (!*ppwzData)
{
WCHAR szEmpty[1] = L"";
er = ::MsiGetPropertyW(WcaGetInstallHandle(), wzProperty, szEmpty, &cch);
if (ERROR_MORE_DATA == er || ERROR_SUCCESS == er)
{
hr = StrAlloc(ppwzData, ++cch);
}
else
{
hr = HRESULT_FROM_WIN32(er);
}
ExitOnRootFailure(hr, "Failed to allocate string for Property '%ls'", wzProperty);
}
else
{
hr = StrMaxLength(*ppwzData, &cchMax);
ExitOnFailure(hr, "Failed to get previous size of property data string.");
cch = (DWORD)min(MAXDWORD, cchMax);
}
er = ::MsiGetPropertyW(WcaGetInstallHandle(), wzProperty, *ppwzData, &cch);
if (ERROR_MORE_DATA == er)
{
Assert(*ppwzData);
hr = StrAlloc(ppwzData, ++cch);
ExitOnFailure(hr, "Failed to allocate string for Property '%ls'", wzProperty);
er = ::MsiGetPropertyW(WcaGetInstallHandle(), wzProperty, *ppwzData, &cch);
}
ExitOnWin32Error(er, hr, "Failed to get data for property '%ls'", wzProperty);
LExit:
return hr;
}
/********************************************************************
WcaGetFormattedProperty - gets a formatted string property value from
the active install
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetFormattedProperty(
__in_z LPCWSTR wzProperty,
__out LPWSTR* ppwzData
)
{
if (!wzProperty || !*wzProperty || !ppwzData)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
LPWSTR pwzPropertyValue = NULL;
hr = WcaGetProperty(wzProperty, &pwzPropertyValue);
ExitOnFailure(hr, "failed to get %ls", wzProperty);
hr = WcaGetFormattedString(pwzPropertyValue, ppwzData);
ExitOnFailure(hr, "failed to get formatted value for property: '%ls' with value: '%ls'", wzProperty, pwzPropertyValue);
LExit:
ReleaseStr(pwzPropertyValue);
return hr;
}
/********************************************************************
WcaGetFormattedString - gets a formatted string value from
the active install
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetFormattedString(
__in_z LPCWSTR wzString,
__out LPWSTR* ppwzData
)
{
if (!wzString || !*wzString || !ppwzData)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
PMSIHANDLE hRecord = ::MsiCreateRecord(1);
DWORD cch = 0;
SIZE_T cchMax = 0;
er = ::MsiRecordSetStringW(hRecord, 0, wzString);
ExitOnWin32Error(er, hr, "Failed to set record field 0 with '%ls'", wzString);
if (!*ppwzData)
{
WCHAR szEmpty[1] = L"";
er = ::MsiFormatRecordW(WcaGetInstallHandle(), hRecord, szEmpty, &cch);
if (ERROR_MORE_DATA == er || ERROR_SUCCESS == er)
{
hr = StrAlloc(ppwzData, ++cch);
}
else
{
hr = HRESULT_FROM_WIN32(er);
}
ExitOnFailure(hr, "Failed to allocate string for formatted string: '%ls'", wzString);
}
else
{
hr = StrMaxLength(*ppwzData, &cchMax);
ExitOnFailure(hr, "Failed to get previous size of property data string");
cch = (DWORD)min(MAXDWORD, cchMax);
}
er = ::MsiFormatRecordW(WcaGetInstallHandle(), hRecord, *ppwzData, &cch);
if (ERROR_MORE_DATA == er)
{
hr = StrAlloc(ppwzData, ++cch);
ExitOnFailure(hr, "Failed to allocate string for formatted string: '%ls'", wzString);
er = ::MsiFormatRecordW(WcaGetInstallHandle(), hRecord, *ppwzData, &cch);
}
ExitOnWin32Error(er, hr, "Failed to get formatted string: '%ls'", wzString);
LExit:
return hr;
}
/********************************************************************
WcaGetIntProperty - gets an integer property value from the active install
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetIntProperty(
__in_z LPCWSTR wzProperty,
__inout int* piData
)
{
if (!piData)
return E_INVALIDARG;
HRESULT hr = S_OK;
UINT er;
WCHAR wzValue[32];
DWORD cch = countof(wzValue) - 1;
er = ::MsiGetPropertyW(WcaGetInstallHandle(), wzProperty, wzValue, &cch);
ExitOnWin32Error(er, hr, "Failed to get data for property '%ls'", wzProperty);
*piData = wcstol(wzValue, NULL, 10);
LExit:
return hr;
}
/********************************************************************
WcaGetTargetPath - gets the target path for a specified folder
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetTargetPath(
__in_z LPCWSTR wzFolder,
__out LPWSTR* ppwzData
)
{
if (!wzFolder || !*wzFolder || !ppwzData)
return E_INVALIDARG;
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
DWORD cch = 0;
SIZE_T cchMax = 0;
if (!*ppwzData)
{
WCHAR szEmpty[1] = L"";
er = ::MsiGetTargetPathW(WcaGetInstallHandle(), wzFolder, szEmpty, &cch);
if (ERROR_MORE_DATA == er || ERROR_SUCCESS == er)
{
++cch; //Add one for the null terminator
hr = StrAlloc(ppwzData, cch);
}
else
{
hr = HRESULT_FROM_WIN32(er);
}
ExitOnFailure(hr, "Failed to allocate string for target path of folder: '%ls'", wzFolder);
}
else
{
hr = StrMaxLength(*ppwzData, &cchMax);
ExitOnFailure(hr, "Failed to get previous size of string");
cch = (DWORD)min(MAXDWORD, cchMax);
}
er = ::MsiGetTargetPathW(WcaGetInstallHandle(), wzFolder, *ppwzData, &cch);
if (ERROR_MORE_DATA == er)
{
++cch;
hr = StrAlloc(ppwzData, cch);
ExitOnFailure(hr, "Failed to allocate string for target path of folder: '%ls'", wzFolder);
er = ::MsiGetTargetPathW(WcaGetInstallHandle(), wzFolder, *ppwzData, &cch);
}
ExitOnWin32Error(er, hr, "Failed to get target path for folder '%ls'", wzFolder);
LExit:
return hr;
}
/********************************************************************
WcaSetProperty - sets a string property value in the active install
********************************************************************/
extern "C" HRESULT WIXAPI WcaSetProperty(
__in_z LPCWSTR wzPropertyName,
__in_z LPCWSTR wzPropertyValue
)
{
HRESULT hr = S_OK;
if (!wzPropertyName || !*wzPropertyName || !wzPropertyValue)
return E_INVALIDARG;
UINT er = ::MsiSetPropertyW(WcaGetInstallHandle(), wzPropertyName, wzPropertyValue);
ExitOnWin32Error(er, hr, "failed to set property: %ls", wzPropertyName);
LExit:
return hr;
}
/********************************************************************
WcaSetIntProperty - sets a integer property value in the active install
********************************************************************/
extern "C" HRESULT WIXAPI WcaSetIntProperty(
__in_z LPCWSTR wzPropertyName,
__in int nPropertyValue
)
{
if (!wzPropertyName || !*wzPropertyName)
return E_INVALIDARG;
// 12 characters should be enough for a 32-bit int: 10 digits, 1 sign, 1 null
WCHAR wzPropertyValue[13];
HRESULT hr = StringCchPrintfW(wzPropertyValue, countof(wzPropertyValue), L"%d", nPropertyValue);
ExitOnFailure(hr, "failed to convert into string property value: %d", nPropertyValue);
UINT er = ::MsiSetPropertyW(WcaGetInstallHandle(), wzPropertyName, wzPropertyValue);
ExitOnWin32Error(er, hr, "failed to set property: %ls", wzPropertyName);
LExit:
return hr;
}
/********************************************************************
WcaIsPropertySet() - returns TRUE if property is set
********************************************************************/
extern "C" BOOL WIXAPI WcaIsPropertySet(
__in LPCSTR szProperty
)
{
DWORD cchProperty = 0;
char szEmpty[1] = "";
#ifdef DEBUG
UINT er =
#endif
::MsiGetPropertyA(WcaGetInstallHandle(), szProperty, szEmpty, &cchProperty);
AssertSz(ERROR_INVALID_PARAMETER != er && ERROR_INVALID_HANDLE != er, "Unexpected return value from ::MsiGetProperty()");
return 0 < cchProperty; // property is set if the length is greater than zero
}
/********************************************************************
WcaIsUnicodePropertySet() - returns TRUE if property is set
********************************************************************/
extern "C" BOOL WIXAPI WcaIsUnicodePropertySet(
__in LPCWSTR wzProperty
)
{
DWORD cchProperty = 0;
wchar_t wzEmpty[1] = L"";
#ifdef DEBUG
UINT er =
#endif
::MsiGetPropertyW(WcaGetInstallHandle(), wzProperty, wzEmpty, &cchProperty);
AssertSz(ERROR_INVALID_PARAMETER != er && ERROR_INVALID_HANDLE != er, "Unexpected return value from ::MsiGetProperty()");
return 0 < cchProperty; // property is set if the length is greater than zero
}
/********************************************************************
WcaGetRecordInteger() - gets an integer field out of a record
NOTE: returns S_FALSE if the field was null
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetRecordInteger(
__in MSIHANDLE hRec,
__in UINT uiField,
__inout int* piData
)
{
if (!hRec || !piData)
return E_INVALIDARG;
HRESULT hr = S_OK;
*piData = ::MsiRecordGetInteger(hRec, uiField);
if (MSI_NULL_INTEGER == *piData)
hr = S_FALSE;
//LExit:
return hr;
}
/********************************************************************
WcaGetRecordString() - gets a string field out of a record
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetRecordString(
__in MSIHANDLE hRec,
__in UINT uiField,
__inout LPWSTR* ppwzData
)
{
if (!hRec || !ppwzData)
return E_INVALIDARG;
HRESULT hr = S_OK;
UINT er;
DWORD cch = 0;
SIZE_T cchMax = 0;
if (!*ppwzData)
{
WCHAR szEmpty[1] = L"";
er = ::MsiRecordGetStringW(hRec, uiField, szEmpty, &cch);
if (ERROR_MORE_DATA == er || ERROR_SUCCESS == er)
{
hr = StrAlloc(ppwzData, ++cch);
}
else
{
hr = HRESULT_FROM_WIN32(er);
}
ExitOnFailure(hr, "Failed to allocate memory for record string");
}
else
{
hr = StrMaxLength(*ppwzData, &cchMax);
ExitOnFailure(hr, "Failed to get previous size of string");
cch = (DWORD)min(MAXDWORD, cchMax);
}
er = ::MsiRecordGetStringW(hRec, uiField, *ppwzData, &cch);
if (ERROR_MORE_DATA == er)
{
hr = StrAlloc(ppwzData, ++cch);
ExitOnFailure(hr, "Failed to allocate memory for record string");
er = ::MsiRecordGetStringW(hRec, uiField, *ppwzData, &cch);
}
ExitOnWin32Error(er, hr, "Failed to get string from record");
LExit:
return hr;
}
/********************************************************************
HideNulls() - internal helper function to escape [~] in formatted strings
********************************************************************/
static void HideNulls(
__inout_z LPWSTR wzData
)
{
LPWSTR pwz = wzData;
while(*pwz)
{
if (pwz[0] == L'[' && pwz[1] == L'~' && pwz[2] == L']') // found a null [~]
{
pwz[0] = L'!'; // turn it into !$!
pwz[1] = L'$';
pwz[2] = L'!';
pwz += 3;
}
else
{
++pwz;
}
}
}
/********************************************************************
RevealNulls() - internal helper function to unescape !$! in formatted strings
********************************************************************/
static void RevealNulls(
__inout_z LPWSTR wzData
)
{
LPWSTR pwz = wzData;
while(*pwz)
{
if (pwz[0] == L'!' && pwz[1] == L'$' && pwz[2] == L'!') // found the fake null !$!
{
pwz[0] = L'['; // turn it back into [~]
pwz[1] = L'~';
pwz[2] = L']';
pwz += 3;
}
else
{
++pwz;
}
}
}
/********************************************************************
WcaGetRecordFormattedString() - gets formatted string field from record
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetRecordFormattedString(
__in MSIHANDLE hRec,
__in UINT uiField,
__inout LPWSTR* ppwzData
)
{
if (!hRec || !ppwzData)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
UINT er;
DWORD cch = 0;
SIZE_T cchMax = 0;
PMSIHANDLE hRecFormat;
// get the format string
hr = WcaGetRecordString(hRec, uiField, ppwzData);
ExitOnFailure(hr, "failed to get string from record");
if (!**ppwzData)
{
ExitFunction();
}
// hide the nulls '[~]' so we can get them back after formatting
HideNulls(*ppwzData);
// set up the format record
hRecFormat = ::MsiCreateRecord(1);
ExitOnNull(hRecFormat, hr, E_UNEXPECTED, "Failed to create record to format string");
hr = WcaSetRecordString(hRecFormat, 0, *ppwzData);
ExitOnFailure(hr, "failed to set string to format record");
// format the string
hr = StrMaxLength(*ppwzData, &cchMax);
ExitOnFailure(hr, "failed to get max length of string");
cch = (DWORD)min(MAXDWORD, cchMax);
er = ::MsiFormatRecordW(WcaGetInstallHandle(), hRecFormat, *ppwzData, &cch);
if (ERROR_MORE_DATA == er)
{
hr = StrAlloc(ppwzData, ++cch);
ExitOnFailure(hr, "Failed to allocate memory for record string");
er = ::MsiFormatRecordW(WcaGetInstallHandle(), hRecFormat, *ppwzData, &cch);
}
ExitOnWin32Error(er, hr, "Failed to format string");
// put the nulls back
RevealNulls(*ppwzData);
LExit:
return hr;
}
/********************************************************************
WcaGetRecordFormattedInteger() - gets formatted integer from record
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetRecordFormattedInteger(
__in MSIHANDLE hRec,
__in UINT uiField,
__out int* piData
)
{
if (!hRec || !piData)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
LPWSTR pwzData = NULL;
hr = WcaGetRecordFormattedString(hRec, uiField, &pwzData);
ExitOnFailure(hr, "failed to get record field: %u", uiField);
if (pwzData && *pwzData)
{
LPWSTR wz = NULL;
*piData = wcstol(pwzData, &wz, 10);
if (wz && *wz)
{
hr = E_INVALIDARG;
ExitOnFailure(hr, "failed to parse record field: %u as number: %ls", uiField, pwzData);
}
}
else
{
*piData = MSI_NULL_INTEGER;
}
LExit:
return hr;
}
/********************************************************************
WcaAllocStream() - creates a byte stream of the specified size
NOTE: Use WcaFreeStream() to release the byte stream
********************************************************************/
extern "C" HRESULT WIXAPI WcaAllocStream(
__deref_out_bcount_part(cbData, 0) BYTE** ppbData,
__in SIZE_T cbData
)
{
Assert(ppbData);
HRESULT hr;
BYTE* pbNewData;
if (*ppbData)
pbNewData = static_cast<BYTE*>(MemReAlloc(*ppbData, cbData, TRUE));
else
pbNewData = static_cast<BYTE*>(MemAlloc(cbData, TRUE));
if (!pbNewData)
{
ExitOnLastError(hr, "Failed to allocate string");
}
*ppbData = pbNewData;
pbNewData = NULL;
hr = S_OK;
LExit:
ReleaseMem(pbNewData);
return hr;
}
/********************************************************************
WcaFreeStream() - frees a byte stream
********************************************************************/
extern "C" HRESULT WIXAPI WcaFreeStream(
__in BYTE* pbData
)
{
if (!pbData)
return E_INVALIDARG;
HRESULT hr = MemFree(pbData);
return hr;
}
/********************************************************************
WcaGetRecordStream() - gets a byte stream field from record
********************************************************************/
extern "C" HRESULT WIXAPI WcaGetRecordStream(
__in MSIHANDLE hRecBinary,
__in UINT uiField,
__deref_out_bcount_full(*pcbData) BYTE** ppbData,
__out DWORD* pcbData
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
if (!hRecBinary || !ppbData || !pcbData)
return E_INVALIDARG;
*pcbData = 0;
er = ::MsiRecordReadStream(hRecBinary, uiField, NULL, pcbData);
ExitOnWin32Error(er, hr, "failed to get size of stream");
hr = WcaAllocStream(ppbData, *pcbData);
ExitOnFailure(hr, "failed to allocate data for stream");
er = ::MsiRecordReadStream(hRecBinary, uiField, (char*)*ppbData, pcbData);
ExitOnWin32Error(er, hr, "failed to read from stream");
LExit:
return hr;
}
/********************************************************************
WcaSetRecordString() - set a string field in record
********************************************************************/
extern "C" HRESULT WIXAPI WcaSetRecordString(
__in MSIHANDLE hRec,
__in UINT uiField,
__in_z LPCWSTR wzData
)
{
if (!hRec || !wzData)
return E_INVALIDARG;
HRESULT hr = S_OK;
UINT er = ::MsiRecordSetStringW(hRec, uiField, wzData);
ExitOnWin32Error(er, hr, "failed to set string in record");
LExit:
return hr;
}
/********************************************************************
WcaSetRecordInteger() - set a integer field in record
********************************************************************/
extern "C" HRESULT WIXAPI WcaSetRecordInteger(
__in MSIHANDLE hRec,
__in UINT uiField,
__in int iValue
)
{
if (!hRec)
return E_INVALIDARG;
HRESULT hr = S_OK;
UINT er = ::MsiRecordSetInteger(hRec, uiField, iValue);
ExitOnWin32Error(er, hr, "failed to set integer in record");
LExit:
return hr;
}
/********************************************************************
WcaDoDeferredAction() - schedules an action at this point in the script
********************************************************************/
extern "C" HRESULT WIXAPI WcaDoDeferredAction(
__in_z LPCWSTR wzAction,
__in_z LPCWSTR wzCustomActionData,
__in UINT uiCost
)
{
HRESULT hr = S_OK;
UINT er;
if (wzCustomActionData && *wzCustomActionData)
{
er = ::MsiSetPropertyW(WcaGetInstallHandle(), wzAction, wzCustomActionData);
ExitOnWin32Error(er, hr, "Failed to set CustomActionData for deferred action");
}
if (0 < uiCost)
{
hr = WcaProgressMessage(uiCost, TRUE); // add ticks to the progress bar
// TODO: handle the return codes correctly
}
er = ::MsiDoActionW(WcaGetInstallHandle(), wzAction);
if (ERROR_INSTALL_USEREXIT == er)
{
WcaSetReturnValue(er);
}
ExitOnWin32Error(er, hr, "Failed MsiDoAction on deferred action");
LExit:
return hr;
}
/********************************************************************
WcaCountOfCustomActionDataRecords() - counts the number of records
passed to a deferred CustomAction
********************************************************************/
extern "C" DWORD WIXAPI WcaCountOfCustomActionDataRecords(
__in_z LPCWSTR wzData
)
{
WCHAR delim[] = {MAGIC_MULTISZ_DELIM, 0}; // magic char followed by NULL terminator
DWORD dwCount = 0;
// Loop through until there are no delimiters, we are at the end of the string, or the delimiter is the last character in the string
for (LPCWSTR pwzCurrent = wzData; pwzCurrent && *pwzCurrent && *(pwzCurrent + 1); pwzCurrent = wcsstr(pwzCurrent, delim))
{
++dwCount;
++pwzCurrent;
}
return dwCount;
}
/********************************************************************
BreakDownCustomActionData() - internal helper to chop up CustomActionData
NOTE: this modifies the passed in data
********************************************************************/
static LPWSTR BreakDownCustomActionData(
__inout LPWSTR* ppwzData
)
{
if (!ppwzData)
return NULL;
if (0 == *ppwzData)
return NULL;
WCHAR delim[] = {MAGIC_MULTISZ_DELIM, 0}; // magic char followed by Null terminator
LPWSTR pwzReturn = *ppwzData;
LPWSTR pwz = wcsstr(pwzReturn, delim);
if (pwz)
{
*pwz = 0;
*ppwzData = pwz + 1;
}
else
*ppwzData = 0;
return pwzReturn;
}
/********************************************************************
RevertCustomActionData() - Reverts custom action data changes made
by BreakDownCustomActionData;
NOTE: this modifies the passed in data
********************************************************************/
extern "C" void WIXAPI RevertCustomActionData(
__in LPWSTR wzRevertTo,
__in LPCWSTR wzRevertFrom
)
{
if (!wzRevertTo)
return;
if (!wzRevertFrom)
return;
// start at the revert point and replace all \0 with MAGIC_MULTISZ_DELIM
for(LPWSTR wzIndex = wzRevertTo; wzIndex < wzRevertFrom; wzIndex++)
{
if (0 == *wzIndex)
{
*wzIndex = MAGIC_MULTISZ_DELIM;
}
}
return;
}
/********************************************************************
WcaReadStringFromCaData() - reads a string out of the CustomActionData
NOTE: this modifies the passed in ppwzCustomActionData variable
********************************************************************/
extern "C" HRESULT WIXAPI WcaReadStringFromCaData(
__deref_in LPWSTR* ppwzCustomActionData,
__deref_out_z LPWSTR* ppwzString
)
{
HRESULT hr = S_OK;
LPCWSTR pwz = BreakDownCustomActionData(ppwzCustomActionData);
if (!pwz)
return E_NOMOREITEMS;
hr = StrAllocString(ppwzString, pwz, 0);
ExitOnFailure(hr, "failed to allocate memory for string");
hr = S_OK;
LExit:
return hr;
}
/********************************************************************
WcaReadIntegerFromCaData() - reads an integer out of the CustomActionData
NOTE: this modifies the passed in ppwzCustomActionData variable
********************************************************************/
extern "C" HRESULT WIXAPI WcaReadIntegerFromCaData(
__deref_in LPWSTR* ppwzCustomActionData,
__out int* piResult
)
{
LPCWSTR pwz = BreakDownCustomActionData(ppwzCustomActionData);
if (!pwz || !*pwz)
return E_NOMOREITEMS;
*piResult = wcstol(pwz, NULL, 10);
return S_OK;
}
/********************************************************************
WcaReadStreamFromCaData() - reads a stream out of the CustomActionData
NOTE: this modifies the passed in ppwzCustomActionData variable
NOTE: returned stream should be freed with WcaFreeStream()
********************************************************************/
extern "C" HRESULT WIXAPI WcaReadStreamFromCaData(
__deref_in LPWSTR* ppwzCustomActionData,
__deref_out_bcount(*pcbData) BYTE** ppbData,
__out DWORD_PTR* pcbData
)
{
HRESULT hr;
LPCWSTR pwz = BreakDownCustomActionData(ppwzCustomActionData);
if (!pwz)
return E_NOMOREITEMS;
hr = StrAllocBase85Decode(pwz, ppbData, pcbData);
ExitOnFailure(hr, "failed to decode string into stream");
LExit:
return hr;
}
/********************************************************************
WcaWriteStringToCaData() - adds a string to the CustomActionData to
feed a deferred CustomAction
********************************************************************/
extern "C" HRESULT WIXAPI WcaWriteStringToCaData(
__in_z LPCWSTR wzString,
__deref_inout_z_opt LPWSTR* ppwzCustomActionData
)
{
HRESULT hr = S_OK;
WCHAR delim[] = {MAGIC_MULTISZ_DELIM, 0}; // magic char followed by NULL terminator
SIZE_T cchString = 0;
SIZE_T cchCustomActionData = 0;
SIZE_T cchMax = 0;
if (!ppwzCustomActionData)
{
ExitFunction1(hr = E_INVALIDARG);
}
hr = ::StringCchLengthW(wzString, STRSAFE_MAX_LENGTH, reinterpret_cast<size_t*>(&cchString));
ExitOnRootFailure(hr, "failed to get length of ca data string");
++cchString; // assume we'll be adding the delim
if (*ppwzCustomActionData)
{
hr = StrMaxLength(*ppwzCustomActionData, &cchCustomActionData);
ExitOnFailure(hr, "failed to get max length of custom action data");
hr = ::StringCchLengthW(*ppwzCustomActionData, STRSAFE_MAX_LENGTH, reinterpret_cast<size_t*>(&cchMax));
ExitOnRootFailure(hr, "failed to get length of custom action data");
}
if ((cchCustomActionData - cchMax) < cchString + 1)
{
cchCustomActionData += cchString + 1 + 255; // add 255 for good measure
cchCustomActionData = min(STRSAFE_MAX_LENGTH, cchCustomActionData);
hr = StrAlloc(ppwzCustomActionData, cchCustomActionData);
ExitOnFailure(hr, "Failed to allocate memory for CustomActionData string");
}
if (**ppwzCustomActionData) // if data exists toss the delimiter on before adding more to the end
{
hr = ::StringCchCatW(*ppwzCustomActionData, cchCustomActionData, delim);
ExitOnRootFailure(hr, "Failed to concatenate CustomActionData string");
}
hr = ::StringCchCatW(*ppwzCustomActionData, cchCustomActionData, wzString);
ExitOnRootFailure(hr, "Failed to concatenate CustomActionData string");
LExit:
return hr;
}
/********************************************************************
WcaWriteIntegerToCaData() - adds an integer to the CustomActionData to
feed a deferred CustomAction
********************************************************************/
extern "C" HRESULT WIXAPI WcaWriteIntegerToCaData(
__in int i,
__deref_out_z_opt LPWSTR* ppwzCustomActionData
)
{
WCHAR wzBuffer[13];
HRESULT hr = StringCchPrintfW(wzBuffer, countof(wzBuffer), L"%d", i);
ExitOnFailure(hr, "failed to write integer to ca data");
hr = WcaWriteStringToCaData(wzBuffer, ppwzCustomActionData);
ExitOnFailure(hr, "failed to write integer to ca data");
LExit:
return hr;
}
/********************************************************************
WcaWriteStreamToCaData() - adds a byte stream to the CustomActionData to
feed a deferred CustomAction
********************************************************************/
extern "C" HRESULT WIXAPI WcaWriteStreamToCaData(
__in_bcount(cbData) const BYTE* pbData,
__in SIZE_T cbData,
__deref_inout_z_opt LPWSTR* ppwzCustomActionData
)
{
HRESULT hr;
LPWSTR pwzData = NULL;
hr = StrAllocBase85Encode(pbData, cbData, &pwzData);
ExitOnFailure(hr, "failed to encode data into string");
hr = WcaWriteStringToCaData(pwzData, ppwzCustomActionData);
LExit:
ReleaseStr(pwzData);
return hr;
}
/********************************************************************
WcaAddTempRecord - adds a temporary record to the active database
NOTE: you cannot use PMSIHANDLEs for the __in/__out parameters
NOTE: uiUniquifyColumn can be 0 if no column needs to be made unique
********************************************************************/
extern "C" HRESULT __cdecl WcaAddTempRecord(
__inout MSIHANDLE* phTableView,
__inout MSIHANDLE* phColumns,
__in_z LPCWSTR wzTable,
__out_opt MSIDBERROR* pdbError,
__in UINT uiUniquifyColumn,
__in UINT cColumns,
...
)
{
Assert(phTableView && phColumns);
static DWORD dwUniquifyValue = ::GetTickCount();
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR pwzQuery = NULL;
PMSIHANDLE hTempRec;
DWORD i;
va_list args;
LPWSTR pwzData = NULL;
LPWSTR pwzUniquify = NULL;
//
// if we don't have a table and its columns already
//
if (NULL == *phTableView)
{
// set the query
hr = StrAllocFormatted(&pwzQuery, L"SELECT * FROM `%s`",wzTable);
ExitOnFailure(hr, "failed to allocate string for query");
// Open and Execute the temp View
hr = WcaOpenExecuteView(pwzQuery, phTableView);
ExitOnFailure(hr, "failed to openexecute temp view with query %ls", pwzQuery);
}
if (NULL == *phColumns)
{
// use GetColumnInfo to populate the datatype record
er = ::MsiViewGetColumnInfo(*phTableView, MSICOLINFO_TYPES, phColumns);
ExitOnWin32Error(er, hr, "failed to columns for table: %ls", wzTable);
}
AssertSz(::MsiRecordGetFieldCount(*phColumns) == cColumns, "passed in argument does not match number of columns in table");
//
// create the temp record
//
hTempRec = ::MsiCreateRecord(cColumns);
ExitOnNull(hTempRec, hr, E_UNEXPECTED, "could not create temp record for table: %ls", wzTable);
//
// loop through all the columns filling in the data
//
va_start(args, cColumns);
for (i = 1; i <= cColumns; i++)
{
hr = WcaGetRecordString(*phColumns, i, &pwzData);
ExitOnFailure(hr, "failed to get the data type for %d", i);
// if data type is string write string
if (L's' == *pwzData || L'S' == *pwzData || L'g' == *pwzData || L'G' == *pwzData || L'l' == *pwzData || L'L' == *pwzData)
{
LPCWSTR wz = va_arg(args, WCHAR*);
// if this is the column that is supposed to be unique add the time stamp on the end
if (uiUniquifyColumn == i)
{
hr = StrAllocFormatted(&pwzUniquify, L"%s%u", wz, ++dwUniquifyValue); // up the count so we have no collisions on the unique name
ExitOnFailure(hr, "failed to allocate string for unique column: %d", uiUniquifyColumn);
wz = pwzUniquify;
}
er = ::MsiRecordSetStringW(hTempRec, i, wz);
ExitOnWin32Error(er, hr, "failed to set string value at position %d", i);
}
// if data type is integer write integer
else if (L'i' == *pwzData || L'I' == *pwzData || L'j' == *pwzData || L'J' == *pwzData)
{
AssertSz(uiUniquifyColumn != i, "Cannot uniquify an integer column");
int iData = va_arg(args, int);
er = ::MsiRecordSetInteger(hTempRec, i, iData);
ExitOnWin32Error(er, hr, "failed to set integer value at position %d", i);
}
else
{
// not supporting binary streams so error out
hr = HRESULT_FROM_WIN32(ERROR_DATATYPE_MISMATCH);
ExitOnRootFailure(hr, "unsupported data type '%ls' in column: %d", pwzData, i);
}
}
va_end(args);
//
// add the temporary record to the MSI
//
er = ::MsiViewModify(*phTableView, MSIMODIFY_INSERT_TEMPORARY, hTempRec);
hr = HRESULT_FROM_WIN32(er);
if (FAILED(hr))
{
if (pdbError)
{
// MSI provides only a generic ERROR_FUNCTION_FAILED if a temporary row
// can't be inserted; if we're being asked to provide the detailed error,
// get it using the MSIMODIFY_VALIDATE_NEW flag
er = ::MsiViewModify(*phTableView, MSIMODIFY_VALIDATE_NEW, hTempRec);
hr = HRESULT_FROM_WIN32(er);
}
WCHAR wzBuf[MAX_PATH];
DWORD cchBuf = countof(wzBuf);
MSIDBERROR dbErr = ::MsiViewGetErrorW(*phTableView, wzBuf, &cchBuf);
if (pdbError)
{
*pdbError = dbErr;
}
ExitOnFailure(hr, "failed to add temporary row, dberr: %d, err: %ls", dbErr, wzBuf);
}
LExit:
ReleaseStr(pwzUniquify);
ReleaseStr(pwzData);
ReleaseStr(pwzQuery);
return hr;
}
/********************************************************************
WcaDumpTable - dumps a table to the log file
********************************************************************/
extern "C" HRESULT WIXAPI WcaDumpTable(
__in_z LPCWSTR wzTable
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR pwzQuery = NULL;
PMSIHANDLE hView;
PMSIHANDLE hColumns;
DWORD cColumns = 0;
PMSIHANDLE hRec;
LPWSTR pwzData = NULL;
LPWSTR pwzPrint = NULL;
hr = StrAllocFormatted(&pwzQuery, L"SELECT * FROM `%s`",wzTable);
ExitOnFailure(hr, "failed to allocate string for query");
// Open and Execute the temp View
hr = WcaOpenExecuteView(pwzQuery, &hView);
ExitOnFailure(hr, "failed to openexecute temp view with query %ls", pwzQuery);
// Use GetColumnInfo to populate the names of the columns.
er = ::MsiViewGetColumnInfo(hView, MSICOLINFO_NAMES, &hColumns);
hr = HRESULT_FROM_WIN32(er);
ExitOnFailure(hr, "failed to get column info for table: %ls", wzTable);
cColumns = ::MsiRecordGetFieldCount(hColumns);
WcaLog(LOGMSG_STANDARD, "--- Begin Table Dump %ls ---", wzTable);
// Loop through all the columns filling in the data.
for (DWORD i = 1; i <= cColumns; i++)
{
hr = WcaGetRecordString(hColumns, i, &pwzData);
ExitOnFailure(hr, "failed to get the column name for %d", i);
hr = StrAllocConcat(&pwzPrint, pwzData, 0);
ExitOnFailure(hr, "Failed to add column name.");
hr = StrAllocConcat(&pwzPrint, L"\t", 1);
ExitOnFailure(hr, "Failed to add column name.");
}
WcaLog(LOGMSG_STANDARD, "%ls", pwzPrint);
// Now dump the actual rows.
while (S_OK == (hr = WcaFetchRecord(hView, &hRec)))
{
if (pwzPrint && *pwzPrint)
{
*pwzPrint = L'\0';
}
for (DWORD i = 1; i <= cColumns; i++)
{
hr = WcaGetRecordString(hRec, i, &pwzData);
ExitOnFailure(hr, "failed to get the column name for %d", i);
hr = StrAllocConcat(&pwzPrint, pwzData, 0);
ExitOnFailure(hr, "Failed to add column name.");
hr = StrAllocConcat(&pwzPrint, L"\t", 1);
ExitOnFailure(hr, "Failed to add column name.");
}
WcaLog(LOGMSG_STANDARD, "%ls", pwzPrint);
}
WcaLog(LOGMSG_STANDARD, "--- End Table Dump %ls ---", wzTable);
LExit:
ReleaseStr(pwzPrint);
ReleaseStr(pwzData);
ReleaseStr(pwzQuery);
return hr;
}
HRESULT WIXAPI WcaDeferredActionRequiresReboot()
{
HRESULT hr = S_OK;
ATOM atomReboot = 0;
atomReboot = ::GlobalAddAtomW(L"WcaDeferredActionRequiresReboot");
ExitOnNullWithLastError(atomReboot, hr, "Failed to create WcaDeferredActionRequiresReboot global atom.");
LExit:
return hr;
}
BOOL WIXAPI WcaDidDeferredActionRequireReboot()
{
// NOTE: This function does not delete the global atom. That is done
// purposefully so that any other installs that occur after this point also
// require a reboot.
ATOM atomReboot = ::GlobalFindAtomW(L"WcaDeferredActionRequiresReboot");
return 0 != atomReboot;
}
|