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
|
// 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.
namespace WixToolset.Dtf.WindowsInstaller
{
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// The Record object is a container for holding and transferring a variable number of values.
/// Fields within the record are numerically indexed and can contain strings, integers, streams,
/// and null values. Record fields are indexed starting with 1. Field 0 is a special format field.
/// </summary>
/// <remarks><p>
/// Most methods on the Record class have overloads that allow using either a number
/// or a name to designate a field. However note that field names only exist when the
/// Record is directly returned from a query on a database. For other records, attempting
/// to access a field by name will result in an InvalidOperationException.
/// </p></remarks>
public class Record : InstallerHandle
{
private View view;
private bool isFormatStringInvalid;
/// <summary>
/// IsFormatStringInvalid is set from several View methods that invalidate the FormatString
/// and used to determine behavior during Record.ToString().
/// </summary>
internal protected bool IsFormatStringInvalid
{
set { this.isFormatStringInvalid = value; }
get { return this.isFormatStringInvalid; }
}
/// <summary>
/// Creates a new record object with the requested number of fields.
/// </summary>
/// <param name="fieldCount">Required number of fields, which may be 0.
/// The maximum number of fields in a record is limited to 65535.</param>
/// <remarks><p>
/// The Record object should be <see cref="InstallerHandle.Close"/>d after use.
/// It is best that the handle be closed manually as soon as it is no longer
/// needed, as leaving lots of unused handles open can degrade performance.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msicreaterecord.asp">MsiCreateRecord</a>
/// </p></remarks>
public Record(int fieldCount)
: this((IntPtr) RemotableNativeMethods.MsiCreateRecord((uint) fieldCount, 0), true, (View) null)
{
}
/// <summary>
/// Creates a new record object, providing values for an arbitrary number of fields.
/// </summary>
/// <param name="fields">The values of the record fields. The parameters should be of type Int16, Int32 or String</param>
/// <remarks><p>
/// The Record object should be <see cref="InstallerHandle.Close"/>d after use.
/// It is best that the handle be closed manually as soon as it is no longer
/// needed, as leaving lots of unused handles open can degrade performance.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msicreaterecord.asp">MsiCreateRecord</a>
/// </p></remarks>
public Record(params object[] fields)
: this(fields.Length)
{
for (int i = 0; i < fields.Length; i++)
{
this[i + 1] = fields[i];
}
}
internal Record(IntPtr handle, bool ownsHandle, View view)
: base(handle, ownsHandle)
{
this.view = view;
}
/// <summary>
/// Gets the number of fields in a record.
/// </summary>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordgetfieldcount.asp">MsiRecordGetFieldCount</a>
/// </p></remarks>
public int FieldCount
{
get
{
return (int) RemotableNativeMethods.MsiRecordGetFieldCount((int) this.Handle);
}
}
/// <summary>
/// Gets or sets field 0 of the Record, which is the format string.
/// </summary>
public string FormatString
{
get { return this.GetString(0); }
set { this.SetString(0, value); }
}
/// <summary>
/// Gets or sets a record field value.
/// </summary>
/// <param name="fieldName">Specifies the name of the field of the Record to get or set.</param>
/// <exception cref="ArgumentOutOfRangeException">The name does not match any known field of the Record.</exception>
/// <remarks><p>
/// When getting a field, the type of the object returned depends on the type of the Record field.
/// The object will be one of: Int16, Int32, String, Stream, or null.
/// </p><p>
/// When setting a field, the type of the object provided will be converted to match the View
/// query that returned the record, or if Record was not returned from a view then the type of
/// the object provided will determine the type of the Record field. The object should be one of:
/// Int16, Int32, String, Stream, or null.
/// </p></remarks>
public object this[string fieldName]
{
get
{
int field = this.FindColumn(fieldName);
return this[field];
}
set
{
int field = this.FindColumn(fieldName);
this[field] = value;
}
}
/// <summary>
/// Gets or sets a record field value.
/// </summary>
/// <param name="field">Specifies the field of the Record to get or set.</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Record fields are indexed starting with 1. Field 0 is a special format field.
/// </p><p>
/// When getting a field, the type of the object returned depends on the type of the Record field.
/// The object will be one of: Int16, Int32, String, Stream, or null. If the Record was returned
/// from a View, the type will match that of the field from the View query. Otherwise, the type
/// will match the type of the last value set for the field.
/// </p><p>
/// When setting a field, the type of the object provided will be converted to match the View
/// query that returned the Record, or if Record was not returned from a View then the type of
/// the object provided will determine the type of the Record field. The object should be one of:
/// Int16, Int32, String, Stream, or null.
/// </p><p>
/// The type-specific getters and setters are slightly more efficient than this property, since
/// they don't have to do the extra work to infer the value's type every time.
/// </p><p>
/// Win32 MSI APIs:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordgetinteger.asp">MsiRecordGetInteger</a>,
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordgetstring.asp">MsiRecordGetString</a>,
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetinteger.asp">MsiRecordSetInteger</a>,
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetstring.asp">MsiRecordSetString</a>
/// </p></remarks>
public object this[int field]
{
get
{
if (field == 0)
{
return this.GetString(0);
}
else
{
Type valueType = null;
if (this.view != null)
{
this.CheckRange(field);
valueType = this.view.Columns[field - 1].Type;
}
if (valueType == null || valueType == typeof(String))
{
return this.GetString(field);
}
else if (valueType == typeof(Stream))
{
return this.IsNull(field) ? null : new RecordStream(this, field);
}
else
{
int? value = this.GetNullableInteger(field);
return value.HasValue ? (object) value.Value : null;
}
}
}
set
{
if (field == 0)
{
if (value == null)
{
value = String.Empty;
}
this.SetString(0, value.ToString());
}
else if (value == null)
{
this.SetNullableInteger(field, null);
}
else
{
Type valueType = value.GetType();
if (valueType == typeof(Int32) || valueType == typeof(Int16))
{
this.SetInteger(field, (int) value);
}
else if (valueType.IsSubclassOf(typeof(Stream)))
{
this.SetStream(field, (Stream) value);
}
else
{
this.SetString(field, value.ToString());
}
}
}
}
/// <summary>
/// Creates a new Record object from an integer record handle.
/// </summary>
/// <remarks><p>
/// This method is only provided for interop purposes. A Record object
/// should normally be obtained by calling <see cref="WixToolset.Dtf.WindowsInstaller.View.Fetch"/>
/// other methods.
/// <p>The handle will be closed when this object is disposed or finalized.</p>
/// </p></remarks>
/// <param name="handle">Integer record handle</param>
/// <param name="ownsHandle">true to close the handle when this object is disposed or finalized</param>
public static Record FromHandle(IntPtr handle, bool ownsHandle)
{
return new Record(handle, ownsHandle, (View) null);
}
/// <summary>
/// Sets all fields in a record to null.
/// </summary>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordcleardata.asp">MsiRecordClearData</a>
/// </p></remarks>
public void Clear()
{
uint ret = RemotableNativeMethods.MsiRecordClearData((int) this.Handle);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
/// <summary>
/// Reports whether a record field is null.
/// </summary>
/// <param name="field">Specifies the field to check.</param>
/// <returns>True if the field is null, false otherwise.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordisnull.asp">MsiRecordIsNull</a>
/// </p></remarks>
public bool IsNull(int field)
{
this.CheckRange(field);
return RemotableNativeMethods.MsiRecordIsNull((int) this.Handle, (uint) field);
}
/// <summary>
/// Reports whether a record field is null.
/// </summary>
/// <param name="fieldName">Specifies the field to check.</param>
/// <returns>True if the field is null, false otherwise.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
public bool IsNull(string fieldName)
{
int field = this.FindColumn(fieldName);
return this.IsNull(field);
}
/// <summary>
/// Gets the length of a record field. The count does not include the terminating null.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// The returned data size is 0 if the field is null, non-existent,
/// or an internal object pointer. The method also returns 0 if the handle is not a valid
/// Record handle.
/// </p><p>
/// If the data is in integer format, the property returns 2 or 4.
/// </p><p>
/// If the data is in string format, the property returns the character count
/// (not including the NULL terminator).
/// </p><p>
/// If the data is in stream format, the property returns the byte count.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecorddatasize.asp">MsiRecordDataSize</a>
/// </p></remarks>
public int GetDataSize(int field)
{
this.CheckRange(field);
return (int) RemotableNativeMethods.MsiRecordDataSize((int) this.Handle, (uint) field);
}
/// <summary>
/// Gets the length of a record field. The count does not include the terminating null.
/// </summary>
/// <param name="fieldName">Specifies the field to check.</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <remarks><p>The returned data size is 0 if the field is null, non-existent,
/// or an internal object pointer. The method also returns 0 if the handle is not a valid
/// Record handle.
/// </p><p>
/// If the data is in integer format, the property returns 2 or 4.
/// </p><p>
/// If the data is in string format, the property returns the character count
/// (not including the NULL terminator).
/// </p><p>
/// If the data is in stream format, the property returns the byte count.
/// </p></remarks>
public int GetDataSize(string fieldName)
{
int field = this.FindColumn(fieldName);
return this.GetDataSize(field);
}
/// <summary>
/// Gets a field value as an integer.
/// </summary>
/// <param name="field">Specifies the field to retrieve.</param>
/// <returns>Integer value of the field, or 0 if the field is null.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordgetinteger.asp">MsiRecordGetInteger</a>
/// </p></remarks>
/// <seealso cref="GetNullableInteger(int)"/>
[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "integer")]
public int GetInteger(int field)
{
this.CheckRange(field);
int value = RemotableNativeMethods.MsiRecordGetInteger((int) this.Handle, (uint) field);
if (value == Int32.MinValue) // MSI_NULL_INTEGER
{
return 0;
}
return value;
}
/// <summary>
/// Gets a field value as an integer.
/// </summary>
/// <param name="fieldName">Specifies the field to retrieve.</param>
/// <returns>Integer value of the field, or 0 if the field is null.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <seealso cref="GetNullableInteger(string)"/>
[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "integer")]
public int GetInteger(string fieldName)
{
int field = this.FindColumn(fieldName);
return this.GetInteger(field);
}
/// <summary>
/// Gets a field value as an integer.
/// </summary>
/// <param name="field">Specifies the field to retrieve.</param>
/// <returns>Integer value of the field, or null if the field is null.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordgetinteger.asp">MsiRecordGetInteger</a>
/// </p></remarks>
/// <seealso cref="GetInteger(int)"/>
public int? GetNullableInteger(int field)
{
this.CheckRange(field);
int value = RemotableNativeMethods.MsiRecordGetInteger((int) this.Handle, (uint) field);
if (value == Int32.MinValue) // MSI_NULL_INTEGER
{
return null;
}
return value;
}
/// <summary>
/// Gets a field value as an integer.
/// </summary>
/// <param name="fieldName">Specifies the field to retrieve.</param>
/// <returns>Integer value of the field, or null if the field is null.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <seealso cref="GetInteger(string)"/>
public int? GetNullableInteger(string fieldName)
{
int field = this.FindColumn(fieldName);
return this.GetInteger(field);
}
/// <summary>
/// Sets the value of a field to an integer.
/// </summary>
/// <param name="field">Specifies the field to set.</param>
/// <param name="value">new value of the field</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetinteger.asp">MsiRecordSetInteger</a>
/// </p></remarks>
/// <seealso cref="SetNullableInteger(int,int?)"/>
public void SetInteger(int field, int value)
{
this.CheckRange(field);
uint ret = RemotableNativeMethods.MsiRecordSetInteger((int) this.Handle, (uint) field, value);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
/// <summary>
/// Sets the value of a field to an integer.
/// </summary>
/// <param name="fieldName">Specifies the field to set.</param>
/// <param name="value">new value of the field</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <seealso cref="SetNullableInteger(string,int?)"/>
public void SetInteger(string fieldName, int value)
{
int field = this.FindColumn(fieldName);
this.SetInteger(field, value);
}
/// <summary>
/// Sets the value of a field to a nullable integer.
/// </summary>
/// <param name="field">Specifies the field to set.</param>
/// <param name="value">new value of the field</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetinteger.asp">MsiRecordSetInteger</a>
/// </p></remarks>
/// <seealso cref="SetInteger(int,int)"/>
public void SetNullableInteger(int field, int? value)
{
this.CheckRange(field);
uint ret = RemotableNativeMethods.MsiRecordSetInteger(
(int) this.Handle,
(uint) field,
value.HasValue ? (int) value : Int32.MinValue);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
/// <summary>
/// Sets the value of a field to a nullable integer.
/// </summary>
/// <param name="fieldName">Specifies the field to set.</param>
/// <param name="value">new value of the field</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <seealso cref="SetInteger(string,int)"/>
public void SetNullableInteger(string fieldName, int? value)
{
int field = this.FindColumn(fieldName);
this.SetNullableInteger(field, value);
}
/// <summary>
/// Gets a field value as a string.
/// </summary>
/// <param name="field">Specifies the field to retrieve.</param>
/// <returns>String value of the field, or an empty string if the field is null.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordgetstring.asp">MsiRecordGetString</a>
/// </p></remarks>
public string GetString(int field)
{
this.CheckRange(field);
StringBuilder buf = new StringBuilder(String.Empty);
uint bufSize = 0;
uint ret = RemotableNativeMethods.MsiRecordGetString((int) this.Handle, (uint) field, buf, ref bufSize);
if (ret == (uint) NativeMethods.Error.MORE_DATA)
{
buf.Capacity = (int) ++bufSize;
ret = RemotableNativeMethods.MsiRecordGetString((int) this.Handle, (uint) field, buf, ref bufSize);
}
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
return buf.ToString();
}
/// <summary>
/// Gets a field value as a string.
/// </summary>
/// <param name="fieldName">Specifies the field to retrieve.</param>
/// <returns>String value of the field, or an empty string if the field is null.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
public string GetString(string fieldName)
{
int field = this.FindColumn(fieldName);
return this.GetString(field);
}
/// <summary>
/// Sets the value of a field to a string.
/// </summary>
/// <param name="field">Specifies the field to set.</param>
/// <param name="value">new value of the field</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetstring.asp">MsiRecordSetString</a>
/// </p></remarks>
public void SetString(int field, string value)
{
this.CheckRange(field);
if (value == null)
{
value = String.Empty;
}
uint ret = RemotableNativeMethods.MsiRecordSetString((int) this.Handle, (uint) field, value);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
// If we set the FormatString manually, then it should be valid again
if (field == 0)
{
this.IsFormatStringInvalid = false;
}
}
/// <summary>
/// Sets the value of a field to a string.
/// </summary>
/// <param name="fieldName">Specifies the field to set.</param>
/// <param name="value">new value of the field</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
public void SetString(string fieldName, string value)
{
int field = this.FindColumn(fieldName);
this.SetString(field, value);
}
/// <summary>
/// Reads a record stream field into a file.
/// </summary>
/// <param name="field">Specifies the field of the Record to get.</param>
/// <param name="filePath">Specifies the path to the file to contain the stream.</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <exception cref="NotSupportedException">Attempt to extract a storage from a database open
/// in read-write mode, or from a database without an associated file path</exception>
/// <remarks><p>
/// This method is capable of directly extracting substorages. To do so, first select both the
/// `Name` and `Data` column of the `_Storages` table, then get the stream of the `Data` field.
/// However, substorages may only be extracted from a database that is open in read-only mode.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordreadstream.asp">MsiRecordReadStream</a>
/// </p></remarks>
public void GetStream(int field, string filePath)
{
if (String.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException("filePath");
}
IList<TableInfo> tables = (this.view != null ? this.view.Tables : null);
if (tables != null && tables.Count == 1 && tables[0].Name == "_Storages" && field == this.FindColumn("Data"))
{
if (!this.view.Database.IsReadOnly)
{
throw new NotSupportedException("Database must be opened read-only to support substorage extraction.");
}
else if (this.view.Database.FilePath == null)
{
throw new NotSupportedException("Database must have an associated file path to support substorage extraction.");
}
else if (this.FindColumn("Name") <= 0)
{
throw new NotSupportedException("Name column must be part of the Record in order to extract substorage.");
}
else
{
Record.ExtractSubStorage(this.view.Database.FilePath, this.GetString("Name"), filePath);
}
}
else
{
if (!this.IsNull(field))
{
Stream readStream = null, writeStream = null;
try
{
readStream = new RecordStream(this, field);
writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
int count = 512;
byte[] buf = new byte[count];
while (count == buf.Length)
{
if ((count = readStream.Read(buf, 0, buf.Length)) > 0)
{
writeStream.Write(buf, 0, count);
}
}
}
finally
{
if (readStream != null) readStream.Close();
if (writeStream != null) writeStream.Close();
}
}
}
}
/// <summary>
/// Reads a record stream field into a file.
/// </summary>
/// <param name="fieldName">Specifies the field of the Record to get.</param>
/// <param name="filePath">Specifies the path to the file to contain the stream.</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <exception cref="NotSupportedException">Attempt to extract a storage from a database open
/// in read-write mode, or from a database without an associated file path</exception>
/// <remarks><p>
/// This method is capable of directly extracting substorages. To do so, first select both the
/// `Name` and `Data` column of the `_Storages` table, then get the stream of the `Data` field.
/// However, substorages may only be extracted from a database that is open in read-only mode.
/// </p></remarks>
public void GetStream(string fieldName, string filePath)
{
int field = this.FindColumn(fieldName);
this.GetStream(field, filePath);
}
/// <summary>
/// Gets a record stream field.
/// </summary>
/// <param name="field">Specifies the field of the Record to get.</param>
/// <returns>A Stream that reads the field data.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// This method is not capable of reading substorages. To extract a substorage,
/// use <see cref="GetStream(int,string)"/>.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordreadstream.asp">MsiRecordReadStream</a>
/// </p></remarks>
public Stream GetStream(int field)
{
this.CheckRange(field);
return this.IsNull(field) ? null : new RecordStream(this, field);
}
/// <summary>
/// Gets a record stream field.
/// </summary>
/// <param name="fieldName">Specifies the field of the Record to get.</param>
/// <returns>A Stream that reads the field data.</returns>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <remarks><p>
/// This method is not capable of reading substorages. To extract a substorage,
/// use <see cref="GetStream(string,string)"/>.
/// </p></remarks>
public Stream GetStream(string fieldName)
{
int field = this.FindColumn(fieldName);
return this.GetStream(field);
}
/// <summary>
/// Sets a record stream field from a file. Stream data cannot be inserted into temporary fields.
/// </summary>
/// <param name="field">Specifies the field of the Record to set.</param>
/// <param name="filePath">Specifies the path to the file containing the stream.</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// The contents of the specified file are read into a stream object. The stream persists if
/// the Record is inserted into the Database and the Database is committed.
/// </p><p>
/// To reset the stream to its beginning you must pass in null for filePath.
/// Do not pass an empty string, "", to reset the stream.
/// </p><p>
/// Setting a stream with this method is more efficient than setting a field to a
/// FileStream object.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetstream.asp">MsiRecordsetStream</a>
/// </p></remarks>
public void SetStream(int field, string filePath)
{
this.CheckRange(field);
if (String.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException("filePath");
}
uint ret = RemotableNativeMethods.MsiRecordSetStream((int) this.Handle, (uint) field, filePath);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
/// <summary>
/// Sets a record stream field from a file. Stream data cannot be inserted into temporary fields.
/// </summary>
/// <param name="fieldName">Specifies the field name of the Record to set.</param>
/// <param name="filePath">Specifies the path to the file containing the stream.</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <remarks><p>
/// The contents of the specified file are read into a stream object. The stream persists if
/// the Record is inserted into the Database and the Database is committed.
/// To reset the stream to its beginning you must pass in null for filePath.
/// Do not pass an empty string, "", to reset the stream.
/// </p><p>
/// Setting a stream with this method is more efficient than setting a field to a
/// FileStream object.
/// </p></remarks>
public void SetStream(string fieldName, string filePath)
{
int field = this.FindColumn(fieldName);
this.SetStream(field, filePath);
}
/// <summary>
/// Sets a record stream field from a Stream object. Stream data cannot be inserted into temporary fields.
/// </summary>
/// <param name="field">Specifies the field of the Record to set.</param>
/// <param name="stream">Specifies the stream data.</param>
/// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the
/// number of fields in the Record.</exception>
/// <remarks><p>
/// The stream persists if the Record is inserted into the Database and the Database is committed.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetstream.asp">MsiRecordsetStream</a>
/// </p></remarks>
public void SetStream(int field, Stream stream)
{
this.CheckRange(field);
if (stream == null)
{
uint ret = RemotableNativeMethods.MsiRecordSetStream((int) this.Handle, (uint) field, null);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
else
{
Stream writeStream = null;
string tempPath = Path.GetTempFileName();
try
{
writeStream = new FileStream(tempPath, FileMode.Truncate, FileAccess.Write);
byte[] buf = new byte[512];
int count;
while ((count = stream.Read(buf, 0, buf.Length)) > 0)
{
writeStream.Write(buf, 0, count);
}
writeStream.Close();
writeStream = null;
uint ret = RemotableNativeMethods.MsiRecordSetStream((int) this.Handle, (uint) field, tempPath);
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
}
finally
{
if (writeStream != null) writeStream.Close();
if (File.Exists(tempPath))
{
try
{
File.Delete(tempPath);
}
catch (IOException)
{
if (this.view != null)
{
this.view.Database.DeleteOnClose(tempPath);
}
}
}
}
}
}
/// <summary>
/// Sets a record stream field from a Stream object. Stream data cannot be inserted into temporary fields.
/// </summary>
/// <param name="fieldName">Specifies the field name of the Record to set.</param>
/// <param name="stream">Specifies the stream data.</param>
/// <exception cref="ArgumentOutOfRangeException">The field name does not match any
/// of the named fields in the Record.</exception>
/// <remarks><p>
/// The stream persists if the Record is inserted into the Database and the Database is committed.
/// </p></remarks>
public void SetStream(string fieldName, Stream stream)
{
int field = this.FindColumn(fieldName);
this.SetStream(field, stream);
}
/// <summary>
/// Gets a formatted string representation of the Record.
/// </summary>
/// <returns>A formatted string representation of the Record.</returns>
/// <remarks><p>
/// If field 0 of the Record is set to a nonempty string, it is used to format the data in the Record.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiformatrecord.asp">MsiFormatRecord</a>
/// </p></remarks>
/// <seealso cref="FormatString"/>
/// <seealso cref="Session.FormatRecord(Record)"/>
public override string ToString()
{
return this.ToString((IFormatProvider) null);
}
/// <summary>
/// Gets a formatted string representation of the Record, optionally using a Session to format properties.
/// </summary>
/// <param name="provider">an optional Session instance that will be used to lookup any
/// properties in the Record's format string</param>
/// <returns>A formatted string representation of the Record.</returns>
/// <remarks><p>
/// If field 0 of the Record is set to a nonempty string, it is used to format the data in the Record.
/// </p><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiformatrecord.asp">MsiFormatRecord</a>
/// </p></remarks>
/// <seealso cref="FormatString"/>
/// <seealso cref="Session.FormatRecord(Record)"/>
public string ToString(IFormatProvider provider)
{
if (this.IsFormatStringInvalid) // Format string is invalid
{
// TODO: return all values by default?
return String.Empty;
}
InstallerHandle session = provider as InstallerHandle;
int sessionHandle = session != null ? (int) session.Handle : 0;
StringBuilder buf = new StringBuilder(String.Empty);
uint bufSize = 1;
uint ret = RemotableNativeMethods.MsiFormatRecord(sessionHandle, (int) this.Handle, buf, ref bufSize);
if (ret == (uint) NativeMethods.Error.MORE_DATA)
{
bufSize++;
buf = new StringBuilder((int) bufSize);
ret = RemotableNativeMethods.MsiFormatRecord(sessionHandle, (int) this.Handle, buf, ref bufSize);
}
if (ret != 0)
{
throw InstallerException.ExceptionFromReturnCode(ret);
}
return buf.ToString();
}
/// <summary>
/// Gets a formatted string representation of the Record.
/// </summary>
/// <param name="format">String to be used to format the data in the Record,
/// instead of the Record's format string.</param>
/// <returns>A formatted string representation of the Record.</returns>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiformatrecord.asp">MsiFormatRecord</a>
/// </p></remarks>
[Obsolete("This method is obsolete because it has undesirable side-effects. As an alternative, set the FormatString " +
"property separately before calling the ToString() override that takes no parameters.")]
public string ToString(string format)
{
return this.ToString(format, null);
}
/// <summary>
/// Gets a formatted string representation of the Record, optionally using a Session to format properties.
/// </summary>
/// <param name="format">String to be used to format the data in the Record,
/// instead of the Record's format string.</param>
/// <param name="provider">an optional Session instance that will be used to lookup any
/// properties in the Record's format string</param>
/// <returns>A formatted string representation of the Record.</returns>
/// <remarks><p>
/// Win32 MSI API:
/// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiformatrecord.asp">MsiFormatRecord</a>
/// </p></remarks>
/// <seealso cref="FormatString"/>
/// <seealso cref="Session.FormatRecord(Record)"/>
[Obsolete("This method is obsolete because it has undesirable side-effects. As an alternative, set the FormatString " +
"property separately before calling the ToString() override that takes just a format provider.")]
public string ToString(string format, IFormatProvider provider)
{
if (format == null)
{
return this.ToString(provider);
}
else if (format.Length == 0)
{
return String.Empty;
}
else
{
string savedFormatString = (string) this[0];
try
{
this.FormatString = format;
return this.ToString(provider);
}
finally
{
this.FormatString = savedFormatString;
}
}
}
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
private static void ExtractSubStorage(string databaseFile, string storageName, string extractFile)
{
IStorage storage;
NativeMethods.STGM openMode = NativeMethods.STGM.READ | NativeMethods.STGM.SHARE_DENY_WRITE;
int hr = NativeMethods.StgOpenStorage(databaseFile, IntPtr.Zero, (uint) openMode, IntPtr.Zero, 0, out storage);
if (hr != 0)
{
Marshal.ThrowExceptionForHR(hr);
}
try
{
openMode = NativeMethods.STGM.READ | NativeMethods.STGM.SHARE_EXCLUSIVE;
IStorage subStorage = storage.OpenStorage(storageName, IntPtr.Zero, (uint) openMode, IntPtr.Zero, 0);
try
{
IStorage newStorage;
openMode = NativeMethods.STGM.CREATE | NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE;
hr = NativeMethods.StgCreateDocfile(extractFile, (uint) openMode, 0, out newStorage);
if (hr != 0)
{
Marshal.ThrowExceptionForHR(hr);
}
try
{
subStorage.CopyTo(0, IntPtr.Zero, IntPtr.Zero, newStorage);
newStorage.Commit(0);
}
finally
{
Marshal.ReleaseComObject(newStorage);
}
}
finally
{
Marshal.ReleaseComObject(subStorage);
}
}
finally
{
Marshal.ReleaseComObject(storage);
}
}
private int FindColumn(string fieldName)
{
if (this.view == null)
{
throw new InvalidOperationException();
}
ColumnCollection columns = this.view.Columns;
for (int i = 0; i < columns.Count; i++)
{
if (columns[i].Name == fieldName)
{
return i + 1;
}
}
throw new ArgumentOutOfRangeException("fieldName");
}
private void CheckRange(int field)
{
if (field < 0 || field > this.FieldCount)
{
throw new ArgumentOutOfRangeException("field");
}
}
}
}
|