-
-
Save marcinotorowski/77062aeeae519820b189bd623dfecbab to your computer and use it in GitHub Desktop.
Gets full name of Appx Package and family name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static string GetPackageFamilyName(string name, string publisherId) | |
{ | |
string packageFamilyName = null; | |
PACKAGE_ID packageId = new PACKAGE_ID | |
{ | |
name = name, | |
publisher = publisherId | |
}; | |
uint packageFamilyNameLength = 0; | |
//First get the length of the Package Name -> Pass NULL as Output Buffer | |
if (PackageFamilyNameFromId(packageId, ref packageFamilyNameLength, null) == 122) //ERROR_INSUFFICIENT_BUFFER | |
{ | |
StringBuilder packageFamilyNameBuilder = new StringBuilder((int)packageFamilyNameLength); | |
if (PackageFamilyNameFromId(packageId, ref packageFamilyNameLength, packageFamilyNameBuilder) == 0) | |
{ | |
packageFamilyName = packageFamilyNameBuilder.ToString(); | |
} | |
} | |
return packageFamilyName; | |
} | |
public enum APPX_PACKAGE_ARCHITECTURE : uint | |
{ | |
APPX_PACKAGE_ARCHITECTURE_X86 = 1, | |
APPX_PACKAGE_ARCHITECTURE_ARM = 5, | |
APPX_PACKAGE_ARCHITECTURE_X64 = 9, | |
APPX_PACKAGE_ARCHITECTURE_NEUTRAL = 11, | |
APPX_PACKAGE_ARCHITECTURE_ARM64 = 12, | |
}; | |
// Declares managed structures instead of unions. | |
[StructLayout(LayoutKind.Explicit)] | |
public struct PackageVersion | |
{ | |
public PackageVersion(ulong version) : this() | |
{ | |
Version = version; | |
} | |
public PackageVersion(ushort major, ushort minor, ushort build, ushort revision) : this() | |
{ | |
Revision = revision; | |
Build = build; | |
Minor = minor; | |
Major = major; | |
} | |
public PackageVersion(Version version) : this((ushort)version.Major, (ushort)version.Minor, (ushort)version.Build, (ushort)version.Revision) | |
{ | |
} | |
public PackageVersion(string version) : this(System.Version.Parse(version)) | |
{ | |
} | |
[FieldOffset(0 * sizeof(ulong))] | |
public ulong Version; | |
[FieldOffset(0 * sizeof(ushort))] | |
public ushort Revision; | |
[FieldOffset(1 * sizeof(ushort))] | |
public ushort Build; | |
[FieldOffset(2 * sizeof(ushort))] | |
public ushort Minor; | |
[FieldOffset(3 * sizeof(ushort))] | |
public ushort Major; | |
} | |
private static string GetPackageFullName(string name, string publisherId, APPX_PACKAGE_ARCHITECTURE architecture, PackageVersion version = default, string resourceId = null) | |
{ | |
string packageFullName = null; | |
PACKAGE_ID packageId = new PACKAGE_ID | |
{ | |
name = name, | |
publisher = publisherId, | |
processorArchitecture = (uint)architecture, | |
version = version.Version, | |
resourceId = resourceId | |
}; | |
uint packageFamilyNameLength = 0; | |
//First get the length of the Package Name -> Pass NULL as Output Buffer | |
if (PackageFullNameFromId(packageId, ref packageFamilyNameLength, null) == 122) //ERROR_INSUFFICIENT_BUFFER | |
{ | |
StringBuilder packageFamilyNameBuilder = new StringBuilder((int)packageFamilyNameLength); | |
if (PackageFullNameFromId(packageId, ref packageFamilyNameLength, packageFamilyNameBuilder) == 0) | |
{ | |
packageFullName = packageFamilyNameBuilder.ToString(); | |
} | |
} | |
return packageFullName; | |
} | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 4)] | |
private class PACKAGE_ID | |
{ | |
public uint reserved; | |
public uint processorArchitecture; | |
public ulong version; | |
public string name; | |
public string publisher; | |
public string resourceId; | |
public string publisherId; | |
} | |
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] | |
private static extern uint PackageFamilyNameFromId(PACKAGE_ID packageId, ref uint packageFamilyNameLength, StringBuilder packageFamilyName); | |
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] | |
private static extern uint PackageFullNameFromId(PACKAGE_ID packageId, ref uint packageFamilyNameLength, StringBuilder packageFamilyName); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment