Skip to content

Commit

Permalink
Support user-scope for UpgradeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbar committed Aug 31, 2023
1 parent f1d2ea1 commit 58ea21b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
version:
description: 'Build & package version'
required: true
default: 0.1.1
default: 0.1.2
type: string
jobs:
Build:
Expand Down
1 change: 0 additions & 1 deletion MsiZapEx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Install.txt = Install.txt
LICENSE = LICENSE
README.md = README.md
TidyBuild.custom.props = TidyBuild.custom.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MsiZapEx", "MsiZapEx\MsiZapEx.csproj", "{9F89D927-E4D7-48F1-89B6-C7E41816F202}"
Expand Down
2 changes: 2 additions & 0 deletions MsiZapEx/BundleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public enum StatusFlags
Good = ProviderKeyProductCodeMatch | HkcrDependencies | ArpPorviderKey | ArpUpgradeCodes | ARP
}

//TODO Support per-user bundles

public RegistryView RegistryView { get; private set; }
public Guid BundleProductCode { get; private set; } = Guid.Empty;
public List<Guid> BundleUpgradeCodes { get; private set; } = new List<Guid>();
Expand Down
10 changes: 5 additions & 5 deletions MsiZapEx/ProductInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ public void PrintState()

if (!Status.HasFlag(StatusFlags.HkcrProduct))
{
Console.WriteLine($@"{'\t'}Missing HKCR key under 'Installer\Products");
Console.WriteLine($@"{'\t'}Missing HKCR key under 'Installer\Products'");
}
if (!Status.HasFlag(StatusFlags.HkcrFeatures))
{
Console.WriteLine($@"{'\t'}Missing HKCR key under 'Installer\Features");
Console.WriteLine($@"{'\t'}Missing HKCR key under 'Installer\Features'");
}
if (!Status.HasFlag(StatusFlags.ARP))
{
Console.WriteLine($@"{'\t'}Missing Uninstall key under 'Software\Microsoft\Windows\CurrentVersion\Uninstall");
Console.WriteLine($@"{'\t'}Missing Uninstall key under 'Software\Microsoft\Windows\CurrentVersion\Uninstall'");
}
if (!Status.HasFlag(StatusFlags.HklmFeatures))
{
Console.WriteLine($@"{'\t'}Missing HKLM key under 'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{UserSID}\Products\<ProductCode SUID>\Features");
Console.WriteLine($@"{'\t'}Missing HKLM key under 'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{UserSID}\Products\<ProductCode SUID>\Features'");
}
if (!Status.HasFlag(StatusFlags.HklmProduct))
{
Console.WriteLine($@"{'\t'}Missing HKLM key under 'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{UserSID}\Products\<ProductCode SUID>\InstallProperties");
Console.WriteLine($@"{'\t'}Missing HKLM key under 'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{UserSID}\Products\<ProductCode SUID>\InstallProperties'");
}

if (IsShallow)
Expand Down
43 changes: 36 additions & 7 deletions MsiZapEx/UpgradeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,31 @@ public enum StatusFlags
public List<ProductInfo> RelatedProducts { get; private set; }
public StatusFlags Status { get; private set; } = StatusFlags.None;

public UpgradeInfo(Guid upgradeCode, bool shallow = false)
public bool MachineScope { get; private set; }
public bool UserScope => !MachineScope;
public string UserSID => MachineScope ? ProductInfo.LocalSystemSID : ProductInfo.CurrentUserSID;

internal static bool ResolveScope(Guid productCode)
{
string obfuscatedGuid = GuidEx.MsiObfuscate(productCode);
using (RegistryKey hklm64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (RegistryKey k = hklm64.OpenSubKey($@"SOFTWARE\Classes\Installer\UpgradeCodes\{obfuscatedGuid}", false))
{
if (k != null)
{
// Machine scope exists for this upgrade-code
return true;
}
}
}
return false;
}

public UpgradeInfo(Guid upgradeCode, bool shallow = false, bool? machineScope = null)
{
UpgradeCode = upgradeCode;
MachineScope = machineScope ?? ResolveScope(upgradeCode);
Enumerate(shallow);
}

Expand All @@ -45,11 +67,11 @@ public void PrintState()
Console.WriteLine($"UpgradeCode '{UpgradeCode}', {RelatedProducts.Count} products");
if (!Status.HasFlag(StatusFlags.HkcrUpgarde))
{
Console.WriteLine($@"{'\t'}Missing HKCR key under 'Installer\UpgradeCodes");
Console.WriteLine($@"{'\t'}Missing HKCR key under 'Installer\UpgradeCodes'");
}
if (!Status.HasFlag(StatusFlags.HklmHkcrProductsMatch))
{
Console.WriteLine($@"{'\t'}HKCR key 'Installer\UpgradeCodes and HKLM key 'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes' have mismatching products");
Console.WriteLine($@"{'\t'}HKCR key 'Installer\UpgradeCodes' and HKLM key 'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes' have mismatching products");
}
if (!Status.HasFlag(StatusFlags.HklmUpgarde))
{
Expand Down Expand Up @@ -112,9 +134,10 @@ private void GetRelatedProducts(bool shallow)
bool hkcrHklmMatch = true;
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (RegistryKey hkcr = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64))
string keyBase = MachineScope ? @"SOFTWARE\Classes" : @"Software\Microsoft";
using (RegistryKey hkmu = RegistryKey.OpenBaseKey(MachineScope ? RegistryHive.LocalMachine : RegistryHive.CurrentUser, RegistryView.Registry64))
{
using (RegistryKey ck = hkcr.OpenSubKey($@"Installer\UpgradeCodes\{obfuscatedUpgradeCode}", false))
using (RegistryKey ck = hkmu.OpenSubKey($@"{keyBase}\Installer\UpgradeCodes\{obfuscatedUpgradeCode}", false))
{
using (RegistryKey mk = hklm.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes\{obfuscatedUpgradeCode}", false))
{
Expand Down Expand Up @@ -210,12 +233,18 @@ public void Prune(ProductInfo product)
if (RelatedProducts.Count > 1)
{
modifier.DeferDeleteValue(RegistryHive.LocalMachine, RegistryView.Registry64, $@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes\{obfuscatedUpgradeCode}", obfuscatedProductCode);
modifier.DeferDeleteValue(RegistryHive.ClassesRoot, RegistryView.Registry64, $@"Installer\UpgradeCodes\{obfuscatedUpgradeCode}", obfuscatedProductCode);

string keyBase = MachineScope ? @"SOFTWARE\Classes" : @"Software\Microsoft";
RegistryHive hiveBase = MachineScope ? RegistryHive.LocalMachine : RegistryHive.CurrentUser;
modifier.DeferDeleteValue(hiveBase, RegistryView.Registry64, $@"{keyBase}\Installer\UpgradeCodes\{obfuscatedUpgradeCode}", obfuscatedProductCode);
}
else
{
modifier.DeferDeleteKey(RegistryHive.LocalMachine, RegistryView.Registry64, $@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes\{obfuscatedUpgradeCode}");
modifier.DeferDeleteKey(RegistryHive.ClassesRoot, RegistryView.Registry64, $@"Installer\UpgradeCodes\{obfuscatedUpgradeCode}");

string keyBase = MachineScope ? @"SOFTWARE\Classes" : @"Software\Microsoft";
RegistryHive hiveBase = MachineScope ? RegistryHive.LocalMachine : RegistryHive.CurrentUser;
modifier.DeferDeleteKey(hiveBase, RegistryView.Registry64, $@"{keyBase}\Installer\UpgradeCodes\{obfuscatedUpgradeCode}");
}
}
}
Expand Down

0 comments on commit 58ea21b

Please sign in to comment.