-
Notifications
You must be signed in to change notification settings - Fork 33
/
NtUtils.Packages.SRCache.pas
667 lines (555 loc) · 16.5 KB
/
NtUtils.Packages.SRCache.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
unit NtUtils.Packages.SRCache;
{
This module provides functions for retrieving information about packaged
applications from State Repository Cache.
}
interface
uses
Ntapi.appmodel, NtUtils;
type
TSRCachePackageFamilyId = type Cardinal;
TSRCachePackageId = type Cardinal;
TSRCacheApplicationId = type Cardinal;
{ Package Families }
// Enumerate package family names
function PkgxSRCacheIteratePackageFamilyNames(
[out, opt] Status: PNtxStatus
): IEnumerable<String>;
// Enumerate package family IDs
function PkgxSRCacheIteratePackageFamilyIds(
[out, opt] Status: PNtxStatus
): IEnumerable<TSRCachePackageFamilyId>;
// Lookup package family identifier
function PkgxSRCacheLookupPackageFamilyId(
const PackageFamilyName: String;
out PackageFamilyId: TSRCachePackageFamilyId
): TNtxStatus;
// Open package family data key by ID
function PkgxSRCacheOpenPackageFamiliy(
PackageFamilyId: TSRCachePackageFamilyId;
out hxPackageFamilyKey: IHandle
): TNtxStatus;
// Open package family data key by name
function PkgxSRCacheOpenPackageFamiliyByName(
const PackageFamilyName: String;
out hxPackageFamilyKey: IHandle
): TNtxStatus;
// Query package family name from data key
function PkgxSRCacheQueryPackageFamilyName(
const hxPackageFamilyKey: IHandle;
out PackageFamilyName: String
): TNtxStatus;
// Query package family publisher from data key
function PkgxSRCacheQueryPackageFamilyPublisher(
const hxPackageFamilyKey: IHandle;
out Publisher: String
): TNtxStatus;
{ Packages }
// Enumerate package IDs beloning to a package family by ID
function PkgxSRCacheIteratePackageIDsInFamily(
[out, opt] Status: PNtxStatus;
PackageFamilyId: TSRCachePackageFamilyId
): IEnumerable<TSRCachePackageId>;
// Enumerate package IDs
function PkgxSRCacheIteratePackageIDs(
[out, opt] Status: PNtxStatus
): IEnumerable<TSRCachePackageId>;
// Lookup package identifier
function PkgxSRCacheLookupPackageId(
const PackageFullName: String;
out PackageId: TSRCachePackageId
): TNtxStatus;
// Open package data key by ID
function PkgxSRCacheOpenPackage(
PackageId: TSRCachePackageId;
out hxPackageKey: IHandle
): TNtxStatus;
// Open package data key by name
function PkgxSRCacheOpenPackageByName(
const PackageFullName: String;
out hxPackageKey: IHandle
): TNtxStatus;
// Query package name from data key
function PkgxSRCacheQueryPackageName(
const hxPackageKey: IHandle;
out PackageFullName: String
): TNtxStatus;
// Query package family ID from data key
function PkgxSRCacheQueryPackageFamilyId(
const hxPackageKey: IHandle;
out PackageFamilyId: TSRCachePackageFamilyId
): TNtxStatus;
// Query package flags from data key
function PkgxSRCacheQueryPackageFlags(
const hxPackageKey: IHandle;
out Flags: TStateRepositoryPackageFlags
): TNtxStatus;
// Query package flags v2 from data key
function PkgxSRCacheQueryPackageFlags2(
const hxPackageKey: IHandle;
out Flags2: TStateRepositoryPackageFlags2
): TNtxStatus;
// Query package type from data key
function PkgxSRCacheQueryPackageType(
const hxPackageKey: IHandle;
out PackageType: TStateRepositoryPackageType
): TNtxStatus;
// Query package installed location from data key
function PkgxSRCacheQueryPackageLocation(
const hxPackageKey: IHandle;
out InstalledLocation: String
): TNtxStatus;
{ Applications }
// Enumerate applications in a package
function PkgxSRCacheIterateApplicationIDsInPackage(
[out, opt] Status: PNtxStatus;
PackageId: TSRCachePackageId
): IEnumerable<TSRCacheApplicationId>;
// Enumerate applications in all packages
function PkgxSRCacheIterateApplicationIDs(
[out, opt] Status: PNtxStatus
): IEnumerable<TSRCacheApplicationId>;
// Lookup application identifier
function PkgxSRCacheLookupApplicationId(
out ApplicationId: TSRCacheApplicationId;
PackageId: TSRCachePackageId;
const RelativeName: String
): TNtxStatus;
// Open application data key by ID
function PkgxSRCacheOpenApplication(
ApplicationId: TSRCacheApplicationId;
out hxApplicationKey: IHandle
): TNtxStatus;
// Query full application name from data key
function PkgxSRCacheQueryApplicationAumid(
const hxApplicationKey: IHandle;
out Aumid: String
): TNtxStatus;
// Query relative application name from data key
function PkgxSRCacheQueryApplicationPraid(
const hxApplicationKey: IHandle;
out RelativeName: String
): TNtxStatus;
// Query full application name from data key
function PkgxSRCacheQueryApplicationPackageID(
const hxApplicationKey: IHandle;
out PackageId: TSRCachePackageId
): TNtxStatus;
// Query full application flags from data key
function PkgxSRCacheQueryApplicationFlags(
const hxApplicationKey: IHandle;
out Flags: TStateRepositoryApplicationFlags
): TNtxStatus;
// Query full application entrypoint from data key
function PkgxSRCacheQueryApplicationEntrypoint(
const hxApplicationKey: IHandle;
out Entrypoint: String
): TNtxStatus;
// Query full application entrypoint from data key
function PkgxSRCacheQueryApplicationExecutable(
const hxApplicationKey: IHandle;
out Executable: String
): TNtxStatus;
// Query full application entrypoint from data key
function PkgxSRCacheQueryApplicationStartPage(
const hxApplicationKey: IHandle;
out StartPage: String
): TNtxStatus;
implementation
uses
Ntapi.ntregapi, Ntapi.WinError, NtUtils.Registry, NtUtils.Packages,
NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
const
SR_CACHE_ROOT = REG_PATH_MACHINE + '\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModel\StateRepository\Cache';
SR_CACHE_PACKAGE_FAMILY_INDEX = SR_CACHE_ROOT + '\PackageFamily\Index\PackageFamilyName';
SR_CACHE_PACKAGE_FAMILY_DATA = SR_CACHE_ROOT + '\PackageFamily\Data';
SR_CACHE_PACKAGE_INDEX = SR_CACHE_ROOT + '\Package\Index\PackageFullName';
SR_CACHE_PACKAGE_INDEX_FAMILY = SR_CACHE_ROOT + '\Package\Index\PackageFamily';
SR_CACHE_PACKAGE_DATA = SR_CACHE_ROOT + '\Package\Data';
SR_CACHE_APPLICATION_INDEX = SR_CACHE_ROOT + '\Application\Index\Package';
SR_CACHE_APPLICATION_INDEX_PRAID = SR_CACHE_ROOT + '\Application\Index\PackageAndPackageRelativeApplicationId';
SR_CACHE_APPLICATION_DATA = SR_CACHE_ROOT + '\Application\Data';
{ Common }
function PkgxUIntToStr(Id: Cardinal): String;
begin
Result := RtlxUIntToStr(Id, nsHexadecimal, 0, []);
end;
function PkgxStrToUInt(const S: String; out Id: Cardinal): Boolean;
begin
Result := RtlxStrToUInt(S, Id, nsHexadecimal, []);
end;
{ Package Families }
function PkgxSRCacheIteratePackageFamilyNames;
var
hxIndexKey: IHandle;
Index: Integer;
begin
Index := 0;
Result := NtxAuto.IterateEx<String>(
Status,
function : TNtxStatus
begin
// Open the package families index
Result := NtxOpenKey(hxIndexKey, SR_CACHE_PACKAGE_FAMILY_INDEX,
KEY_ENUMERATE_SUB_KEYS);
end,
function (out Current: String): TNtxStatus
var
KeyInfo: TNtxRegKey;
begin
// Retrieve a sub-key
Result := NtxEnumerateKey(hxIndexKey, Index, KeyInfo);
if not Result.IsSuccess then
Exit;
// Parse the name into an ID
if not PkgxIsValidFamilyName(KeyInfo.Name) then
begin
Result.Location := 'PkgxSRCacheIteratePackageFamilyNames';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
Exit;
end
else
Current := KeyInfo.Name;
// Advance to the next
Inc(Index);
end
);
end;
function PkgxSRCacheIteratePackageFamilyIds;
var
hxDataKey: IHandle;
Index: Integer;
begin
Index := 0;
Result := NtxAuto.IterateEx<TSRCachePackageFamilyId>(
Status,
function : TNtxStatus
begin
// Open the package families index
Result := NtxOpenKey(hxDataKey, SR_CACHE_PACKAGE_FAMILY_DATA,
KEY_ENUMERATE_SUB_KEYS);
end,
function (out Current: TSRCachePackageFamilyId): TNtxStatus
var
KeyInfo: TNtxRegKey;
begin
// Retrieve a sub-key
Result := NtxEnumerateKey(hxDataKey, Index, KeyInfo);
if not Result.IsSuccess then
Exit;
// Parse the name into an ID
if not PkgxStrToUInt(KeyInfo.Name, Cardinal(Current)) then
begin
Result.Location := 'PkgxSRCacheIteratePackageFamilyIds';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
Exit;
end;
// Advance to the next
Inc(Index);
end
);
end;
function PkgxSRCacheLookupPackageFamilyId;
var
IndexInfo: TNtxRegKey;
hxIndexKey: IHandle;
begin
// Open the index key
Result := NtxOpenKey(
hxIndexKey,
RtlxCombinePaths(SR_CACHE_PACKAGE_FAMILY_INDEX, PackageFamilyName),
KEY_ENUMERATE_SUB_KEYS
);
if not Result.IsSuccess then
Exit;
// Query the first sub-key
Result := NtxEnumerateKey(hxIndexKey, 0, IndexInfo);
if not Result.IsSuccess then
Exit;
// Convert the sub-key name to an ID
if not PkgxStrToUInt(IndexInfo.Name, Cardinal(PackageFamilyId)) then
begin
Result.Location := 'PkgxSRCacheLookupPackageFamilyId';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
end;
end;
function PkgxSRCacheOpenPackageFamiliy;
begin
Result := NtxOpenKey(
hxPackageFamilyKey,
RtlxCombinePaths(SR_CACHE_PACKAGE_FAMILY_DATA,
PkgxUIntToStr(PackageFamilyId)),
KEY_QUERY_VALUE
);
end;
function PkgxSRCacheOpenPackageFamiliyByName;
var
FamilyId: TSRCachePackageFamilyId;
begin
// Convert the name into ID
Result := PkgxSRCacheLookupPackageFamilyId(PackageFamilyName, FamilyId);
if not Result.IsSuccess then
Exit;
// Open by ID
Result := PkgxSRCacheOpenPackageFamiliy(FamilyId, hxPackageFamilyKey);
end;
function PkgxSRCacheQueryPackageFamilyName;
begin
Result := NtxQueryValueKeyString(hxPackageFamilyKey, 'PackageFamilyName',
PackageFamilyName);
end;
function PkgxSRCacheQueryPackageFamilyPublisher;
begin
Result := NtxQueryValueKeyString(hxPackageFamilyKey, 'Publisher', Publisher);
end;
{ Packages }
function PkgxSRCacheIteratePackageIDsInFamily;
var
hxKey: IHandle;
Index: Integer;
begin
hxKey := nil;
Index := 0;
Result := NtxAuto.IterateEx<TSRCachePackageId>(
Status,
function : TNtxStatus
begin
// Open the package full name list in the state repository cache
Result := NtxOpenKey(
hxKey,
RtlxCombinePaths(SR_CACHE_PACKAGE_INDEX_FAMILY,
PkgxUIntToStr(PackageFamilyId)),
KEY_ENUMERATE_SUB_KEYS
);
end,
function (out Current: TSRCachePackageId): TNtxStatus
var
KeyInfo: TNtxRegKey;
begin
// Retrieve a sub-key
Result := NtxEnumerateKey(hxKey, Index, KeyInfo);
if not Result.IsSuccess then
Exit;
// Parse the name into an ID
if not PkgxStrToUInt(KeyInfo.Name, Cardinal(Current)) then
begin
Result.Location := 'PkgxSRCacheIteratePackageIDsInFamily';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
Exit;
end;
// Advance to the next
Inc(Index);
end
);
end;
function PkgxSRCacheIteratePackageIDs;
begin
end;
function PkgxSRCacheLookupPackageId;
var
IndexInfo: TNtxRegKey;
hxIndexKey: IHandle;
begin
// Open the index key
Result := NtxOpenKey(
hxIndexKey,
RtlxCombinePaths(SR_CACHE_PACKAGE_INDEX, PackageFullName),
KEY_ENUMERATE_SUB_KEYS
);
if not Result.IsSuccess then
Exit;
// Query the first sub-key
Result := NtxEnumerateKey(hxIndexKey, 0, IndexInfo);
if not Result.IsSuccess then
Exit;
// Convert the sub-key name to an ID
if not RtlxStrToUInt(IndexInfo.Name, Cardinal(PackageId),
nsHexadecimal, []) then
begin
Result.Location := 'PkgxSRCacheLookupPackageId';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
end;
end;
function PkgxSRCacheOpenPackage;
begin
Result := NtxOpenKey(
hxPackageKey,
RtlxCombinePaths(SR_CACHE_PACKAGE_DATA, PkgxUIntToStr(PackageId)),
KEY_QUERY_VALUE
);
end;
function PkgxSRCacheOpenPackageByName;
var
PackageId: TSRCachePackageId;
begin
// Convert the name to ID
Result := PkgxSRCacheLookupPackageId(PackageFullName, PackageId);
if not Result.IsSuccess then
Exit;
// Open by ID
Result := PkgxSRCacheOpenPackage(PackageId, hxPackageKey);
end;
function PkgxSRCacheQueryPackageName;
begin
Result := NtxQueryValueKeyString(hxPackageKey, 'PackageFullName',
PackageFullName);
end;
function PkgxSRCacheQueryPackageFamilyId;
begin
Result := NtxQueryValueKeyUInt32(hxPackageKey, 'PackageFamily',
Cardinal(PackageFamilyId));
end;
function PkgxSRCacheQueryPackageFlags;
begin
Result := NtxQueryValueKeyUInt32(hxPackageKey, 'Flags', Cardinal(Flags));
end;
function PkgxSRCacheQueryPackageFlags2;
begin
Result := NtxQueryValueKeyUInt32(hxPackageKey, 'Flags2', Cardinal(Flags2));
end;
function PkgxSRCacheQueryPackageType;
begin
Result := NtxQueryValueKeyUInt32(hxPackageKey, 'PackageType',
Cardinal(PackageType));
end;
function PkgxSRCacheQueryPackageLocation;
begin
Result := NtxQueryValueKeyString(hxPackageKey, 'InstalledLocation',
InstalledLocation);
end;
{ Applications }
function PkgxSRCacheIterateApplicationIDsInPackage;
var
hxIndexKey: IHandle;
Index: Integer;
begin
Index := 0;
Result := NtxAuto.IterateEx<TSRCacheApplicationId>(
Status,
function : TNtxStatus
begin
Result := NtxOpenKey(hxIndexKey,
RtlxCombinePaths(SR_CACHE_APPLICATION_INDEX, PkgxUIntToStr(PackageId)),
KEY_ENUMERATE_SUB_KEYS);
end,
function (out Current: TSRCacheApplicationId): TNtxStatus
var
IndexInfo: TNtxRegKey;
begin
// Query the sub-key
Result := NtxEnumerateKey(hxIndexKey, Index, IndexInfo);
if not Result.IsSuccess then
Exit;
// Convert the sub-key name to an ID
if not PkgxStrToUInt(IndexInfo.Name, Cardinal(Current)) then
begin
Result.Location := 'PkgxSRCacheIterateApplicationIDsInPackage';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
Exit;
end;
// Advance to the next
Inc(Index);
end
);
end;
function PkgxSRCacheIterateApplicationIDs;
var
hxDataKey: IHandle;
Index: Integer;
begin
Index := 0;
Result := NtxAuto.IterateEx<TSRCacheApplicationId>(
Status,
function : TNtxStatus
begin
Result := NtxOpenKey(hxDataKey, SR_CACHE_APPLICATION_DATA,
KEY_ENUMERATE_SUB_KEYS);
end,
function (out Current: TSRCacheApplicationId): TNtxStatus
var
IndexInfo: TNtxRegKey;
begin
// Query the sub-key
Result := NtxEnumerateKey(hxDataKey, Index, IndexInfo);
if not Result.IsSuccess then
Exit;
// Convert the sub-key name to an ID
if not PkgxStrToUInt(IndexInfo.Name, Cardinal(Current)) then
begin
Result.Location := 'PkgxSRCacheIterateApplicationIDs';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
Exit;
end;
// Advance to the next
Inc(Index);
end
);
end;
function PkgxSRCacheLookupApplicationId;
var
hxIndexKey: IHandle;
IndexInfo: TNtxRegKey;
begin
// Open the index key
Result := NtxOpenKey(
hxIndexKey,
RtlxCombinePaths(SR_CACHE_APPLICATION_INDEX_PRAID,
PkgxUIntToStr(PackageId) + '^' + RelativeName),
KEY_ENUMERATE_SUB_KEYS
);
if not Result.IsSuccess then
Exit;
// Query the first sub-key
Result := NtxEnumerateKey(hxIndexKey, 0, IndexInfo);
if not Result.IsSuccess then
Exit;
// Convert the sub-key name to an ID
if not PkgxStrToUInt(IndexInfo.Name, Cardinal(ApplicationId)) then
begin
Result.Location := 'PkgxSRCacheLookupApplicationId';
Result.Win32Error := APPMODEL_ERROR_PACKAGE_IDENTITY_CORRUPT;
end;
end;
function PkgxSRCacheOpenApplication;
begin
Result := NtxOpenKey(hxApplicationKey, RtlxCombinePaths(
SR_CACHE_APPLICATION_DATA, PkgxUIntToStr(ApplicationId)), KEY_QUERY_VALUE);
end;
function PkgxSRCacheQueryApplicationAumid;
begin
Result := NtxQueryValueKeyString(hxApplicationKey, 'ApplicationUserModelId',
Aumid);
end;
function PkgxSRCacheQueryApplicationPraid;
begin
Result := NtxQueryValueKeyString(hxApplicationKey,
'PackageRelativeApplicationId', RelativeName);
end;
function PkgxSRCacheQueryApplicationPackageID;
begin
Result := NtxQueryValueKeyUInt32(hxApplicationKey, 'Package',
Cardinal(PackageId));
end;
function PkgxSRCacheQueryApplicationFlags;
begin
Result := NtxQueryValueKeyUInt32(hxApplicationKey, 'Flags',
Cardinal(Flags));
end;
function PkgxSRCacheQueryApplicationEntrypoint;
begin
Result := NtxQueryValueKeyString(hxApplicationKey, 'Entrypoint',
Entrypoint);
end;
function PkgxSRCacheQueryApplicationExecutable;
begin
Result := NtxQueryValueKeyString(hxApplicationKey, 'Executable',
Executable);
end;
function PkgxSRCacheQueryApplicationStartPage;
begin
Result := NtxQueryValueKeyString(hxApplicationKey, 'StartPage', StartPage);
end;
end.