-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNtUtils.Csr.pas
632 lines (518 loc) · 18.6 KB
/
NtUtils.Csr.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
unit NtUtils.Csr;
{
This module provides functions for calling into CSRSS Win32 subsystem process.
}
interface
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntcsrapi, Ntapi.ntpsapi, Ntapi.actctx,
Ntapi.ImageHlp, NtUtils, NtUtils.ActCtx, DelphiUtils.AutoObjects;
type
ICsrCaptureHeader = IMemory<PCsrCaptureHeader>;
// Allocate a buffer for capturing pointers before sending them to CSRSS
function CsrxAllocateCaptureBuffer(
out CaptureBuffer: ICsrCaptureHeader;
TotalLength: Cardinal;
PointerCount: Cardinal
): TNtxStatus;
// Prepare a region for storing data in a capture buffer
function CsrxAllocateMessagePointer(
const CaptureBuffer: ICsrCaptureHeader;
RequiredLength: Cardinal;
out MessagePointer: Pointer
): TNtxStatus;
// Marshal a string into a capture buffer
procedure CsrxCaptureMessageString(
const CaptureBuffer: ICsrCaptureHeader;
const StringData: String;
out CapturedString: TNtUnicodeString
);
// Capture multiple string pointers in a buffer without copying
function CsrxCaptureMessageMultiUnicodeStringsInPlace(
out CaptureBuffer: ICsrCaptureHeader;
const Strings: TArray<PNtUnicodeString>
): TNtxStatus;
// Send a message to CSRSS
function CsrxClientCallServer(
var Msg: TCsrApiMsg;
MsgSizeIncludingHeader: Cardinal;
ApiNumber: TCsrApiNumber;
[in, opt] CaptureBuffer: PCsrCaptureHeader = nil
): TNtxStatus;
{ BASESRV functions }
// Adjust shutdown order for the current process
function CsrxSetShutdownParameters(
ShutdownLevel: Cardinal;
ShutdownFlags: TShutdownParamFlags
): TNtxStatus;
// Determine shutdown order for the current process
function CsrxGetShutdownParameters(
out ShutdownLevel: Cardinal;
out ShutdownFlags: TShutdownParamFlags
): TNtxStatus;
// Define/undefine a symbolic link in the DosDevices object namespace directory
function CsrxDefineDosDevice(
const DeviceName: String;
const TargetPath: String;
Flags: TDefineDosDeviceFlags = 0
): TNtxStatus;
// Register a process with SxS
function CsrxRegisterProcessManifest(
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
const hxThread: IHandle;
const ClientId: TClientId;
const Handle: IHandle;
HandleType: TBaseMsgHandleType;
const Region: TMemory;
const Path: String
): TNtxStatus;
// Register a process with SxS using an external manifest from a file
function CsrxRegisterProcessManifestFromFile(
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
const hxThread: IHandle;
const ClientId: TClientId;
const FileName: String;
const Path: String
): TNtxStatus;
// Register a process with SxS using an external manifest from a string
function CsrxRegisterProcessManifestFromString(
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
const hxThread: IHandle;
const ClientId: TClientId;
const ManifestString: UTF8String;
const Path: String
): TNtxStatus;
// Create an activation context via a message to SxS
function CsrxCreateActivationContext(
out hxActCtx: IActivationContext;
const Handle: IHandle;
HandleType: TBaseMsgHandleType;
const Region: TMemory;
const ManifestPath: String = '';
AssemblyDirectory: String = '';
ResourceId: PWideChar = CREATEPROCESS_MANIFEST_RESOURCE_ID;
ProcessorArchitecture: TProcessorArchitecture16 = PROCESSOR_ARCHITECTURE_CURRENT
): TNtxStatus;
// Create an activation context using an external manifest from a file
function CsrxCreateActivationContextFromFile(
out hxActCtx: IActivationContext;
const FileName: String;
const AssemblyDirectory: String = '';
ResourceId: PWideChar = CREATEPROCESS_MANIFEST_RESOURCE_ID;
ProcessorArchitecture: TProcessorArchitecture16 = PROCESSOR_ARCHITECTURE_CURRENT
): TNtxStatus;
// Create an activation context using an external manifest from a string
function CsrxCreateActivationContextFromString(
out hxActCtx: IActivationContext;
const ManifestString: UTF8String;
const FileName: String = '';
const AssemblyDirectory: String = '';
ResourceId: PWideChar = CREATEPROCESS_MANIFEST_RESOURCE_ID;
ProcessorArchitecture: TProcessorArchitecture16 = PROCESSOR_ARCHITECTURE_CURRENT
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntmmapi, Ntapi.ntrtl, Ntapi.ntioapi, Ntapi.ntpebteb,
Ntapi.Versions, NtUtils.Processes, NtUtils.Processes.Info, NtUtils.Files.Open,
NtUtils.Files.Operations, NtUtils.Sections, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TCsrAutoBuffer = class (TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
procedure TCsrAutoBuffer.Release;
begin
if Assigned(FData) then
CsrFreeCaptureBuffer(FData);
FData := nil;
inherited;
end;
function CsrxAllocateCaptureBuffer;
var
Buffer: PCsrCaptureHeader;
begin
Buffer := CsrAllocateCaptureBuffer(PointerCount, TotalLength);
if not Assigned(Buffer) then
begin
Result.Location := 'CsrAllocateCaptureBuffer';
Result.Status := STATUS_NO_MEMORY;
end
else
begin
IMemory(CaptureBuffer) := TCsrAutoBuffer.Capture(Buffer, TotalLength);
Result := NtxSuccess;
end
end;
function CsrxAllocateMessagePointer;
var
AllocatedBytes: Cardinal;
begin
AllocatedBytes := CsrAllocateMessagePointer(CaptureBuffer.Data,
RequiredLength, MessagePointer);
if AllocatedBytes < RequiredLength then
begin
Result.Location := 'CsrAllocateMessagePointer';
Result.Status := STATUS_NO_MEMORY;
end
else
Result := NtxSuccess;
end;
procedure CsrxCaptureMessageString;
begin
CsrCaptureMessageString(
CaptureBuffer.Data,
PWideChar(StringData),
StringSizeNoZero(StringData),
StringSizeZero(StringData),
CapturedString
);
end;
function CsrxCaptureMessageMultiUnicodeStringsInPlace;
var
Buffer: PCsrCaptureHeader;
begin
Buffer := nil;
Result.Location := 'CsrCaptureMessageMultiUnicodeStringsInPlace';
Result.Status := CsrCaptureMessageMultiUnicodeStringsInPlace(Buffer,
Length(Strings), Strings);
if Result.IsSuccess then
IMemory(CaptureBuffer) := TCsrAutoBuffer.Capture(Buffer, Buffer.Length);
end;
function CsrxClientCallServer;
begin
if MsgSizeIncludingHeader < SizeOf(TCsrApiMsg) then
begin
Result.Location := 'CsrxClientCallServer';
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
Result.Location := 'CsrClientCallServer';
Result.Status := CsrClientCallServer(Msg, CaptureBuffer, ApiNumber,
MsgSizeIncludingHeader - SizeOf(TCsrApiMsg));
end;
{ Base SRV }
function CsrxClientCallServerBaseSrv(
var Msg: TCsrApiMsg;
MsgSizeIncludingHeader: Cardinal;
BaseSrvApiNumber: TBaseSrvApiNumber;
[in, opt] CaptureBuffer: PCsrCaptureHeader = nil
): TNtxStatus;
begin
Result := CsrxClientCallServer(Msg, MsgSizeIncludingHeader,
CsrMakeApiNumber(BASESRV_SERVERDLL_INDEX, Word(BaseSrvApiNumber)),
CaptureBuffer);
Result.LastCall.UsesInfoClass(BaseSrvApiNumber, icControl);
end;
function CsrxSetShutdownParameters;
var
Msg: TBaseShutdownParamMsg;
begin
Msg := Default(TBaseShutdownParamMsg);
Msg.ShutdownLevel := ShutdownLevel;
Msg.ShutdownFlags := ShutdownFlags;
Result := CsrxClientCallServerBaseSrv(Msg.CsrMessage,
SizeOf(TBaseShutdownParamMsg), BasepSetProcessShutdownParam);
end;
function CsrxGetShutdownParameters;
var
Msg: TBaseShutdownParamMsg;
begin
Msg := Default(TBaseShutdownParamMsg);
Result := CsrxClientCallServerBaseSrv(Msg.CsrMessage,
SizeOf(TBaseShutdownParamMsg), BasepGetProcessShutdownParam);
if Result.IsSuccess then
begin
ShutdownLevel := Msg.ShutdownLevel;
ShutdownFlags := Msg.ShutdownFlags;
end;
end;
function CsrxDefineDosDevice;
var
CaptureBuffer: ICsrCaptureHeader;
Msg: TBaseDefineDosDeviceMsg;
begin
// Allocate a Csr buffer for capturing string pointers
Result := CsrxAllocateCaptureBuffer(CaptureBuffer, StringSizeZero(DeviceName)
+ StringSizeZero(TargetPath), 2);
if not Result.IsSuccess then
Exit;
// Prepare the message and capture the strings
Msg := Default(TBaseDefineDosDeviceMsg);
Msg.Flags := Flags;
CsrxCaptureMessageString(CaptureBuffer, DeviceName, Msg.DeviceName);
CsrxCaptureMessageString(CaptureBuffer, TargetPath, Msg.TargetPath);
// Call CSRSS
Result := CsrxClientCallServerBaseSrv(Msg.CsrMessage,
SizeOf(TBaseDefineDosDeviceMsg), BasepDefineDosDevice, CaptureBuffer.Data);
end;
procedure CsrxpAdjustCreateProcessMsgLayout(
var Msg: IMemory<PBaseCreateProcessMsgV1>;
out StringsToCapture: TArray<PNtUnicodeString>
);
var
MsgWin7: IMemory<PBaseCreateProcessMsgV1Win7>;
begin
if RtlOsVersion in [OsWin7, OsWin8, OsWin81, OsWin1019H1, OsWin1019H2] then
begin
// Older versions of the OS have a slightly different structure layout
IMemory(MsgWin7) := Auto.AllocateDynamic(SizeOf(TBaseCreateProcessMsgV1Win7));
// Copy all fields
MsgWin7.Data.CsrMessage := Msg.Data.CsrMessage;
MsgWin7.Data.ProcessHandle := Msg.Data.ProcessHandle;
MsgWin7.Data.ThreadHandle := Msg.Data.ThreadHandle;
MsgWin7.Data.ClientID := Msg.Data.ClientID;
MsgWin7.Data.CreationFlags := Msg.Data.CreationFlags;
MsgWin7.Data.VdmBinaryType := Msg.Data.VdmBinaryType;
MsgWin7.Data.VdmTask := Msg.Data.VdmTask;
MsgWin7.Data.hVDM := Msg.Data.hVDM;
MsgWin7.Data.Sxs.Flags := Msg.Data.Sxs.Flags;
MsgWin7.Data.Sxs.ProcessParameterFlags := Msg.Data.Sxs.ProcessParameterFlags;
MsgWin7.Data.Sxs.Union := Msg.Data.Sxs.Union;
MsgWin7.Data.Sxs.CultureFallbacks := Msg.Data.Sxs.CultureFallbacks;
MsgWin7.Data.Sxs.RunLevel := Msg.Data.Sxs.RunLevel;
MsgWin7.Data.Sxs.SupportedOsInfo := Msg.Data.Sxs.SupportedOsInfo;
// <-- Here is the field that breaks the layout
MsgWin7.Data.Sxs.AssemblyName := Msg.Data.Sxs.AssemblyName;
MsgWin7.Data.PebAddressNative := Msg.Data.PebAddressNative;
MsgWin7.Data.PebAddressWow64 := Msg.Data.PebAddressWow64;
MsgWin7.Data.ProcessorArchitecture := Msg.Data.ProcessorArchitecture;
// Capture the strings with adjusted layout
StringsToCapture := [
@MsgWin7.Data.Sxs.Union.Local.Manifest.Path,
@MsgWin7.Data.Sxs.Union.Local.Policy.Path,
@MsgWin7.Data.Sxs.Union.Local.AssemblyDirectory,
@MsgWin7.Data.Sxs.CultureFallbacks,
@MsgWin7.Data.Sxs.AssemblyName
];
// Replace the buffer
IMemory(Msg) := IMemory(MsgWin7);
end
else
begin
// Capture the strings with normal layout
StringsToCapture := [
@Msg.Data.Sxs.Union.Local.Manifest.Path,
@Msg.Data.Sxs.Union.Local.Policy.Path,
@Msg.Data.Sxs.Union.Local.AssemblyDirectory,
@Msg.Data.Sxs.CultureFallbacks,
@Msg.Data.Sxs.AssemblyName
];
end;
end;
function CsrxRegisterProcessManifest;
var
Msg: IMemory<PBaseCreateProcessMsgV1>;
StringsToCapture: TArray<PNtUnicodeString>;
CaptureBuffer: ICsrCaptureHeader;
BasicInfo: TProcessBasicInformation;
WoW64Peb: Pointer;
ImageInfo: TSectionImageInformation;
AssemblyDirectory: String;
begin
// Determine native PEB location
Result := NtxProcess.Query(hxProcess, ProcessBasicInformation, BasicInfo);
if not Result.IsSuccess then
Exit;
// Determine WoW64 PEB location
Result := NtxProcess.Query(hxProcess, ProcessWow64Information, WoW64Peb);
if not Result.IsSuccess then
Exit;
// Determine image architecture
Result := NtxProcess.Query(hxProcess, ProcessImageInformation, ImageInfo);
if not Result.IsSuccess then
Exit;
// Prepare a message to Csr/SxS
IMemory(Msg) := Auto.AllocateDynamic(SizeOf(TBaseCreateProcessMsgV1));
Result := RtlxInitUnicodeString(Msg.Data.Sxs.Union.Local.Manifest.Path,
Path);
if not Result.IsSuccess then
Exit;
AssemblyDirectory := RtlxExtractRootPath(Path);
Result := RtlxInitUnicodeString(Msg.Data.Sxs.Union.Local.AssemblyDirectory,
AssemblyDirectory);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(Msg.Data.Sxs.CultureFallbacks,
DEFAULT_CULTURE_FALLBACKS);
if not Result.IsSuccess then
Exit;
Msg.Data.ProcessHandle := HandleOrDefault(hxProcess);
Msg.Data.ThreadHandle := HandleOrDefault(hxThread);
Msg.Data.ClientID := ClientID;
Msg.Data.Sxs.Flags := BASE_MSG_SXS_MANIFEST_PRESENT;
Msg.Data.Sxs.ProcessParameterFlags := RTL_USER_PROC_APP_MANIFEST_PRESENT;
Msg.Data.Sxs.Union.Local.Manifest.FileType := BASE_MSG_FILETYPE_XML;
Msg.Data.Sxs.Union.Local.Manifest.PathType := BASE_MSG_PATHTYPE_FILE;
Msg.Data.Sxs.Union.Local.Manifest.HandleType := HandleType;
Msg.Data.Sxs.Union.Local.Manifest.Handle := HandleOrDefault(Handle);
Msg.Data.Sxs.Union.Local.Manifest.Offset := UIntPtr(Region.Address);
Msg.Data.Sxs.Union.Local.Manifest.Size := Region.Size;
Msg.Data.PebAddressNative := UIntPtr(BasicInfo.PebBaseAddress);
Msg.Data.PebAddressWow64 := UIntPtr(WoW64Peb);
case ImageInfo.Machine of
IMAGE_FILE_MACHINE_I386:
Msg.Data.ProcessorArchitecture := PROCESSOR_ARCHITECTURE_INTEL;
IMAGE_FILE_MACHINE_AMD64:
Msg.Data.ProcessorArchitecture := PROCESSOR_ARCHITECTURE_AMD64;
end;
// Fix data layout for older OS versions
CsrxpAdjustCreateProcessMsgLayout(Msg, StringsToCapture);
// Capture string buffers
Result := CsrxCaptureMessageMultiUnicodeStringsInPlace(CaptureBuffer,
StringsToCapture);
if not Result.IsSuccess then
Exit;
// Send the message to Csr
Result := CsrxClientCallServerBaseSrv(Msg.Data.CsrMessage, Msg.Size,
BasepCreateProcess, CaptureBuffer.Data);
end;
function CsrxRegisterProcessManifestFromFile;
var
hxFile, hxSection: IHandle;
FileInfo: TFileStandardInformation;
begin
// Open the manifest file
Result := NtxOpenFile(hxFile, FileParameters
.UseFileName(FileName, fnWin32)
.UseAccess(FILE_READ_DATA)
.UseOptions(FILE_NON_DIRECTORY_FILE)
.UseSyncMode(fsAsynchronous)
);
if not Result.IsSuccess then
Exit;
// Determine its size
Result := NtxFile.Query(hxFile, FileStandardInformation, FileInfo);
if not Result.IsSuccess then
Exit;
// Create a section from the manifest
Result := NtxCreateFileSection(hxSection, hxFile, PAGE_READONLY, SEC_COMMIT);
if not Result.IsSuccess then
Exit;
// Use the section object as the manifest source
Result := CsrxRegisterProcessManifest(hxProcess, hxThread, ClientId,
hxSection, BASE_MSG_HANDLETYPE_SECTION, TMemory.From(nil,
FileInfo.EndOfFile), Path);
end;
function CsrxRegisterProcessManifestFromString;
var
hxSection: IHandle;
ManifestSize: NativeUInt;
Mapping: IMemory;
begin
ManifestSize := Length(ManifestString) * SizeOf(UTF8Char);
// Create a section for sharing the manifest with SxS
Result := NtxCreateSection(hxSection, ManifestSize, PAGE_READWRITE);
if not Result.IsSuccess then
Exit;
// Map it locally to fill in the content
Result := NtxMapViewOfSection(hxSection, NtxCurrentProcess, Mapping,
MappingParameters.UseProtection(PAGE_READWRITE));
if not Result.IsSuccess then
Exit;
// Copy the XML from the string
Move(PUTF8Char(ManifestString)^, Mapping.Data^, ManifestSize);
// Send the message to SxS
Result := CsrxRegisterProcessManifest(hxProcess, hxThread, ClientId,
hxSection, BASE_MSG_HANDLETYPE_SECTION, TMemory.From(nil,
ManifestSize), Path);
end;
function CsrxCreateActivationContext;
var
Msg: TBaseSxsCreateActivationContextMsg;
ActivationContextData: PActivationContextData;
CaptureBuffer: ICsrCaptureHeader;
begin
if AssemblyDirectory = '' then
AssemblyDirectory := USER_SHARED_DATA.NtSystemRoot + '\WinSxS';
Result := RtlxInitUnicodeString(Msg.AssemblyDirectory, AssemblyDirectory);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(Msg.CultureFallbacks, DEFAULT_CULTURE_FALLBACKS);
if not Result.IsSuccess then
Exit;
Msg := Default(TBaseSxsCreateActivationContextMsg);
Msg.Flags := BASE_MSG_SXS_MANIFEST_PRESENT;
Msg.ProcessorArchitecture := ProcessorArchitecture;
Msg.Manifest.FileType := BASE_MSG_FILETYPE_XML;
Msg.Manifest.HandleType := HandleType;
Msg.Manifest.Handle := HandleOrDefault(Handle);
Msg.Manifest.Offset := UIntPtr(Region.Address);
Msg.Manifest.Size := Region.Size;
Msg.ResourceName := ResourceId;
Msg.ActivationContextData := @ActivationContextData;
if ManifestPath <> '' then
begin
Msg.Manifest.PathType := BASE_MSG_PATHTYPE_FILE;
Result := RtlxInitUnicodeString(Msg.Manifest.Path, ManifestPath);
if not Result.IsSuccess then
Exit;
end;
// Capture string buffers
Result := CsrxCaptureMessageMultiUnicodeStringsInPlace(CaptureBuffer,
[@Msg.CultureFallbacks, @Msg.Manifest.Path, @Msg.Policy.Path,
@Msg.AssemblyDirectory, @Msg.TextualAssemblyIdentity, @Msg.AssemblyName]);
if not Result.IsSuccess then
Exit;
// Send the message to Csr
Result := CsrxClientCallServerBaseSrv(Msg.CsrMessage, SizeOf(Msg),
BasepCreateActivationContext, CaptureBuffer.Data);
if not Result.IsSuccess then
Exit;
// Convert the activation context data into an activation context
Result := RtlxCreateActivationContext(hxActCtx, ActivationContextData);
// Make sure to unmap the data if something goes wrong
if not Result.IsSuccess then
NtxUnmapViewOfSection(NtxCurrentProcess, ActivationContextData);
end;
function CsrxCreateActivationContextFromFile;
var
hxFile, hxSection: IHandle;
FileInfo: TFileStandardInformation;
begin
// Open the manifest file
Result := NtxOpenFile(hxFile, FileParameters
.UseFileName(FileName, fnWin32)
.UseAccess(FILE_READ_DATA)
.UseOptions(FILE_NON_DIRECTORY_FILE)
.UseSyncMode(fsAsynchronous)
);
if not Result.IsSuccess then
Exit;
// Determine its size
Result := NtxFile.Query(hxFile, FileStandardInformation, FileInfo);
if not Result.IsSuccess then
Exit;
// Create a section from the manifest
Result := NtxCreateFileSection(hxSection, hxFile, PAGE_READONLY, SEC_COMMIT);
if not Result.IsSuccess then
Exit;
// Use the section object as the manifest source
Result := CsrxCreateActivationContext(hxActCtx, hxSection,
BASE_MSG_HANDLETYPE_SECTION, TMemory.From(nil, FileInfo.EndOfFile),
FileName, AssemblyDirectory, ResourceId, ProcessorArchitecture);
end;
function CsrxCreateActivationContextFromString;
var
hxSection: IHandle;
ManifestSize: NativeUInt;
Mapping: IMemory;
begin
ManifestSize := Length(ManifestString) * SizeOf(UTF8Char);
// Create a section for sharing the manifest with SxS
Result := NtxCreateSection(hxSection, ManifestSize, PAGE_READWRITE);
if not Result.IsSuccess then
Exit;
// Map it locally to fill in the content
Result := NtxMapViewOfSection(hxSection, NtxCurrentProcess, Mapping,
MappingParameters.UseProtection(PAGE_READWRITE));
if not Result.IsSuccess then
Exit;
// Copy the XML from the string
Move(PUTF8Char(ManifestString)^, Mapping.Data^, ManifestSize);
// Send the message to SxS
Result := CsrxCreateActivationContext(hxActCtx, hxSection,
BASE_MSG_HANDLETYPE_SECTION, TMemory.From(nil, ManifestSize), FileName,
AssemblyDirectory, ResourceId, ProcessorArchitecture);
end;
end.