-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Dism.pas
1601 lines (1354 loc) · 39.9 KB
/
NtUtils.Dism.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit NtUtils.Dism;
{
This module provides support for using DISM (Deployment Image Servicing and
Managemen) API.
}
interface
uses
Ntapi.WinNt, Ntapi.dismapi, Ntapi.ntseapi, Ntapi.WinBase, Ntapi.Versions,
NtUtils, DelphiApi.Reflection;
const
// The online image pseudo-path for opening sessions
DISM_ONLINE_IMAGE = Ntapi.dismapi.DISM_ONLINE_IMAGE;
type
IDismSession = NtUtils.IHandle;
// An anonymous callback for monitoring progress
TDismxProgressCallback = reference to procedure (Current, Total: Cardinal);
[MinOSVersion(OsWin8)]
TDismxImageInfo = record
ImageType: TDismImageType;
ImageIndex: Cardinal;
ImageName: String;
ImageDescription: String;
[Bytes] ImageSize: UInt64;
Architecture: TProcessorArchitecture32;
ProductName: String;
EditionId: String;
InstallationType: String;
Hal: String;
ProductType: String;
ProductSuite: String;
MajorVersion: Cardinal;
MinorVersion: Cardinal;
Build: Cardinal;
SpBuild: Cardinal;
SpLevel: Cardinal;
Bootable: TDismImageBootable;
SystemRoot: String;
Language: TArray<String>;
DefaultLanguageIndex: Cardinal;
end;
[MinOSVersion(OsWin8)]
TDismxMountedImageInfo = record
MountPath: String;
ImageFilePath: String;
ImageIndex: Cardinal;
MountMode: TDismMountMode;
MountStatus: TDismMountStatus;
end;
[MinOSVersion(OsWin8)]
TDismxPackage = record
PackageName: String;
PackageState: TDismPackageFeatureState;
ReleaseType: TDismReleaseType;
InstallTime: TSystemTime;
end;
[MinOSVersion(OsWin8)]
TDismxCustomProperty = record
Name: String;
Value: String;
Path: String;
end;
[MinOSVersion(OsWin8)]
TDismxFeature = record
FeatureName: String;
State: TDismPackageFeatureState;
end;
[MinOSVersion(OsWin8)]
TDismxPackageInfo = record
PackageName: String;
PackageState: TDismPackageFeatureState;
ReleaseType: TDismReleaseType;
InstallTime: TSystemTime;
Applicable: Boolean;
Copyright: String;
Company: String;
CreationTime: TSystemTime;
DisplayName: String;
Description: String;
InstallClient: String;
InstallPackageName: String;
LastUpdateTime: TSystemTime;
ProductName: String;
ProductVersion: String;
RestartRequired: TDismRestartType;
FullyOffline: TDismFullyOfflineInstallableType;
SupportInformation: String;
CustomProperties: TArray<TDismxCustomProperty>;
Features: TArray<TDismxFeature>;
end;
[MinOSVersion(OsWin8)]
TDismxFeatureInfo = record
FeatureName: String;
FeatureState: TDismPackageFeatureState;
DisplayName: String;
Description: String;
RestartRequired: TDismRestartType;
CustomProperties: TArray<TDismxCustomProperty>;
end;
[MinOSVersion(OsWin8)]
TDismxDriverPackage = record
PublishedName: String;
OriginalFileName: String;
InBox: Boolean;
CatalogFile: String;
ClassName: String;
ClassGuid: String;
ClassDescription: String;
BootCritical: Boolean;
DriverSignature: TDismDriverSignature;
ProviderName: String;
Date: TSystemTime;
MajorVersion: Cardinal;
MinorVersion: Cardinal;
Build: Cardinal;
Revision: Cardinal;
end;
PDismxDriverPackage = ^TDismxDriverPackage;
[MinOSVersion(OsWin8)]
TDismxDriver = record
ManufacturerName: String;
HardwareDescription: String;
HardwareId: String;
Architecture: TProcessorArchitecture32;
ServiceName: String;
CompatibleIds: String;
ExcludeIds: String;
end;
[MinOSVersion(OsWin10TH1)]
TDismxCapability = record
Name: String;
State: TDismPackageFeatureState;
end;
[MinOSVersion(OsWin10TH1)]
TDismxCapabilityInfo = record
Name: String;
State: TDismPackageFeatureState;
DisplayName: String;
Description: String;
[Bytes] DownloadSize: Cardinal;
[Bytes] InstallSize: Cardinal;
end;
[MinOSVersion(OsWin81)]
TDismxAppxPackage = record
PackageName: String;
DisplayName: String;
PublisherId: String;
MajorVersion: Cardinal;
MinorVersion: Cardinal;
Build: Cardinal;
RevisionNumber: Cardinal;
Architecture: TProcessorArchitecture32;
ResourceId: String;
InstallLocation: String;
Region: String;
end;
{ Initialization }
// Initialize DISM API
[RequiresAdmin]
[MinOSVersion(OsWin8)]
[Result: ReleaseWith('DismxShutdown')]
function DismxInitialize(
LogLevel: TDismLogLevel = DismLogErrorsWarnings;
[opt] const LogFilePath: String = '';
[opt] const ScratchDirectory: String = ''
): TNtxStatus;
// Uninitialize DISM API
[MinOSVersion(OsWin8)]
function DismxShutdown(
): TNtxStatus;
// Initialize DISM API and uninitialize it later
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxInitializeAuto(
out Uninitializer: IAutoReleasable;
LogLevel: TDismLogLevel = DismLogErrorsWarnings;
[opt] const LogFilePath: String = '';
[opt] const ScratchDirectory: String = ''
): TNtxStatus;
// Initialize DISM API once and uninitialize it on this unit finalizatoin
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxInitializeOnce(
LogLevel: TDismLogLevel = DismLogErrorsWarnings;
[opt] const LogFilePath: String = '';
[opt] const ScratchDirectory: String = ''
): TNtxStatus;
{ Images }
// Open a DISM session for the specified online/offline image path
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxOpenSession(
out hxDismSession: IDismSession;
const ImagePath: String;
[opt] const WindowsDirectory: String = '';
[opt] const SystemDrive: String = ''
): TNtxStatus;
// Query information about an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxGetImageInfo(
const ImageFilePath: String;
out ImageInfo: TArray<TDismxImageInfo>
): TNtxStatus;
// Mount a .wim or a .vhdx image to a given directory
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxMountImage(
const ImageFilePath: String;
const MountPath: String;
[opt] ImageIndex: Cardinal;
[opt] ImageName: String;
ImageIdentifier: TDismImageIdentifier;
Flags: TDismMountFlags = DISM_MOUNT_READONLY;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] CancelEvent: THandle = 0
): TNtxStatus;
// Unmount a previously mounted image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxUnmountImage(
const MountPath: String;
Flags: TDismUnmountFlags = DISM_DISCARD_IMAGE;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] CancelEvent: THandle = 0
): TNtxStatus;
// Remount a previously mounted image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxRemountImage(
const MountPath: String
): TNtxStatus;
// Save changes to a mounted image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxCommitImage(
const hxDismSession: IDismSession;
[in] Flags: Cardinal;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Query information about a mounted image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxGetMountedImageInfo(
out ImageInfo: TArray<TDismxMountedImageInfo>
): TNtxStatus;
// Removes files from corrupted and invalid mount points
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxCleanupMountpoints(
): TNtxStatus;
// Verify the image or check if it has already been flagged as corrupted
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxCheckImageHealth(
const hxDismSession: IDismSession;
ScanImage: Boolean;
out ImageHealth: TDismImageHealthState;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Repairs a corrupted image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxRestoreImageHealth(
const hxDismSession: IDismSession;
LimitAccess: Boolean;
[opt] const SourcePaths: TArray<String> = nil;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
{ Packages }
// Add a .cab or a .msu to an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxAddPackage(
const hxDismSession: IDismSession;
const PackagePath: String;
IgnoreCheck: Boolean;
PreventPending: Boolean;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Add a .cab or a .msu from an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxRemovePackage(
const hxDismSession: IDismSession;
const Identifier: String;
PackageIdentifier: TDismPackageIdentifier;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Enumerate .cab or .msu packages in an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxEnumeratePackages(
const hxDismSession: IDismSession;
out Packages: TArray<TDismxPackage>
): TNtxStatus;
// Query information about a .cab or a .msu package
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxQueryPackage(
const hxDismSession: IDismSession;
const Identifier: String;
PackageIdentifier: TDismPackageIdentifier;
out PackageInfo: TDismxPackageInfo
): TNtxStatus;
{ Features }
// Enumerate features in a a .cab or a .msu package
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxEnumerateFeatures(
const hxDismSession: IDismSession;
out Features: TArray<TDismxFeature>;
[opt] const Identifier: String = '';
[opt] PackageIdentifier: TDismPackageIdentifier = DismPackageNone
): TNtxStatus;
// Query information about a package feature
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxQueryFeature(
const hxDismSession: IDismSession;
const FeatureName: String;
out FeatureInfo: TDismxFeatureInfo;
[opt] const Identifier: String = '';
[opt] PackageIdentifier: TDismPackageIdentifier = DismPackageNone
): TNtxStatus;
// Enable a package feature
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxEnableFeature(
const hxDismSession: IDismSession;
const FeatureName: String;
LimitAccess: Boolean;
EnableAll: Boolean;
[opt] const Identifier: String = '';
[opt] PackageIdentifier: TDismPackageIdentifier = DismPackageNone;
[opt] const SourcePaths: TArray<String> = nil;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Disable a package feature
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxDisableFeature(
const hxDismSession: IDismSession;
const FeatureName: String;
RemovePayload: Boolean;
[opt] const PackageName: String = '';
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Enumerate dependencies of a package feature
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxEnumerateFeatureParents(
const hxDismSession: IDismSession;
const FeatureName: String;
out Features: TArray<TDismxFeature>;
[opt] const Identifier: String = '';
[opt] PackageIdentifier: TDismPackageIdentifier = DismPackageNone
): TNtxStatus;
{ Unattend }
// Apply an unattend XML to the image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxApplyUnattend(
const hxDismSession: IDismSession;
const UnattendFile: String;
SingleSession: Boolean = True
): TNtxStatus;
{ Drivers }
// Add a driver into an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxAddDriver(
const hxDismSession: IDismSession;
const DriverPath: String;
ForceUnsigned: Boolean = False
): TNtxStatus;
// Add a driver into an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxRemoveDriver(
const hxDismSession: IDismSession;
const DriverPath: String
): TNtxStatus;
// Enumerate all or out-of-the-box only drivers in an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxEnumerateDrivers(
const hxDismSession: IDismSession;
AllDrivers: Boolean;
out DriverPackages: TArray<TDismxDriverPackage>
): TNtxStatus;
// Query driver information from an image
[RequiresAdmin]
[MinOSVersion(OsWin8)]
function DismxQueryDriver(
const hxDismSession: IDismSession;
const DriverPath: String;
out Drivers: TArray<TDismxDriver>;
[out, opt] DriverPackage: PDismxDriverPackage = nil
): TNtxStatus;
{ Capabilities }
// Enumerate capabilities in an image
[RequiresAdmin]
[MinOSVersion(OsWin10TH1)]
function DismxEnumerateCapabilities(
const hxDismSession: IDismSession;
out Capabilities: TArray<TDismxCapability>
): TNtxStatus;
// Query information about a capability in an image
[RequiresAdmin]
[MinOSVersion(OsWin10TH1)]
function DismxQueryCapability(
const hxDismSession: IDismSession;
const Name: String;
out Info: TDismxCapabilityInfo
): TNtxStatus;
// Add a capability to an image
[RequiresAdmin]
[MinOSVersion(OsWin10TH1)]
function DismxAddCapability(
const hxDismSession: IDismSession;
const Name: String;
LimitAccess: Boolean;
[opt] const SourcePaths: TArray<String> = nil;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
// Remove a capability to an image
[RequiresAdmin]
[MinOSVersion(OsWin10TH1)]
function DismxRemoveCapability(
const hxDismSession: IDismSession;
const Name: String;
[opt] const ProgressCallback: TDismxProgressCallback = nil;
[opt] const CancelEvent: IHandle = nil
): TNtxStatus;
{ Appx }
// Enumerate Appx packages in an image
[RequiresAdmin]
[MinOSVersion(OsWin81)]
function DismxEnumerateProvisionedAppxPackages(
const hxDismSession: IDismSession;
out Packages: TArray<TDismxAppxPackage>
): TNtxStatus;
// Register an Appx package in an image
[RequiresAdmin]
[MinOSVersion(OsWin81)]
function DismxAddProvisionedAppxPackage(
const hxDismSession: IDismSession;
const AppPath: String;
SkipLicense: Boolean;
[opt] const DependencyPackages: TArray<String> = nil;
[opt] const OptionalPackages: TArray<String> = nil;
[opt] const LicensePaths: TArray<String> = nil;
[opt] const CustomDataPath: String = '';
[opt] const Region: String = '';
StubPackageOption: TDismStubPackageOption = DismStubPackageOptionNone
): TNtxStatus;
// Remove an Appx package from an image
[RequiresAdmin]
[MinOSVersion(OsWin81)]
function DismxRemoveProvisionedAppxPackage(
const hxDismSession: IDismSession;
const PackageName: String
): TNtxStatus;
implementation
uses
NtUtils.Ldr, NtUtils.Synchronization, DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Auto resources }
type
TDismSessionAutoHandle = class (TCustomAutoHandle, IDismSession)
procedure Release; override;
end;
procedure TDismSessionAutoHandle.Release;
begin
if (FHandle <> 0) and LdrxCheckDelayedImport(
delayed_DismCloseSession).IsSuccess then
DismCloseSession(FHandle);
FHandle := 0;
inherited;
end;
type
TDismAutoMemory = class (TCustomAutoPointer, IAutoPointer)
procedure Release; override;
end;
procedure TDismAutoMemory.Release;
begin
if Assigned(FData) and LdrxCheckDelayedImport(
delayed_DismDelete).IsSuccess then
DismDelete(FData);
FData := nil;
inherited;
end;
function DismxDelayedFree(
Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
if LdrxCheckDelayedImport(delayed_DismDelete).IsSuccess then
DismDelete(Buffer);
end
);
end;
{ Callback support }
procedure DismxpCallbackDispatcher(
[in] Current: Cardinal;
[in] Total: Cardinal;
[in] UserData: Pointer
); stdcall;
var
Callback: TDismxProgressCallback absolute UserData;
begin
if Assigned(Callback) then
Callback(Current, Total);
end;
[Result: MayReturnNil]
function DismxpGetCallbackDispatcher(
[opt] const Callback: TDismxProgressCallback
): TDismProgressCallback;
begin
if Assigned(Callback) then
Result := DismxpCallbackDispatcher
else
Result := nil;
end;
{ Initialization }
function DismxInitialize;
begin
Result := LdrxCheckDelayedImport(delayed_DismInitialize);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismInitialize';
Result.HResult := DismInitialize(LogLevel, RefStrOrNil(LogFilePath),
RefStrOrNil(ScratchDirectory));
end;
function DismxShutdown;
begin
Result := LdrxCheckDelayedImport(delayed_DismShutdown);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismShutdown';
Result.HResult := DismShutdown;
end;
function DismxInitializeAuto;
begin
Result := DismxInitialize(LogLevel, LogFilePath, ScratchDirectory);
if not Result.IsSuccess then
Exit;
Uninitializer := Auto.Delay(
procedure
begin
DismxShutdown;
end
);
end;
var
DismxInitialized: TRtlRunOnce;
DismxUnitinitializer: IAutoReleasable;
function DismxInitializeOnce;
var
InitState: IAcquiredRunOnce;
begin
if RtlxRunOnceBegin(@DismxInitialized, InitState) then
begin
// Put uninitializer into a global variable to trigger cleaup on unit unload
Result := DismxInitializeAuto(DismxUnitinitializer, LogLevel, LogFilePath,
ScratchDirectory);
if not Result.IsSuccess then
Exit;
InitState.Complete;
end
else
Result := NtxSuccess;
end;
{ Images }
function DismxOpenSession;
var
hSession: TDismSession;
begin
Result := LdrxCheckDelayedImport(delayed_DismOpenSession);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismOpenSession';
Result.HResult := DismOpenSession(PWideChar(ImagePath),
RefStrOrNil(WindowsDirectory), RefStrOrNil(SystemDrive), hSession);
if Result.IsSuccess then
hxDismSession := TDismSessionAutoHandle.Capture(hSession);
end;
function DismxGetImageInfo;
var
Buffer: PDismImageInfoArray;
BufferDeallocator: IAutoReleasable;
Cursor: PDismImageInfo;
Count: Cardinal;
i, j: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_DismGetImageInfo);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismGetImageInfo';
Result.HResult := DismGetImageInfo(PWideChar(ImageFilePath), Buffer, Count);
if not Result.IsSuccess then
Exit;
BufferDeallocator := DismxDelayedFree(Buffer);
SetLength(ImageInfo, Count);
Cursor := @Buffer[0];
for i := 0 to High(ImageInfo) do
begin
ImageInfo[i].ImageType := Cursor.ImageType;
ImageInfo[i].ImageIndex := Cursor.ImageIndex;
ImageInfo[i].ImageName := Cursor.ImageName;
ImageInfo[i].ImageDescription := Cursor.ImageDescription;
ImageInfo[i].ImageSize := Cursor.ImageSize;
ImageInfo[i].Architecture := Cursor.Architecture;
ImageInfo[i].ProductName := Cursor.ProductName;
ImageInfo[i].EditionId := Cursor.EditionId;
ImageInfo[i].InstallationType := Cursor.InstallationType;
ImageInfo[i].Hal := Cursor.Hal;
ImageInfo[i].ProductType := Cursor.ProductType;
ImageInfo[i].ProductSuite := Cursor.ProductSuite;
ImageInfo[i].MajorVersion := Cursor.MajorVersion;
ImageInfo[i].MinorVersion := Cursor.MinorVersion;
ImageInfo[i].Build := Cursor.Build;
ImageInfo[i].SpBuild := Cursor.SpBuild;
ImageInfo[i].SpLevel := Cursor.SpLevel;
ImageInfo[i].Bootable := Cursor.Bootable;
ImageInfo[i].SystemRoot := Cursor.SystemRoot;
SetLength(ImageInfo[i].Language, Cursor.LanguageCount);
for j := 0 to High(ImageInfo[i].Language) do
ImageInfo[i].Language[j] := Cursor
.Language{$R-}[j]{$IFDEF R+}{$R+}{$ENDIF}.Value;
ImageInfo[i].DefaultLanguageIndex := Cursor.DefaultLanguageIndex;
Inc(Cursor);
end;
end;
function DismxMountImage;
var
Context: Pointer absolute ProgressCallback;
begin
Result := LdrxCheckDelayedImport(delayed_DismMountImage);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismMountImage';
Result.HResult := DismMountImage(
PWideChar(ImageFilePath),
PWideChar(MountPath),
ImageIndex,
RefStrOrNil(ImageName),
ImageIdentifier,
Flags,
CancelEvent,
DismxpGetCallbackDispatcher(ProgressCallback),
Context
);
end;
function DismxUnmountImage;
var
Context: Pointer absolute ProgressCallback;
begin
Result := LdrxCheckDelayedImport(delayed_DismUnmountImage);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismUnmountImage';
Result.HResult := DismUnmountImage(
PWideChar(MountPath),
Flags,
CancelEvent,
DismxpGetCallbackDispatcher(ProgressCallback),
Context
);
end;
function DismxRemountImage;
begin
Result := LdrxCheckDelayedImport(delayed_DismRemountImage);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismRemountImage';
Result.HResult := DismRemountImage(PWideChar(MountPath));
end;
function DismxCommitImage;
var
Context: Pointer absolute ProgressCallback;
begin
Result := LdrxCheckDelayedImport(delayed_DismCommitImage);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismCommitImage';
Result.HResult := DismCommitImage(
HandleOrDefault(hxDismSession),
Flags,
HandleOrDefault(CancelEvent),
DismxpGetCallbackDispatcher(ProgressCallback),
Context
);
end;
function DismxGetMountedImageInfo;
var
Buffer: PDismMountedImageInfoArray;
BufferDeallocator: IAutoReleasable;
Cursor: PDismMountedImageInfo;
Count: Cardinal;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_DismGetMountedImageInfo);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismGetMountedImageInfo';
Result.HResult := DismGetMountedImageInfo(Buffer, Count);
if not Result.IsSuccess then
Exit;
BufferDeallocator := DismxDelayedFree(Buffer);
SetLength(ImageInfo, Count);
Cursor := @Buffer[0];
for i := 0 to High(ImageInfo) do
begin
ImageInfo[i].MountPath := Cursor.MountPath;
ImageInfo[i].ImageFilePath := Cursor.ImageFilePath;
ImageInfo[i].ImageIndex := Cursor.ImageIndex;
ImageInfo[i].MountMode := Cursor.MountMode;
ImageInfo[i].MountStatus := Cursor.MountStatus;
Inc(Cursor);
end;
end;
function DismxCleanupMountpoints;
begin
Result := LdrxCheckDelayedImport(delayed_DismCleanupMountpoints);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismCleanupMountpoints';
Result.HResult := DismCleanupMountpoints;
end;
function DismxCheckImageHealth;
var
Context: Pointer absolute ProgressCallback;
begin
Result := LdrxCheckDelayedImport(delayed_DismCheckImageHealth);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismCheckImageHealth';
Result.HResult := DismCheckImageHealth(
HandleOrDefault(hxDismSession),
ScanImage,
HandleOrDefault(CancelEvent),
DismxpGetCallbackDispatcher(ProgressCallback),
Context,
ImageHealth
);
end;
function DismxRestoreImageHealth;
var
Context: Pointer absolute ProgressCallback;
SourcePathRefs: TArray<PWideChar>;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_DismRestoreImageHealth);
if not Result.IsSuccess then
Exit;
SetLength(SourcePathRefs, Length(SourcePaths));
for i := 0 to High(SourcePathRefs) do
SourcePathRefs[i] := PWideChar(SourcePaths[i]);
Result.Location := 'DismRestoreImageHealth';
Result.HResult := DismRestoreImageHealth(
HandleOrDefault(hxDismSession),
SourcePathRefs,
Length(SourcePathRefs),
LimitAccess,
HandleOrDefault(CancelEvent),
DismxpGetCallbackDispatcher(ProgressCallback),
Context
);
end;
{ Packages }
function DismxAddPackage;
var
Context: Pointer absolute ProgressCallback;
begin
Result := LdrxCheckDelayedImport(delayed_DismAddPackage);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismAddPackage';
Result.HResult := DismAddPackage(
HandleOrDefault(hxDismSession),
PWideChar(PackagePath),
IgnoreCheck,
PreventPending,
HandleOrDefault(CancelEvent),
DismxpGetCallbackDispatcher(ProgressCallback),
Context
);
end;
function DismxRemovePackage;
var
Context: Pointer absolute ProgressCallback;
begin
Result := LdrxCheckDelayedImport(delayed_DismRemovePackage);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismRemovePackage';
Result.HResult := DismRemovePackage(
HandleOrDefault(hxDismSession),
PWideChar(Identifier),
PackageIdentifier,
HandleOrDefault(CancelEvent),
DismxpGetCallbackDispatcher(ProgressCallback),
Context
);
end;
function DismxEnumeratePackages;
var
Buffer: PDismPackageArray;
BufferDeallocator: IAutoReleasable;
Cursor: PDismPackage;
Count: Cardinal;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_DismGetPackages);
if not Result.IsSuccess then
Exit;
Result.Location := 'DismGetPackages';
Result.HResult := DismGetPackages(
HandleOrDefault(hxDismSession),
Buffer,
Count
);
if not Result.IsSuccess then
Exit;
BufferDeallocator := DismxDelayedFree(Buffer);
SetLength(Packages, Count);
Cursor := @Buffer[0];
for i := 0 to High(Packages) do
begin
Packages[i].PackageName := Cursor.PackageName;
Packages[i].PackageState := Cursor.PackageState;
Packages[i].ReleaseType := Cursor.ReleaseType;
Packages[i].InstallTime := Cursor.InstallTime;
Inc(Cursor);
end;
end;
function DismxQueryPackage;
var
Buffer: PDismPackageInfo;
BufferDeallocator: IAutoReleasable;
i: Integer;
begin