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
|
// 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.Resources
{
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Allows reading and editing of resource data in a Win32 PE file.
/// </summary>
/// <remarks>
/// To use this class:<list type="number">
/// <item>Create a new ResourceCollection</item>
/// <item>Locate resources for the collection by calling one of the <see cref="ResourceCollection.Find(string)"/> methods</item>
/// <item>Load data of one or more <see cref="Resource"/>s from a file by calling the <see cref="Load"/> method of the
/// Resource class, or load them all at once (more efficient) with the <see cref="Load"/> method of the ResourceCollection.</item>
/// <item>Read and/or edit data of the individual Resource objects using the methods on that class.</item>
/// <item>Save data of one or more <see cref="Resource"/>s to a file by calling the <see cref="Save"/> method of the
/// Resource class, or save them all at once (more efficient) with the <see cref="Save"/> method of the ResourceCollection.</item>
/// </list>
/// </remarks>
public class ResourceCollection : ICollection<Resource>
{
private List<Resource> resources;
/// <summary>
/// Creates a new, empty ResourceCollection.
/// </summary>
public ResourceCollection()
{
this.resources = new List<Resource>();
}
/// <summary>
/// Locates all resources in a file, including all resource types and languages. For each located resource,
/// a <see cref="Resource"/> instance (or subclass) is added to the collection.
/// </summary>
/// <param name="resFile">The file to be searched for resources.</param>
/// <exception cref="IOException">resources could not be read from the file</exception>
public void Find(string resFile)
{
this.Clear();
IntPtr module = NativeMethods.LoadLibraryEx(resFile, IntPtr.Zero, NativeMethods.LOAD_LIBRARY_AS_DATAFILE);
if (module == IntPtr.Zero)
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "Failed to load resource file. Error code: {0}", err));
}
try
{
if (!NativeMethods.EnumResourceTypes(module, new NativeMethods.EnumResTypesProc(this.EnumResTypes), IntPtr.Zero))
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "Failed to enumerate resources. Error code: {0}", err));
}
}
finally
{
NativeMethods.FreeLibrary(module);
}
}
/// <summary>
/// Locates all resources in a file of a given type, including all languages. For each located resource,
/// a <see cref="Resource"/> instance (or subclass) is added to the collection.
/// </summary>
/// <param name="resFile">The file to be searched for resources.</param>
/// <param name="type">The type of resource to search for; may be one of the ResourceType constants or a user-defined type.</param>
/// <exception cref="IOException">resources could not be read from the file</exception>
public void Find(string resFile, ResourceType type)
{
this.Clear();
IntPtr module = NativeMethods.LoadLibraryEx(resFile, IntPtr.Zero, NativeMethods.LOAD_LIBRARY_AS_DATAFILE);
try
{
if (!NativeMethods.EnumResourceNames(module, (string) type, new NativeMethods.EnumResNamesProc(this.EnumResNames), IntPtr.Zero))
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "EnumResourceNames error. Error code: {0}", err));
}
}
finally
{
NativeMethods.FreeLibrary(module);
}
}
/// <summary>
/// Locates all resources in a file of a given type and language. For each located resource,
/// a <see cref="Resource"/> instance (or subclass) is added to the collection.
/// </summary>
/// <param name="resFile">The file to be searched for resources.</param>
/// <param name="type">The type of resource to search for; may be one of the ResourceType constants or a user-defined type.</param>
/// <param name="name">The name of the resource to search for.</param>
/// <exception cref="IOException">resources could not be read from the file</exception>
public void Find(string resFile, ResourceType type, string name)
{
this.Clear();
IntPtr module = NativeMethods.LoadLibraryEx(resFile, IntPtr.Zero, NativeMethods.LOAD_LIBRARY_AS_DATAFILE);
try
{
if (!NativeMethods.EnumResourceLanguages(module, (string) type, name, new NativeMethods.EnumResLangsProc(this.EnumResLangs), IntPtr.Zero))
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "EnumResourceLanguages error. Error code: {0}", err));
}
}
finally
{
NativeMethods.FreeLibrary(module);
}
}
private bool EnumResTypes(IntPtr module, IntPtr type, IntPtr param)
{
if (!NativeMethods.EnumResourceNames(module, type, new NativeMethods.EnumResNamesProc(EnumResNames), IntPtr.Zero))
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "EnumResourceNames error! Error code: {0}", err));
}
return true;
}
private bool EnumResNames(IntPtr module, IntPtr type, IntPtr name, IntPtr param)
{
if (!NativeMethods.EnumResourceLanguages(module, type, name, new NativeMethods.EnumResLangsProc(EnumResLangs), IntPtr.Zero))
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "EnumResourceLanguages error. Error code: {0}", err));
}
return true;
}
private bool EnumResLangs(IntPtr module, IntPtr type, IntPtr name, ushort langId, IntPtr param)
{
Resource res;
if (((int) type) == ResourceType.Version.IntegerValue)
{
res = new VersionResource(ResourceNameToString(name), langId);
}
else
{
res = new Resource(ResourceNameToString(type), ResourceNameToString(name), langId);
}
if (!this.Contains(res))
{
this.Add(res);
}
return true;
}
private static string ResourceNameToString(IntPtr resName)
{
if ((resName.ToInt64() >> 16) == 0)
{
return "#" + resName.ToString();
}
else
{
return Marshal.PtrToStringAuto(resName);
}
}
/// <summary>
/// For all resources in the collection, loads their data from a resource file.
/// </summary>
/// <param name="file">The file from which resources are loaded.</param>
public void Load(string file)
{
IntPtr module = NativeMethods.LoadLibraryEx(file, IntPtr.Zero, NativeMethods.LOAD_LIBRARY_AS_DATAFILE);
try
{
foreach (Resource res in this)
{
res.Load(module);
}
}
finally
{
NativeMethods.FreeLibrary(module);
}
}
/// <summary>
/// For all resources in the collection, saves their data to a resource file.
/// </summary>
/// <param name="file">The file to which resources are saved.</param>
public void Save(string file)
{
IntPtr updateHandle = IntPtr.Zero;
try
{
updateHandle = NativeMethods.BeginUpdateResource(file, false);
foreach (Resource res in this)
{
res.Save(updateHandle);
}
if (!NativeMethods.EndUpdateResource(updateHandle, false))
{
int err = Marshal.GetLastWin32Error();
throw new IOException(String.Format(CultureInfo.InvariantCulture, "Failed to save resource. Error {0}", err));
}
updateHandle = IntPtr.Zero;
}
finally
{
if (updateHandle != IntPtr.Zero)
{
NativeMethods.EndUpdateResource(updateHandle, true);
}
}
}
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
public Resource this[int index]
{
get
{
return (Resource) this.resources[index];
}
set
{
this.resources[index] = value;
}
}
/// <summary>
/// Adds a new item to the collection.
/// </summary>
/// <param name="item">The Resource to add.</param>
public void Add(Resource item)
{
this.resources.Add(item);
}
/// <summary>
/// Removes an item to the collection.
/// </summary>
/// <param name="item">The Resource to remove.</param>
public bool Remove(Resource item)
{
return this.resources.Remove(item);
}
/// <summary>
/// Gets the index of an item in the collection.
/// </summary>
/// <param name="item">The Resource to search for.</param>
/// <returns>The index of the item, or -1 if not found.</returns>
public int IndexOf(Resource item)
{
return this.resources.IndexOf(item);
}
/// <summary>
/// Inserts a item into the collection.
/// </summary>
/// <param name="index">The insertion index.</param>
/// <param name="item">The Resource to insert.</param>
public void Insert(int index, Resource item)
{
this.resources.Insert(index, item);
}
/// <summary>
/// Tests if the collection contains an item.
/// </summary>
/// <param name="item">The Resource to search for.</param>
/// <returns>true if the item is found; false otherwise</returns>
public bool Contains(Resource item)
{
return this.resources.Contains(item);
}
/// <summary>
/// Copies the collection into an array.
/// </summary>
/// <param name="array">The array to copy into.</param>
/// <param name="arrayIndex">The starting index in the destination array.</param>
public void CopyTo(Resource[] array, int arrayIndex)
{
this.resources.CopyTo(array, arrayIndex);
}
/// <summary>
/// Removes all resources from the collection.
/// </summary>
public void Clear()
{
this.resources.Clear();
}
/// <summary>
/// Gets the number of resources in the collection.
/// </summary>
public int Count
{
get
{
return this.resources.Count;
}
}
/// <summary>
/// Gets an enumerator over all resources in the collection.
/// </summary>
/// <returns></returns>
public IEnumerator<Resource> GetEnumerator()
{
return this.resources.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) this.resources).GetEnumerator();
}
bool ICollection<Resource>.IsReadOnly
{
get
{
return false;
}
}
}
}
|