AppxPackageHelper.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections.Generic; 6 using System.Runtime.InteropServices; 7 using System.Runtime.InteropServices.ComTypes; 8 9 using Wox.Plugin.Common.Win32; 10 11 namespace Microsoft.Plugin.Program.Programs 12 { 13 public static class AppxPackageHelper 14 { 15 private static readonly IAppxFactory AppxFactory = (IAppxFactory)new AppxFactory(); 16 17 // This function returns a list of attributes of applications 18 public static IEnumerable<IAppxManifestApplication> GetAppsFromManifest(IStream stream) 19 { 20 var reader = AppxFactory.CreateManifestReader(stream); 21 var manifestApps = reader.GetApplications(); 22 23 while (manifestApps.GetHasCurrent()) 24 { 25 var manifestApp = manifestApps.GetCurrent(); 26 var hr = manifestApp.GetStringValue("AppListEntry", out var appListEntry); 27 _ = CheckHRAndReturnOrThrow(hr, appListEntry); 28 if (appListEntry != "none") 29 { 30 yield return manifestApp; 31 } 32 33 manifestApps.MoveNext(); 34 } 35 } 36 37 public static T CheckHRAndReturnOrThrow<T>(HRESULT hr, T result) 38 { 39 if (hr != HRESULT.S_OK) 40 { 41 Marshal.ThrowExceptionForHR((int)hr); 42 } 43 44 return result; 45 } 46 } 47 }