diff options
Diffstat (limited to 'src/WixToolset.Core.WindowsInstaller/Msi/Record.cs')
-rw-r--r-- | src/WixToolset.Core.WindowsInstaller/Msi/Record.cs | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/src/WixToolset.Core.WindowsInstaller/Msi/Record.cs b/src/WixToolset.Core.WindowsInstaller/Msi/Record.cs new file mode 100644 index 00000000..438aa3b0 --- /dev/null +++ b/src/WixToolset.Core.WindowsInstaller/Msi/Record.cs | |||
@@ -0,0 +1,182 @@ | |||
1 | // 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. | ||
2 | |||
3 | namespace WixToolset.Msi | ||
4 | { | ||
5 | using System; | ||
6 | using System.ComponentModel; | ||
7 | using System.Text; | ||
8 | using WixToolset.Core.Native; | ||
9 | |||
10 | /// <summary> | ||
11 | /// Wrapper class around msi.dll interop for a record. | ||
12 | /// </summary> | ||
13 | public sealed class Record : MsiHandle | ||
14 | { | ||
15 | /// <summary> | ||
16 | /// Creates a record with the specified number of fields. | ||
17 | /// </summary> | ||
18 | /// <param name="fieldCount">Number of fields in record.</param> | ||
19 | public Record(int fieldCount) | ||
20 | { | ||
21 | this.Handle = MsiInterop.MsiCreateRecord(fieldCount); | ||
22 | if (0 == this.Handle) | ||
23 | { | ||
24 | throw new OutOfMemoryException(); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | /// <summary> | ||
29 | /// Creates a record from a handle. | ||
30 | /// </summary> | ||
31 | /// <param name="handle">Handle to create record from.</param> | ||
32 | internal Record(uint handle) | ||
33 | { | ||
34 | this.Handle = handle; | ||
35 | } | ||
36 | |||
37 | /// <summary> | ||
38 | /// Gets a string value at specified location. | ||
39 | /// </summary> | ||
40 | /// <param name="field">Index into record to get string.</param> | ||
41 | public string this[int field] | ||
42 | { | ||
43 | get { return this.GetString(field); } | ||
44 | set { this.SetString(field, (string)value); } | ||
45 | } | ||
46 | |||
47 | /// <summary> | ||
48 | /// Determines if the value is null at the specified location. | ||
49 | /// </summary> | ||
50 | /// <param name="field">Index into record of the field to query.</param> | ||
51 | /// <returns>true if the value is null, false otherwise.</returns> | ||
52 | public bool IsNull(int field) | ||
53 | { | ||
54 | int error = MsiInterop.MsiRecordIsNull(this.Handle, field); | ||
55 | |||
56 | switch (error) | ||
57 | { | ||
58 | case 0: | ||
59 | return false; | ||
60 | case 1: | ||
61 | return true; | ||
62 | default: | ||
63 | throw new Win32Exception(error); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | /// <summary> | ||
68 | /// Gets integer value at specified location. | ||
69 | /// </summary> | ||
70 | /// <param name="field">Index into record to get integer</param> | ||
71 | /// <returns>Integer value</returns> | ||
72 | public int GetInteger(int field) | ||
73 | { | ||
74 | return MsiInterop.MsiRecordGetInteger(this.Handle, field); | ||
75 | } | ||
76 | |||
77 | /// <summary> | ||
78 | /// Sets integer value at specified location. | ||
79 | /// </summary> | ||
80 | /// <param name="field">Index into record to set integer.</param> | ||
81 | /// <param name="value">Value to set into record.</param> | ||
82 | public void SetInteger(int field, int value) | ||
83 | { | ||
84 | int error = MsiInterop.MsiRecordSetInteger(this.Handle, field, value); | ||
85 | if (0 != error) | ||
86 | { | ||
87 | throw new Win32Exception(error); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | /// <summary> | ||
92 | /// Gets string value at specified location. | ||
93 | /// </summary> | ||
94 | /// <param name="field">Index into record to get string.</param> | ||
95 | /// <returns>String value</returns> | ||
96 | public string GetString(int field) | ||
97 | { | ||
98 | int bufferSize = 255; | ||
99 | StringBuilder buffer = new StringBuilder(bufferSize); | ||
100 | int error = MsiInterop.MsiRecordGetString(this.Handle, field, buffer, ref bufferSize); | ||
101 | if (234 == error) | ||
102 | { | ||
103 | buffer.EnsureCapacity(++bufferSize); | ||
104 | error = MsiInterop.MsiRecordGetString(this.Handle, field, buffer, ref bufferSize); | ||
105 | } | ||
106 | |||
107 | if (0 != error) | ||
108 | { | ||
109 | throw new Win32Exception(error); | ||
110 | } | ||
111 | |||
112 | return (0 < buffer.Length ? buffer.ToString() : null); | ||
113 | } | ||
114 | |||
115 | /// <summary> | ||
116 | /// Set string value at specified location | ||
117 | /// </summary> | ||
118 | /// <param name="field">Index into record to set string.</param> | ||
119 | /// <param name="value">Value to set into record</param> | ||
120 | public void SetString(int field, string value) | ||
121 | { | ||
122 | int error = MsiInterop.MsiRecordSetString(this.Handle, field, value); | ||
123 | if (0 != error) | ||
124 | { | ||
125 | throw new Win32Exception(error); | ||
126 | } | ||
127 | } | ||
128 | |||
129 | /// <summary> | ||
130 | /// Get stream at specified location. | ||
131 | /// </summary> | ||
132 | /// <param name="field">Index into record to get stream.</param> | ||
133 | /// <param name="buffer">buffer to receive bytes from stream.</param> | ||
134 | /// <param name="requestedBufferSize">Buffer size to read.</param> | ||
135 | /// <returns>Stream read into string.</returns> | ||
136 | public int GetStream(int field, byte[] buffer, int requestedBufferSize) | ||
137 | { | ||
138 | int bufferSize = 255; | ||
139 | if (requestedBufferSize > 0) | ||
140 | { | ||
141 | bufferSize = requestedBufferSize; | ||
142 | } | ||
143 | |||
144 | int error = MsiInterop.MsiRecordReadStream(this.Handle, field, buffer, ref bufferSize); | ||
145 | if (0 != error) | ||
146 | { | ||
147 | throw new Win32Exception(error); | ||
148 | } | ||
149 | |||
150 | return bufferSize; | ||
151 | } | ||
152 | |||
153 | /// <summary> | ||
154 | /// Sets a stream at a specified location. | ||
155 | /// </summary> | ||
156 | /// <param name="field">Index into record to set stream.</param> | ||
157 | /// <param name="path">Path to file to read into stream.</param> | ||
158 | public void SetStream(int field, string path) | ||
159 | { | ||
160 | int error = MsiInterop.MsiRecordSetStream(this.Handle, field, path); | ||
161 | if (0 != error) | ||
162 | { | ||
163 | throw new Win32Exception(error); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /// <summary> | ||
168 | /// Gets the number of fields in record. | ||
169 | /// </summary> | ||
170 | /// <returns>Count of fields in record.</returns> | ||
171 | public int GetFieldCount() | ||
172 | { | ||
173 | int size = MsiInterop.MsiRecordGetFieldCount(this.Handle); | ||
174 | if (0 > size) | ||
175 | { | ||
176 | throw new Win32Exception(); | ||
177 | } | ||
178 | |||
179 | return size; | ||
180 | } | ||
181 | } | ||
182 | } | ||