-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppxPackagingHelper.cs
141 lines (122 loc) · 5.18 KB
/
AppxPackagingHelper.cs
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
namespace AppPackageInfo
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
/// <summary>
/// Appx packaging helper class
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Appx")]
public static class AppxPackagingHelper
{
/// <summary>
/// The id of class that implements IAppxFactoryInternal
/// </summary>
private const string AppxInternalClassId = "341301BA-111C-408D-A8E3-14D1DDC62A6F";
/// <summary>
/// The id of IAppxFactoryInternal
/// </summary>
private const string AppxInternalInterfaceId = "95903D88-B56C-4230-8278-941A59BD4335";
/// <summary>
/// Gets the SID from the given package family name
/// </summary>
/// <param name="packageName">Name of the package.</param>
/// <param name="publisher">The publisher.</param>
/// <returns>String representing an SID</returns>
[SupportedOSPlatform("windows")]
public static string GetSidFromPackageName(string packageName, string publisher)
{
string resultSid = null;
IAppxFactoryInternal factoryInternal = null;
try
{
factoryInternal = (IAppxFactoryInternal)Win32InteropHelper.CoCreateInstance(new Guid(AppxInternalClassId),
null, CLSCTX.INPROC,
new Guid(AppxInternalInterfaceId));
if (factoryInternal == null)
{
return null;
}
var packageFamilyName = factoryInternal.GetPackageFamilyNameFromPackageId(packageName, publisher);
if (!string.IsNullOrWhiteSpace(packageFamilyName))
{
resultSid = factoryInternal.GetPackageSidFromPackageFamilyName(packageFamilyName);
}
}
finally
{
if (factoryInternal != null)
{
Marshal.FinalReleaseComObject(factoryInternal);
}
}
return resultSid;
}
/// <summary>
/// Gets the SID from the given package family name
/// </summary>
/// <param name="packageFamilyName">Package family name.</param>
/// <returns>String representing an SID</returns>
[SupportedOSPlatform("windows")]
public static string GetSidFromPackageFamilyName(string packageFamilyName)
{
if (string.IsNullOrEmpty(packageFamilyName))
{
throw new ArgumentNullException("packageFamilyName");
}
string packageSid;
IAppxFactoryInternal appxInternalFactory = null;
try
{
appxInternalFactory = (IAppxFactoryInternal)Win32InteropHelper.CoCreateInstance(new Guid(AppxInternalClassId),
null, CLSCTX.INPROC,
new Guid(AppxInternalInterfaceId));
if (appxInternalFactory == null)
{
return null;
}
packageSid = appxInternalFactory.GetPackageSidFromPackageFamilyName(packageFamilyName);
}
finally
{
if (appxInternalFactory != null)
{
Marshal.FinalReleaseComObject(appxInternalFactory);
}
}
return packageSid;
}
/// <summary>
/// Gets the Package Family Name (PFN) from the given identity name and publisher
/// </summary>
/// <param name="packageName">Name of the package.</param>
/// <param name="publisher">The publisher.</param>
/// <returns>String representing an SID</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Pfn")]
[SupportedOSPlatform("windows")]
public static string GetPfnFromPackageName(string packageName, string publisher)
{
string resultPfn = null;
IAppxFactoryInternal factoryInternal = null;
try
{
factoryInternal = (IAppxFactoryInternal)Win32InteropHelper.CoCreateInstance(new Guid(AppxInternalClassId),
null, CLSCTX.INPROC,
new Guid(AppxInternalInterfaceId));
if (factoryInternal == null)
{
return null;
}
resultPfn = factoryInternal.GetPackageFamilyNameFromPackageId(packageName, publisher);
}
finally
{
if (factoryInternal != null)
{
Marshal.FinalReleaseComObject(factoryInternal);
}
}
return resultPfn;
}
}
}