-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Packages.WinRT.pas
244 lines (191 loc) · 5.51 KB
/
NtUtils.Packages.WinRT.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
unit NtUtils.Packages.WinRT;
{
This module provides definitions for application package support in
Windows Runtime.
}
interface
uses
Ntapi.winrt.appmodel, Ntapi.winrt, Ntapi.Versions, NtUtils;
// Enumerate packages
[RequiresWinRT]
[MinOSVersion(OsWin8)]
function RoxEnumeratePackages(
out Packages: TArray<IPackage>;
AllUser: Boolean = False;
[opt] const UserSid: ISid = nil
): TNtxStatus;
// Enumerate packages returning full names
[RequiresWinRT]
[MinOSVersion(OsWin8)]
function RoxEnumeratePackageNames(
out FullNames: TArray<String>;
AllUser: Boolean;
[opt] const UserSid: ISid = nil
): TNtxStatus;
// Enumerate packages returning family names
[RequiresWinRT]
[MinOSVersion(OsWin8)]
function RoxEnumeratePackageFamilyNames(
out FamilyNames: TArray<String>;
AllUser: Boolean;
[opt] const UserSid: ISid = nil
): TNtxStatus;
// Enumerate packages returning application user-mode IDs
[RequiresWinRT]
[MinOSVersion(OsWin81)]
function RoxEnumeratePackageApps(
out AppUserModelIDs: TArray<String>;
AllUser: Boolean = False;
[opt] const UserSid: ISid = nil
): TNtxStatus;
implementation
uses
NtUtils.Com, NtUtils.Security.Sid, NtUtils.Packages, DelphiUtils.Arrays;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function RoxEnumeratePackages;
var
Inspectable: IInspectable;
PackageManager: IPackageManager;
Iterable: IIterable<IPackage>;
Iterator: IIterator<IPackage>;
Package: IPackage;
SidString: IHString;
HasCurrent: Boolean;
begin
Result := RoxActivateInstance('Windows.Management.Deployment.PackageManager',
Inspectable);
if not Result.IsSuccess then
Exit;
Result.Location := 'IInspectable::QueryInterface';
Result.LastCall.Parameter := 'IPackageManager';
Result.HResult := Inspectable.QueryInterface(IPackageManager,
PackageManager);
if not Result.IsSuccess then
Exit;
if AllUser then
begin
// Find all packages
Result.Location := 'IPackageManager::FindPackages';
Result.HResult := PackageManager.FindPackages(Iterable);
end
else
begin
// Prepare the user SID
if Assigned(UserSid) then
begin
Result := RoxCreateString(RtlxSidToString(UserSid), SidString);
if not Result.IsSuccess then
Exit;
end
else
SidString := nil;
// Find all user packages
Result.Location := 'IPackageManager::FindPackagesByUserSecurityId';
Result.HResult := PackageManager.FindPackagesByUserSecurityId(
Auto.RefOrNil<THString>(SidString), Iterable);
end;
if not Result.IsSuccess then
Exit;
Result.Location := 'IIterable<IPackage>::First';
Result.HResultAllowFalse := Iterable.First(Iterator);
if not Result.IsSuccess then
Exit;
Result.Location := 'IIterator<IPackage>::get_HasCurrent';
Result.HResult := Iterator.get_HasCurrent(HasCurrent);
if not Result.IsSuccess then
Exit;
Packages := nil;
while hasCurrent do
begin
Result.Location := 'IIterator<IPackage>::get_Current';
Result.HResult := Iterator.get_Current(Package);
if not Result.IsSuccess then
Exit;
SetLength(Packages, Length(Packages) + 1);
Packages[High(Packages)] := Package;
Result.Location := 'IIterator<IPackage>.MoveNext';
Result.HResult := Iterator.MoveNext(HasCurrent);
if not Result.IsSuccess then
Exit;
end;
end;
function RoxEnumeratePackageNames;
var
Packages: TArray<IPackage>;
PackageId: IPackageId;
i: Integer;
hString: THString;
hStringDeallocator: IAutoReleasable;
begin
Result := RoxEnumeratePackages(Packages, AllUser, UserSid);
if not Result.IsSuccess then
Exit;
SetLength(FullNames, Length(Packages));
for i := 0 to High(Packages) do
begin
Result.Location := 'IPackage::Get_Id';
Result.HResult := Packages[i].Get_Id(PackageId);
if not Result.IsSuccess then
Exit;
Result.Location := 'IPackageId::get_FullName';
Result.HResult := PackageId.get_FullName(hString);
if not Result.IsSuccess then
Exit;
hStringDeallocator := RoxCaptureString(hString);
FullNames[i] := RoxSaveString(hString);
end;
end;
function RoxEnumeratePackageFamilyNames;
var
Packages: TArray<IPackage>;
PackageId: IPackageId;
i: Integer;
hString: THString;
hStringDeallocator: IAutoReleasable;
begin
Result := RoxEnumeratePackages(Packages, AllUser, UserSid);
if not Result.IsSuccess then
Exit;
SetLength(FamilyNames, Length(Packages));
for i := 0 to High(Packages) do
begin
Result.Location := 'IPackage::Get_Id';
Result.HResult := Packages[i].Get_Id(PackageId);
if not Result.IsSuccess then
Exit;
Result.Location := 'IPackageId::get_FamilyName';
Result.HResult := PackageId.get_FamilyName(hString);
if not Result.IsSuccess then
Exit;
hStringDeallocator := RoxCaptureString(hString);
FamilyNames[i] := RoxSaveString(hString);
end;
end;
function RoxEnumeratePackageApps;
var
FullNames: TArray<String>;
IDs: TArray<TArray<String>>;
InfoReference: IPackageInfoReference;
i: Integer;
begin
// Collect all packages
Result := RoxEnumeratePackageNames(FullNames, AllUser, UserSid);
if not Result.IsSuccess then
Exit;
SetLength(IDs, Length(FullNames));
// Enumerate applications in each package
for i := 0 to High(FullNames) do
begin
Result := PkgxOpenPackageInfo(InfoReference, FullNames[i]);
if not Result.IsSuccess then
Exit;
Result := PkgxEnumerateAppUserModelIds(IDs[i], InfoReference);
if not Result.IsSuccess then
Exit;
end;
// Merge them
AppUserModelIDs := TArray.Flatten<String>(IDs);
end;
end.