-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathNtUtils.Packages.Mrm.pas
More file actions
301 lines (243 loc) · 7.59 KB
/
Copy pathNtUtils.Packages.Mrm.pas
File metadata and controls
301 lines (243 loc) · 7.59 KB
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
unit NtUtils.Packages.Mrm;
{
This module provides support for interacting with Package Resource Index (PRI)
files and resolving ms-resource: strings.
}
interface
uses
Ntapi.appmodel.mrm, Ntapi.Versions, Ntapi.ObjBase, NtUtils,
DelphiApi.Reflection;
type
[NamingStyle(nsCamelCase, 'rk')]
TPkgxMrmReferenceKind = (
rkUnknown,
rkFullyQualifiedResource, // @{PackageFullName?ms-resource://ResourcePath}
rkRelativeResource // ms-resource:ResourceName
);
// Determine the location of a merged PRI file
[RequiresCom]
[MinOSVersion(OsWin8)]
function PkgxMrmGetMergedPri(
const MainPriPath: String;
out MergedPriPath: String
): TNtxStatus;
// Get a resource map for a package
[RequiresCom]
[MinOSVersion(OsWin8)]
function PkgxMrmGetPackageResourceMap(
const PackageFullName: String;
out ResourceMap: IResourceMap
): TNtxStatus;
// Get a resource map for a PRI file
[RequiresCom]
[MinOSVersion(OsWin8)]
function PkgxMrmGetFileResourceMap(
const PriFilePath: String;
out ResourceMap: IResourceMap
): TNtxStatus;
// Determine the type of a resource reference
function PkgxMrmResourceReferenceType(
const Reference: String
): TPkgxMrmReferenceKind;
// Resolve a resource reference string
[RequiresCom]
[MinOSVersion(OsWin8)]
function PkgxMrmResolveString(
out Value: String;
const Reference: String;
[opt] const FamilyName: String = ''
): TNtxStatus;
// Resolve a resource reference string in-place
[RequiresCom]
[MinOSVersion(OsWin8)]
function PkgxMrmResolveStringVar(
var Reference: String;
[opt] const FamilyName: String = ''
): TNtxStatus;
implementation
uses
Ntapi.appmodel, Ntapi.ntstatus, Ntapi.WinError, NtUtils.Ldr, NtUtils.Com,
NtUtils.SysUtils, NtUtils.Packages, DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function PkgxMrmResolveFullResourceString(
const Reference: String;
out ResolvedValue: String
): TNtxStatus;
const
INITIAL_SIZE = SizeOf(WideChar) * 200;
var
Buffer: IMemory<PWideChar>;
RequiredLength: NativeUInt;
begin
Result := LdrxCheckDelayedImport(delayed_ResourceManagerQueueGetString);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(INITIAL_SIZE);
Result.Location := 'ResourceManagerQueueGetString';
repeat
RequiredLength := 0;
Result.HResult := ResourceManagerQueueGetString(
PWideChar(Reference), nil, nil, Buffer.Data,
Buffer.Size div SizeOf(WideChar), @RequiredLength);
until not NtxExpandBufferEx(Result, IMemory(Buffer), RequiredLength *
SizeOf(WideChar));
if not Result.IsSuccess then
Exit;
ResolvedValue := RtlxCaptureString(Buffer.Data,
Buffer.Size div SizeOf(WideChar));
end;
function PkgxMrmGetMergedPri;
const
INITIAL_SIZE = SizeOf(WideChar) * 55;
var
Buffer: IMemory<PWideChar>;
begin
Result := LdrxCheckDelayedImport(delayed_GetMergedSystemPri);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(INITIAL_SIZE);
Result.Location := 'GetMergedSystemPri';
repeat
Result.HResult := GetMergedSystemPri(PWideChar(MainPriPath),
Buffer.Size div SizeOf(WideChar), Buffer.Data, nil);
until not NtxExpandBufferGuess(Result, IMemory(Buffer));
if not Result.IsSuccess then
Exit;
MergedPriPath := RtlxCaptureString(Buffer.Data,
Buffer.Size div SizeOf(WideChar));
end;
function PkgxMrmGetPackageResourceMap;
var
ResourceManager: IMrtResourceManager;
begin
Result := ComxCreateInstanceWithFallback(MrmCoreR, CLSID_MrtResourceManager,
IMrtResourceManager, ResourceManager, 'CLSID_MrtResourceManager');
if not Result.IsSuccess then
Exit;
Result.Location := 'IMrtResourceManager::InitializeForPackage';
Result.HResult := ResourceManager.InitializeForPackage(
PWideChar(PackageFullName));
if not Result.IsSuccess then
Exit;
Result.Location := 'IMrtResourceManager::GetMainResourceMap';
Result.HResult := ResourceManager.GetMainResourceMap(IResourceMap,
ResourceMap);
end;
function PkgxMrmGetFileResourceMap;
var
ResourceManager: IMrtResourceManager;
begin
Result := ComxCreateInstanceWithFallback(MrmCoreR, CLSID_MrtResourceManager,
IMrtResourceManager, ResourceManager, 'CLSID_MrtResourceManager');
if not Result.IsSuccess then
Exit;
Result.Location := 'IMrtResourceManager::InitializeForFile';
Result.HResult := ResourceManager.InitializeForFile(
PWideChar(PriFilePath));
if not Result.IsSuccess then
Exit;
Result.Location := 'IMrtResourceManager::GetMainResourceMap';
Result.HResult := ResourceManager.GetMainResourceMap(IResourceMap,
ResourceMap);
end;
function PkgxMrmResolveRelativeResourceString(
const PackageFullName: String;
const Reference: String;
out Value: String
): TNtxStatus;
var
ResourceMap: IResourceMap;
NamedResource: INamedResource;
Candidate: IResourceCandidate;
Buffer: PWideChar;
BufferDeallocator: IAutoReleasable;
begin
Result := PkgxMrmGetPackageResourceMap(PackageFullName, ResourceMap);
if not Result.IsSuccess then
Exit;
// Try the direct string resolution first
Result.Location := 'IResourceMap::GetString';
Result.HResult := ResourceMap.GetString(PWideChar(Reference), Buffer);
// If failed, retry using a method with sligtly different parsing rules
if not Result.IsSuccess then
begin
Result.Location := 'IResourceMap::GetNamedResource';
Result.HResult := ResourceMap.GetNamedResource(PWideChar(Reference),
INamedResource, NamedResource);
if not Result.IsSuccess then
Exit;
Result.Location := 'INamedResource::Resolve';
Result.HResult := NamedResource.Resolve(Candidate);
if not Result.IsSuccess then
Exit;
Result.Location := 'IResourceCandidate::ToString';
Result.HResult := Candidate.ToString(Buffer);
end;
if Result.IsSuccess then
begin
BufferDeallocator := DeferCoTaskMemFree(Buffer);
Value := String(Buffer);
end;
end;
function PkgxMrmResourceReferenceType;
begin
if RtlxPrefixString('@{', Reference, True) and
RtlxSuffixString('}', Reference, True) then
Result := rkFullyQualifiedResource
else if RtlxPrefixString('ms-resource:', Reference) then
Result := rkRelativeResource
else
Result := rkUnknown;
end;
function PkgxMrmResolveString;
var
HeadPackage: TArray<TPkgxPackageNameAndProperties>;
i: Integer;
begin
case PkgxMrmResourceReferenceType(Reference) of
rkFullyQualifiedResource:
Result := PkgxMrmResolveFullResourceString(Reference, Value);
rkRelativeResource:
begin
// Relative resources must reference a package family
if FamilyName = '' then
begin
Result.Location := 'PkgxMrmResolveString';
Result.Status := STATUS_INVALID_PARAMETER;
Exit;
end;
// We need the full package name; find the head package of the family
Result := PkgxEnumeratePackagesInFamilyByNameEx(HeadPackage,
FamilyName, PACKAGE_FILTER_HEAD);
if not Result.IsSuccess then
Exit;
if Length(HeadPackage) <= 0 then
begin
Result.Location := 'PkgxMrmResolveString';
Result.Win32Error := APPMODEL_ERROR_NO_PACKAGE;
Exit;
end;
for i := 0 to High(HeadPackage) do
begin
Result := PkgxMrmResolveRelativeResourceString(HeadPackage[i].FullName,
Reference, Value);
if Result.IsSuccess then
Break;
end;
end
else
Result.Location := 'PkgxMrmResolveString';
Result.Status := STATUS_UNKNOWN_REVISION;
end;
end;
function PkgxMrmResolveStringVar;
var
ResolvedValue: String;
begin
Result := PkgxMrmResolveString(ResolvedValue, Reference, FamilyName);
if Result.IsSuccess then
Reference := ResolvedValue;
end;
end.