-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNtUtils.ActCtx.pas
399 lines (335 loc) · 12.3 KB
/
NtUtils.ActCtx.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
unit NtUtils.ActCtx;
{
This modules provides functions for working with activation contexts.
}
interface
uses
Ntapi.WinNt, Ntapi.actctx, Ntapi.ntrtl, Ntapi.ntpebteb, Ntapi.ImageHlp,
DelphiApi.Reflection, NtUtils, DelphiUtils.AutoObjects;
type
IActivationContext = IAutoPointer<PActivationContext>;
TActxCtxDetailedInfo = record
Flags: TActivationContextFlags;
[Reserved(ACTIVATION_CONTEXT_DATA_FORMAT_WHISTLER)] FormatVersion: Cardinal;
AssemblyCount: Cardinal;
RootManifestPathType: TActivationContextPathType;
RootManifestPath: String;
RootConfigurationPathType: TActivationContextPathType;
RootConfigurationPath: String;
AppDirPathType: TActivationContextPathType;
AppDirPath: String;
end;
TActxCtxAssembly = record
[Hex] Flags: Cardinal;
EncodedAssemblyIdentity: String;
ManifestPathType: TActivationContextPathType;
ManifestPath: String;
ManifestLastWriteTime: TLargeInteger;
PolicyPathType: TActivationContextPathType;
PolicyPath: String;
PolicyLastWriteTime: TLargeInteger;
MetadataSatelliteRosterIndex: Cardinal;
DirectoryName: String;
FileCount: Cardinal;
end;
// Locate the activation context
[Result: MayReturnNil]
function RtlxCurrentActivationContext(
): PActivationContext;
// Locate the activation context data
[Result: MayReturnNil]
function RtlxCurrentActivationContextData(
): PActivationContextData;
type
ActCtx = class abstract
// Query fixed-size activation context information
class function Query<T>(
InfoClass: TActivationContextInfoClass;
out Buffer: T;
Flags: TRtlQueryInfoActCtxFlags =
RTL_QUERY_INFORMATION_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT;
[in] ActivationContext: PActivationContext = nil
): TNtxStatus; static;
end;
// Query variable-size activation context information
function RtlxQueryActivationContext(
out Buffer: IMemory;
InfoClass: TActivationContextInfoClass;
Flags: TRtlQueryInfoActCtxFlags =
RTL_QUERY_INFORMATION_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT;
[in, opt] ActivationContext: PActivationContext = nil;
[in] SubInstanceIndex: PActivationContextQueryIndex = nil;
InitialLength: NativeUInt = 0
): TNtxStatus;
// Query detailed information about an activation context
function RtlxQueryDetailedInfoActivationContext(
out Info: TActxCtxDetailedInfo;
Flags: TRtlQueryInfoActCtxFlags =
RTL_QUERY_INFORMATION_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT;
[in, opt] ActivationContext: PActivationContext = nil
): TNtxStatus;
// Retrieve information about all assemblies in an activation context
function RtlxEnumerateAssembliesActivationContext(
out Assemblies: TArray<TActxCtxAssembly>;
Flags: TRtlQueryInfoActCtxFlags =
RTL_QUERY_INFORMATION_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT;
[in, opt] ActivationContext: PActivationContext = nil
): TNtxStatus;
// Retrieve a string setting from an activation context
function RtlxQueryActivationContextSetting(
[in] ActivationContext: PActivationContext;
[opt] SettingsNameSpace: String;
SettingName: String;
out SettingValue: String
): TNtxStatus;
// Activate an activation context on a thread
function RtlxActivateActivationContext(
out Deactivator: IAutoReleasable;
[in, opt] ActivationContext: PActivationContext;
[in, opt] Teb: PTeb = nil;
Flags: TRtlActivateActCtxExFlags =
RTL_ACTIVATE_ACTIVATION_CONTEXT_EX_FLAG_RELEASE_ON_STACK_DEALLOCATION
): TNtxStatus;
// Create an activation context from an activation context data
function RtlxCreateActivationContext(
out hxActCtx: IActivationContext;
[in] ActivationContextData: PActivationContextData;
[opt] NotificationRoutine: TActivationContextNotifyRoutine = nil;
[in, opt] NotificationContext: Pointer = nil;
ExtraBytes: Cardinal = 0
): TNtxStatus;
// Create an activation context via CreateActCtxW
function AdvxCreateActivationContext(
out hxActCtx: IActivationContext;
Flags: TActCtxFlags;
const Source: String;
[in] Module: Pointer;
[in] ResourceName: PWideChar = CREATEPROCESS_MANIFEST_RESOURCE_ID;
const AssemblyDirectory: String = '';
const ApplicationName: String = '';
ProcessorArchitecture: TProcessorArchitecture16 = PROCESSOR_ARCHITECTURE_CURRENT;
LangId: Word = 0
): TNtxStatus;
implementation
uses
Ntapi.ntmmapi, Ntapi.ntpsapi, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function RtlxCurrentActivationContext;
var
ActiveFrame: PRtlActivationContextStackFrame;
begin
// Take the context from the active stack frame
ActiveFrame := NtCurrentTeb.ActivationStack.ActiveFrame;
if Assigned(ActiveFrame) then
Result := ActiveFrame.ActivationContext
else
Result := ACTCTX_PROCESS_DEFAULT;
end;
function RtlxCurrentActivationContextData;
var
ActiveContext: PActivationContext;
begin
// Use the context from the current thread when available
ActiveContext := RtlxCurrentActivationContext;
if Assigned(ActiveContext) then
Exit(ActiveContext.ActivationContextData);
// Fall back to the process-wide context data
Result := RtlGetCurrentPeb.ActivationContextData;
// Fall back to the system=default context datat
if not Assigned(Result) then
Result := RtlGetCurrentPeb.SystemDefaultActivationContextData;
end;
class function ActCtx.Query<T>;
begin
Result.Location := 'RtlQueryInformationActivationContext';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.Status := RtlQueryInformationActivationContext(Flags,
ActivationContext, nil, InfoClass, @Buffer, SizeOf(Buffer), nil);
end;
function RtlxQueryActivationContext;
var
RequiredSize: NativeUInt;
begin
Result.Location := 'RtlQueryInformationActivationContext';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Buffer := Auto.AllocateDynamic(InitialLength);
repeat
RequiredSize := 0;
Result.Status := RtlQueryInformationActivationContext(Flags,
ActivationContext, SubInstanceIndex, InfoClass, Buffer.Data, Buffer.Size,
@RequiredSize);
until not NtxExpandBufferEx(Result, Buffer, RequiredSize, nil);
if not Result.IsSuccess then
Buffer := nil;
end;
function RtlxQueryDetailedInfoActivationContext;
var
Buffer: IMemory<PActivationContextDetailedInformation>;
begin
Result := RtlxQueryActivationContext(IMemory(Buffer),
ActivationContextDetailedInformation, Flags, ActivationContext,
nil, SizeOf(TActivationContextDetailedInformation));
if not Result.IsSuccess then
Exit;
Info.Flags := Buffer.Data.Flags;
Info.FormatVersion := Buffer.Data.FormatVersion;
Info.AssemblyCount := Buffer.Data.AssemblyCount;
Info.RootManifestPathType := Buffer.Data.RootManifestPathType;
Info.RootConfigurationPathType := Buffer.Data.RootConfigurationPathType;
Info.AppDirPathType := Buffer.Data.AppDirPathType;
SetString(Info.RootManifestPath, Buffer.Data.RootManifestPath,
Buffer.Data.RootManifestPathChars);
SetString(Info.RootConfigurationPath, Buffer.Data.RootConfigurationPath,
Buffer.Data.RootConfigurationPathChars);
SetString(Info.AppDirPath, Buffer.Data.AppDirPath,
Buffer.Data.AppDirPathChars);
end;
function RtlxEnumerateAssembliesActivationContext;
var
Details: TActxCtxDetailedInfo;
Buffer: IMemory<PActivationContextAssemblyDetailedInformation>;
Index: TActivationContextQueryIndex;
i: Integer;
begin
// Query detailed info to determine the number of assemblies
Result := RtlxQueryDetailedInfoActivationContext(Details, Flags,
ActivationContext);
if not Result.IsSuccess then
Exit;
SetLength(Assemblies, Details.AssemblyCount);
for i := 0 to High(Assemblies) do
begin
// Assembly indexes are 1-based
Index.AssemblyIndex := i + 1;
// Query each assembly
Result := RtlxQueryActivationContext(IMemory(Buffer),
AssemblyDetailedInformationInActivationContext, Flags,
ActivationContext, @Index);
if not Result.IsSuccess then
Exit;
Assemblies[i].Flags := Buffer.Data.Flags;
Assemblies[i].ManifestPathType := Buffer.Data.PolicyPathType;
Assemblies[i].ManifestLastWriteTime := Buffer.Data.ManifestLastWriteTime;
Assemblies[i].PolicyPathType := Buffer.Data.PolicyPathType;
Assemblies[i].PolicyLastWriteTime := Buffer.Data.PolicyLastWriteTime;
Assemblies[i].MetadataSatelliteRosterIndex :=
Buffer.Data.MetadataSatelliteRosterIndex;
Assemblies[i].FileCount := Buffer.Data.FileCount;
SetString(Assemblies[i].EncodedAssemblyIdentity,
Buffer.Data.AssemblyEncodedAssemblyIdentity,
Buffer.Data.EncodedAssemblyIdentityLength div SizeOf(WideChar));
SetString(Assemblies[i].ManifestPath,
Buffer.Data.AssemblyManifestPath,
Buffer.Data.ManifestPathLength div SizeOf(WideChar));
SetString(Assemblies[i].PolicyPath,
Buffer.Data.AssemblyPolicyPath,
Buffer.Data.PolicyPathLength div SizeOf(WideChar));
SetString(Assemblies[i].DirectoryName,
Buffer.Data.AssemblyDirectoryName,
Buffer.Data.AssemblyDirectoryNameLength div SizeOf(WideChar));
end;
end;
function RtlxQueryActivationContextSetting;
var
Buffer: IMemory<PWideChar>;
RequiredChars: NativeUInt;
begin
Result.Location := 'RtlQueryActivationContextApplicationSettings';
Result.LastCall.Parameter := SettingName;
IMemory(Buffer) := Auto.AllocateDynamic(64);
repeat
Result.Status := RtlQueryActivationContextApplicationSettings(0,
ActivationContext, RefStrOrNil(SettingsNameSpace), PWideChar(SettingName),
Buffer.Data, Buffer.Size div SizeOf(WideChar), @RequiredChars);
until not NtxExpandBufferEx(Result, IMemory(Buffer), RequiredChars *
SizeOf(WideChar), nil);
// Capture the result
if Result.IsSuccess then
SettingValue := RtlxCaptureString(Buffer.Data,
Buffer.Size div SizeOf(WideChar));
end;
function RtlxDelayedDeactivateActivationContext(
Cookie: NativeUInt
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
RtlDeactivateActivationContext(0, Cookie);
end
);
end;
function RtlxActivateActivationContext;
var
Cookie: NativeUInt;
begin
if not Assigned(Teb) then
Teb := NtCurrentTeb;
Result.Location := 'RtlActivateActivationContextEx';
Result.Status := RtlActivateActivationContextEx(Flags, Teb, ActivationContext,
Cookie);
if Result.IsSuccess then
Deactivator := RtlxDelayedDeactivateActivationContext(Cookie);
end;
type
TAutoActivationContext = class (TCustomAutoPointer, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
procedure TAutoActivationContext.Release;
begin
if Assigned(FData) then
RtlReleaseActivationContext(FData);
FData := nil;
inherited;
end;
procedure RtlxDefaultActivationContextNotification(
NotificationType: TActivationContextNotification;
[in] ActivationContext: PActivationContext;
[in] ActivationContextData: PActivationContextData;
[in] NotificationContext: Pointer;
[in] NotificationData: Pointer;
var DisableThisNotification: Boolean
); stdcall;
begin
// Reproduce behavior of kernel32.BasepSxsActivationContextNotification
if NotificationType = ACTIVATION_CONTEXT_NOTIFICATION_DESTROY then
NtUnmapViewOfSection(NtCurrentProcess, ActivationContextData)
else
DisableThisNotification := True;
end;
function RtlxCreateActivationContext;
var
hActCtx: PActivationContext;
begin
if not Assigned(NotificationRoutine) then
NotificationRoutine := RtlxDefaultActivationContextNotification;
Result.Location := 'RtlCreateActivationContext';
Result.Status := RtlCreateActivationContext(0, ActivationContextData,
ExtraBytes, NotificationRoutine, NotificationContext, hActCtx);
if Result.IsSuccess then
IAutoPointer(hxActCtx) := TAutoActivationContext.Capture(hActCtx);
end;
function AdvxCreateActivationContext;
var
ActCtx: TActCtxW;
hActCtx: PActivationContext;
begin
ActCtx := Default(TActCtxW);
ActCtx.Size := SizeOf(ActCtx);
ActCtx.Flags := Flags;
ActCtx.Source := RefStrOrNil(Source);
ActCtx.ProcessorArchitecture := ProcessorArchitecture;
ActCtx.LangId := LangId;
ActCtx.AssemblyDirectory := RefStrOrNil(AssemblyDirectory);
ActCtx.ResourceName := ResourceName;
ActCtx.ApplicationName := RefStrOrNil(ApplicationName);
ActCtx.hModule := HModule(Module);
Result.Location := 'CreateActCtxW';
hActCtx := CreateActCtxW(ActCtx);
Result.Win32Result := hActCtx <> INVALID_ACTIVATION_CONTEXT;
if Result.IsSuccess then
IAutoPointer(hxActCtx) := TAutoActivationContext.Capture(hActCtx);
end;
end.