11using System . Diagnostics ;
2+ using System . Runtime . ExceptionServices ;
23using Microsoft . Management . Deployment ;
34using UniGetUI . Core . Classes ;
45using UniGetUI . Core . Logging ;
@@ -21,42 +22,36 @@ internal sealed class NativeWinGetHelper : IWinGetManagerHelper
2122 public PackageManager WinGetManager = null ! ;
2223 public static PackageManager ? ExternalWinGetManager ;
2324 private readonly WinGet Manager ;
24- private readonly Func < WinGet , IWinGetManagerHelper > _bundledCliHelperFactory ;
2525 private readonly Func < WinGet , IWinGetManagerHelper > _systemCliHelperFactory ;
2626 private readonly Func < IReadOnlyList < CatalogPackage > > ? _localPackagesProvider ;
2727 private int _activeLocalPackageQueries ;
28+ private ExceptionDispatchInfo ? _lastActivationException ;
2829
2930 public string ActivationMode { get ; private set ; } = string . Empty ;
3031 public string ActivationSource { get ; private set ; } = string . Empty ;
31- private bool _isBundledActivation ;
3232
3333 internal static IReadOnlyList < string > PreferredActivationModes =>
3434 [
3535 "packaged COM registration" ,
3636 "lower-trust COM registration" ,
37- "bundled in-proc COM" ,
3837 ] ;
3938
4039 public NativeWinGetHelper ( WinGet manager )
4140 : this (
4241 manager ,
43- bundledCliHelperFactory : null ,
4442 systemCliHelperFactory : null ,
4543 skipInitialization : false ,
4644 localPackagesProvider : null
4745 ) { }
4846
4947 internal NativeWinGetHelper (
5048 WinGet manager ,
51- Func < WinGet , IWinGetManagerHelper > ? bundledCliHelperFactory ,
5249 Func < WinGet , IWinGetManagerHelper > ? systemCliHelperFactory ,
5350 bool skipInitialization ,
5451 Func < IReadOnlyList < CatalogPackage > > ? localPackagesProvider
5552 )
5653 {
5754 Manager = manager ;
58- _bundledCliHelperFactory =
59- bundledCliHelperFactory ?? ( static manager => new BundledWinGetHelper ( manager ) ) ;
6055 _systemCliHelperFactory =
6156 systemCliHelperFactory
6257 ?? ( static manager => new BundledWinGetHelper ( manager , manager . Status . ExecutablePath ) ) ;
@@ -83,14 +78,9 @@ internal NativeWinGetHelper(
8378 return ;
8479 }
8580
86- InitializeBundledFactory ( ) ;
87- }
81+ _lastActivationException ? . Throw ( ) ;
8882
89- internal void UseBundledActivationForTesting ( )
90- {
91- _isBundledActivation = true ;
92- ActivationMode = "bundled in-proc COM" ;
93- ActivationSource = "test" ;
83+ throw new InvalidOperationException ( "WinGet: Failed to initialize system COM activation." ) ;
9484 }
9585
9686 internal bool HasActiveLocalPackageQuery => Volatile . Read ( ref _activeLocalPackageQueries ) > 0 ;
@@ -117,15 +107,17 @@ private bool TryInitializeLowerTrustFactory()
117107 }
118108 catch ( WinGetComActivationException ex )
119109 {
110+ _lastActivationException = ExceptionDispatchInfo . Capture ( ex ) ;
120111 Logger . Warn (
121- $ "Lower-trust WinGet COM activation failed ({ ex . HResultHex } : { ex . Reason } ), attempting bundled in-proc activation.. ."
112+ $ "Lower-trust WinGet COM activation failed ({ ex . HResultHex } : { ex . Reason } )."
122113 ) ;
123114 return false ;
124115 }
125116 catch ( Exception ex )
126117 {
118+ _lastActivationException = ExceptionDispatchInfo . Capture ( ex ) ;
127119 Logger . Warn (
128- $ "Lower-trust WinGet COM activation failed ({ ex . Message } ), attempting bundled in-proc activation.. ."
120+ $ "Lower-trust WinGet COM activation failed ({ ex . Message } )."
129121 ) ;
130122 return false ;
131123 }
@@ -148,34 +140,22 @@ private bool TryInitializeStandardFactory()
148140 }
149141 catch ( WinGetComActivationException ex )
150142 {
143+ _lastActivationException = ExceptionDispatchInfo . Capture ( ex ) ;
151144 Logger . Warn (
152145 $ "Packaged WinGet COM activation failed ({ ex . HResultHex } : { ex . Reason } ), attempting lower-trust activation..."
153146 ) ;
154147 return false ;
155148 }
156149 catch ( Exception ex )
157150 {
151+ _lastActivationException = ExceptionDispatchInfo . Capture ( ex ) ;
158152 Logger . Warn (
159153 $ "Packaged WinGet COM activation failed ({ ex . Message } ), attempting lower-trust activation..."
160154 ) ;
161155 return false ;
162156 }
163157 }
164158
165- private void InitializeBundledFactory ( )
166- {
167- var factory = new WindowsPackageManagerBundledFactory ( ) ;
168- var winGetManager = factory . CreatePackageManager ( ) ;
169- ApplyFactory (
170- factory ,
171- winGetManager ,
172- "bundled in-proc COM" ,
173- factory . LibraryPath ,
174- "Connected to WinGet API using bundled in-proc activation."
175- ) ;
176- _isBundledActivation = true ;
177- }
178-
179159 private void ApplyFactory (
180160 WindowsPackageManagerFactory factory ,
181161 PackageManager winGetManager ,
@@ -321,12 +301,6 @@ var filter_type in new[]
321301
322302 public IReadOnlyList < Package > GetAvailableUpdates_UnSafe ( )
323303 {
324- if ( _isBundledActivation )
325- {
326- Logger . Info ( "WinGet is using bundled in-proc COM — falling back to CLI for update detection" ) ;
327- return _bundledCliHelperFactory ( Manager ) . GetAvailableUpdates_UnSafe ( ) ;
328- }
329-
330304 var logger = Manager . TaskLogger . CreateNew ( LoggableTaskType . ListUpdates ) ;
331305 IReadOnlyList < CatalogPackage > nativePackages ;
332306 try
@@ -401,12 +375,6 @@ public IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
401375
402376 public IReadOnlyList < Package > GetInstalledPackages_UnSafe ( )
403377 {
404- if ( _isBundledActivation )
405- {
406- Logger . Info ( "WinGet is using bundled in-proc COM — falling back to CLI for installed package detection" ) ;
407- return _bundledCliHelperFactory ( Manager ) . GetInstalledPackages_UnSafe ( ) ;
408- }
409-
410378 var logger = Manager . TaskLogger . CreateNew ( LoggableTaskType . ListInstalledPackages ) ;
411379 IReadOnlyList < CatalogPackage > nativePackages ;
412380 try
0 commit comments