-
Notifications
You must be signed in to change notification settings - Fork 44
/
Resources.pas
702 lines (573 loc) · 18.1 KB
/
Resources.pas
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
unit Resources;
////////////////////////////////////////////////////////////////////////////////
//
// Unit : Resources
// Author : rllibby
// Date : 02.07.2008
// Description : Source code for PE resource enumeration and misc handling
//
////////////////////////////////////////////////////////////////////////////////
interface
{ Example usage:
var lpResources: TPEResources;
dwIndex: Integer;
begin
// Testing code
if (CopyFile('c:\windows\system32\calc.exe', 'c:\calc.exe', False)) then
begin
// Create resource object list
lpResources:=TPEResources.Create('c:\calc.exe');
try
// Remove the resource
lpResources.Remove(RT_MANIFEST, MakeIntResource(1));
lpResources.Remove(RT_VERSION, MakeIntResource(1));
lpResources.Remove(RT_GROUP_ICON, 'SC');
for dwIndex:=1 to 6 do
begin
lpResources.Remove(RT_STRING, MakeIntResource(dwIndex));
end;
finally
// Free object
lpResources.Free;
end;
end;
end;
}
////////////////////////////////////////////////////////////////////////////////
// Include units
////////////////////////////////////////////////////////////////////////////////
uses
Windows, SysUtils, Classes;
////////////////////////////////////////////////////////////////////////////////
// Data types
////////////////////////////////////////////////////////////////////////////////
type
TResNameID = packed record
Name: PChar;
ID: PChar;
end;
////////////////////////////////////////////////////////////////////////////////
// Updated list of predefined resource types
////////////////////////////////////////////////////////////////////////////////
const
// Missing predefined types
RT_HTML = MakeIntResource(23);
RT_MANIFEST = MakeIntResource(24);
// Maximum predefined value
RT_MAX_PREDEFINE = 24;
// List of predefined types with associated name
RT_PREDEFINED: Array [0..RT_MAX_PREDEFINE] of TResNameID =
(
(Name: '#0'; ID: MakeIntResource(0)),
(Name: 'Cursor'; ID: RT_CURSOR),
(Name: 'Bitmap'; ID: RT_BITMAP),
(Name: 'Icon'; ID: RT_ICON),
(Name: 'Menu'; ID: RT_MENU),
(Name: 'Dialog'; ID: RT_DIALOG),
(Name: 'String'; ID: RT_STRING),
(Name: 'Fonts'; ID: RT_FONTDIR),
(Name: 'Font'; ID: RT_FONT),
(Name: 'Accelerator'; ID: RT_ACCELERATOR),
(Name: 'Data'; ID: RT_RCDATA),
(Name: 'Message Table'; ID: RT_MESSAGETABLE),
(Name: 'Group Cursor'; ID: RT_GROUP_CURSOR),
(Name: '#13'; ID: MakeIntResource(13)),
(Name: 'Group Icon'; ID: RT_GROUP_ICON),
(Name: '#15'; ID: MakeIntResource(15)),
(Name: 'Version'; ID: RT_VERSION),
(Name: 'Dialog'; ID: RT_DLGINCLUDE),
(Name: '#18'; ID: MakeIntResource(18)),
(Name: 'Plug And Play'; ID: RT_PLUGPLAY),
(Name: 'VXD'; ID: RT_VXD),
(Name: 'Animated Cursor'; ID: RT_ANICURSOR),
(Name: 'Animated Icon'; ID: RT_ANIICON),
(Name: 'Html'; ID: RT_HTML),
(Name: 'Manifest'; ID: RT_MANIFEST)
);
////////////////////////////////////////////////////////////////////////////////
// PE Resource objects
////////////////////////////////////////////////////////////////////////////////
type
// Resource object
TPEResource = class(TObject)
private
// Private declarations
FName: PChar;
FType: PChar;
FLanguages: TList;
protected
// Protected declarations
procedure Load(Module: HMODULE);
function GetLanguages(Index: Integer): Word;
function GetLanguageCount: Integer;
public
// Public declarations
constructor Create(AModule: HMODULE; AName, AType: PChar);
destructor Destroy; override;
property Languages[Index: Integer]: Word read GetLanguages;
property LanguageCount: Integer read GetLanguageCount;
property ResName: PChar read FName;
property ResType: PChar read FType;
end;
// Resource group object
TPEResourceGroup = class(TObject)
private
// Private declarations
FType: PChar;
FResources: TList;
protected
// Protected declarations
procedure Clear;
procedure Load(Module: HMODULE);
procedure Remove(Obj: TObject);
function GetResources(Index: Integer): TPEResource;
function GetResourceCount: Integer;
function GetResTypeName: String;
public
// Public declarations
constructor Create(AModule: HMODULE; AType: PChar);
destructor Destroy; override;
function Find(ResourceName: PChar): TPEResource;
property ResType: PChar read FType;
property ResTypeName: String read GetResTypeName;
property Resources[Index: Integer]: TPEResource read GetResources;
property ResourceCount: Integer read GetResourceCount;
end;
// Resource container
TPEResources = class(TObject)
private
// Private declarations
FLibFileName: String;
FGroups: TList;
protected
// Protected declarations
procedure Clear;
procedure Load(LibFileName: String);
function GetGroupCount: Integer;
function GetGroups(Index: Integer): TPEResourceGroup;
public
// Public declarations
constructor Create(LibFileName: String);
destructor Destroy; override;
function Find(ResourceType, ResourceName: PChar): TPEResource;
function GroupByType(ResourceType: PChar): TPEResourceGroup;
procedure Remove(ResourceType, ResourceName: PChar);
property Groups[Index: Integer]: TPEResourceGroup read GetGroups;
property GroupCount: Integer read GetGroupCount;
end;
////////////////////////////////////////////////////////////////////////////////
// Utility functions
////////////////////////////////////////////////////////////////////////////////
function GetResourceTypeName(ResourceType: PChar): String;
function GetResourceName(ResourceName: PChar): String;
function IsIntResource(ResourceTypeOrName: PChar): Boolean;
procedure KillResource(ExeFile: String; ResType, ResName: PChar; LangID: Word = 0);
function ResAlloc(ResourceTypeOrName: PChar): PChar;
procedure ResFree(ResourceTypeOrName: PChar);
function ResCompare(ResourceTypeOrName1, ResourceTypeOrName2: PChar): Boolean;
implementation
//// Callback functions ////////////////////////////////////////////////////////
function EnumResLangProc(Module: HMODULE; lpszType, lpszName: PChar; wIDLanguage: Word; lParam: Pointer): BOOL; stdcall;
begin
// Resource protection
try
// Cast lParam as resource object and add language
TPEResource(lParam).FLanguages.Add(Pointer(wIDLanguage));
finally
// Keep enumerating
result:=True;
end;
end;
function EnumResNameProc(Module: HMODULE; lpszType, lpszName: PChar; lParam: Pointer): BOOL; stdcall;
begin
// Resource protection
try
// Create new resource and add to list
TPEResourceGroup(lParam).FResources.Add(TPEResource.Create(Module, lpszName, lpszType));
finally
// Keep enumerating
result:=True;
end;
end;
function EnumResTypeProc(Module: HMODULE; lpszType: PChar; lParam: Pointer): BOOL; stdcall;
begin
// Resource protection
try
// Create new resource group and add to list
TPEResources(lParam).FGroups.Add(TPEResourceGroup.Create(Module, lpszType));
finally
// Keep enumerating
result:=True;
end;
end;
//// TPEResource ///////////////////////////////////////////////////////////////
procedure TPEResource.Load(Module: HMODULE);
begin
// Enumerate the languages
EnumResourceLanguages(Module, FType, FName, @EnumResLangProc, Integer(Self));
end;
function TPEResource.GetLanguageCount: Integer;
begin
// Return count of languages
result:=FLanguages.Count;
end;
function TPEResource.GetLanguages(Index: Integer): Word;
begin
// Return the langauge at the specified index
result:=Word(FLanguages[Index]);
end;
constructor TPEResource.Create(AModule: HMODULE; AName, AType: PChar);
begin
// Perform inherited
inherited Create;
// Set defaults
FLanguages:=TList.Create;
FName:=ResAlloc(AName);
FType:=ResAlloc(AType);
// Load
Load(AModule);
end;
destructor TPEResource.Destroy;
begin
// Resource protection
try
// Free the list
FLanguages.Free;
// Free allocated memory
ResFree(FName);
ResFree(FType);
finally
// Perform inherited
inherited Destroy;
end;
end;
//// TPEResourceGroup //////////////////////////////////////////////////////////
function TPEResourceGroup.Find(ResourceName: PChar): TPEResource;
var lpResource: TPEResource;
dwIndex: Integer;
begin
// Set default result
result:=nil;
// Find the desired resource
for dwIndex:=0 to Pred(FResources.Count) do
begin
// Access the resource
lpResource:=GetResources(dwIndex);
// Check the resource name
if ResCompare(ResourceName, lpResource.ResName) then
begin
// Match was found, return resource
result:=lpResource;
// Done processing
break;
end;
end;
end;
procedure TPEResourceGroup.Remove(Obj: TObject);
begin
// Check assignment
if Assigned(Obj) and (FResources.IndexOf(Obj) >= 0) then
begin
// Resource protection
try
// Remove from list
FResources.Remove(Obj);
finally
// Free the object
Obj.Free;
end;
end;
end;
procedure TPEResourceGroup.Load(Module: HMODULE);
begin
// Enumerate the resources for this type
EnumResourceNames(Module, FType, @EnumResNameProc, LongInt(Self));
end;
procedure TPEResourceGroup.Clear;
var dwIndex: Integer;
begin
// Resource protection
try
// Walk the resource list
for dwIndex:=0 to Pred(FResources.Count) do
begin
// Free the resource
TPEResource(FResources[dwIndex]).Free;
end;
finally
// Clear the list
FResources.Clear;
end;
end;
function TPEResourceGroup.GetResources(Index: Integer): TPEResource;
begin
// Return the resource at the specified index
result:=TPEResource(FResources[Index]);
end;
function TPEResourceGroup.GetResourceCount: Integer;
begin
// Return the count of resources
result:=FResources.Count;
end;
function TPEResourceGroup.GetResTypeName: String;
begin
// Return the stringifed name
result:=GetResourceTypeName(FType);
end;
constructor TPEResourceGroup.Create(AModule: HMODULE; AType: PChar);
begin
// Perform inherited
inherited Create;
// Set defaults
FResources:=TList.Create;
FType:=ResAlloc(AType);
// Load
Load(AModule);
end;
destructor TPEResourceGroup.Destroy;
begin
// Resource protection
try
// Clear
Clear;
// Free the list
FResources.Free;
// Free allocated memory
ResFree(FType);
finally
// Perform inherited
inherited Destroy;
end;
end;
//// TPEResources //////////////////////////////////////////////////////////////
procedure TPEResources.Remove(ResourceType, ResourceName: PChar);
var lpGroup: TPEResourceGroup;
lpResource: TPEResource;
dwIndex: Integer;
begin
// Try to get the group
lpGroup:=GroupByType(ResourceType);
// Check result
if Assigned(lpGroup) then
begin
// Have the group lookup the resource name
lpResource:=lpGroup.Find(ResourceName);
// Check result
if Assigned(lpResource) then
begin
// Resource protection
try
// Remove all resources using the language values
if (lpResource.LanguageCount = 0) then
// No language, kill resource
KillResource(FLibFileName, lpResource.ResType, lpResource.ResName, 0)
else
begin
// Walk the languages
for dwIndex:=0 to Pred(lpResource.LanguageCount) do
begin
// Kill the resource based on language id
KillResource(FLibFileName, lpResource.ResType, lpResource.ResName, lpResource.Languages[dwIndex]);
end;
end;
finally
// Have the group free the resource object
lpGroup.Remove(lpResource);
end;
end;
end;
end;
function TPEResources.GroupByType(ResourceType: PChar): TPEResourceGroup;
var lpGroup: TPEResourceGroup;
dwIndex: Integer;
begin
// Set default result
result:=nil;
// Find the desired group
for dwIndex:=0 to Pred(FGroups.Count) do
begin
// Access the group
lpGroup:=GetGroups(dwIndex);
// Check the group
if ResCompare(lpGroup.ResType, ResourceType) then
begin
// Set group result
result:=lpGroup;
// Done processing
break;
end;
end;
end;
function TPEResources.Find(ResourceType, ResourceName: PChar): TPEResource;
var lpGroup: TPEResourceGroup;
dwIndex: Integer;
begin
// Try to get the group
lpGroup:=GroupByType(ResourceType);
// Check result
if Assigned(lpGroup) then
// Have the group lookup the resource name
result:=lpGroup.Find(ResourceName)
else
// Set default result
result:=nil;
end;
procedure TPEResources.Clear;
var dwIndex: Integer;
begin
// Resource protection
try
// Walk the groups list
for dwIndex:=0 to Pred(FGroups.Count) do
begin
// Free the group
TPEResourceGroup(FGroups[dwIndex]).Free;
end;
finally
// Clear the list
FGroups.Clear;
end;
end;
procedure TPEResources.Load(LibFileName: String);
var hMod: HMODULE;
begin
// Attempt to load the library
hMod:=LoadLibraryEx(PChar(LibFileName), 0, LOAD_LIBRARY_AS_DATAFILE);
// Check module handle
if (hMod = 0) then
// Failed to load
RaiseLastWin32Error
else
begin
// Resource protection
try
// Enumerate the resource types
EnumResourceTypes(hMod, @EnumResTypeProc, LongInt(Self));
finally
// Free the library
FreeLibrary(hMod);
end;
end;
end;
function TPEResources.GetGroupCount: Integer;
begin
// Return the count of groups
result:=FGroups.Count;
end;
function TPEResources.GetGroups(Index: Integer): TPEResourceGroup;
begin
// Return the group at the specified index
result:=TPEResourceGroup(FGroups[Index]);
end;
constructor TPEResources.Create(LibFileName: String);
begin
// Perform inherited
inherited Create;
// Set defaults
FGroups:=TList.Create;
FLibFileName:=LibFileName;
// Load the library / exe file
Load(LibFileName);
end;
destructor TPEResources.Destroy;
begin
// Resource protection
try
// Clear
Clear;
// Free the list
FGroups.Free;
finally
// Perform inherited
inherited Destroy;
end;
end;
//// Utility functions /////////////////////////////////////////////////////////
function ResCompare(ResourceTypeOrName1, ResourceTypeOrName2: PChar): Boolean;
begin
// Check the name for integer value
if IsIntResource(ResourceTypeOrName1) then
// Can only match if the second value is an integer as well
result:=(IsIntResource(ResourceTypeOrName2) and (ResourceTypeOrName1 = ResourceTypeOrName2))
// Check second value
else if IsIntResource(ResourceTypeOrName2) then
// Can only match if the first value is an integer as well
result:=(IsIntResource(ResourceTypeOrName1) and (ResourceTypeOrName1 = ResourceTypeOrName2))
else
// Case insensitive compare
result:=(StrIComp(ResourceTypeOrName1, ResourceTypeOrName2) = 0);
end;
function GetResourceName(ResourceName: PChar): String;
begin
// Check the resource type
if IsIntResource(ResourceName) then
// Format as #NUMBER
result:=Format('#%d', [Word(Pointer(ResourceName))])
else
// Return type name
result:=ResourceName;
end;
function ResAlloc(ResourceTypeOrName: PChar): PChar;
begin
// Check for int resource name
if IsIntResource(ResourceTypeOrName) then
// Return as is
result:=ResourceTypeOrName
else
// Allocate and copy
result:=StrCopy(AllocMem(Succ(StrLen(ResourceTypeOrName))), ResourceTypeOrName);
end;
procedure ResFree(ResourceTypeOrName: PChar);
begin
// Check for int resource name
if not(IsIntResource(ResourceTypeOrName)) then
begin
// Free allocated memory
FreeMem(ResourceTypeOrName);
end;
end;
function GetResourceTypeName(ResourceType: PChar): String;
var wType: Word;
begin
// Check the resource type
if IsIntResource(ResourceType) then
begin
// Get actual word value
wType:=Word(Pointer(ResourceType));
// Determine if predefined
if (wType <= RT_MAX_PREDEFINE) then
// Get name from static table
result:=RT_PREDEFINED[wType].Name
else
// Format as #NUMBER
result:=Format('#%d', [wType]);
end
else
// Return type name
result:=ResourceType;
end;
function IsIntResource(ResourceTypeOrName: PChar): Boolean;
begin
// Determine if the resource type / name is actually a word value
result:=((LongWord(Pointer(ResourceTypeOrName)) shr 16) = 0);
end;
procedure KillResource(ExeFile: String; ResType, ResName: PChar; LangID: Word = 0);
var hUpdate: THandle;
begin
// Open file for resource updating
hUpdate:=BeginUpdateResource(PChar(ExeFile), False);
// Check handle
if (hUpdate = 0) then
// Raise error
RaiseLastWin32Error
// Remove resource
else if UpdateResource(hUpdate, ResType, ResName, LangID, nil, 0) then
begin
// End the update
if not(EndUpdateResource(hUpdate, False)) then RaiseLastWin32Error;
end
else
// Raise error
RaiseLastWin32Error
end;
end.