85 lines
4.9 KiB
C#
85 lines
4.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Principal;
|
|
using System.Text;
|
|
|
|
// Read-only native MSI inventory. Also used by unattended qualification; never
|
|
// enumerate Win32_Product, execute an MSI action, or infer context from HKCU alone.
|
|
public sealed class CompanionInstalledProduct
|
|
{
|
|
public string ProductCode { get; set; }
|
|
public uint Context { get; set; }
|
|
public string UserSid { get; set; }
|
|
public string Version { get; set; }
|
|
public string LocalPackage { get; set; }
|
|
}
|
|
public static class CompanionInstallerInventory
|
|
{
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] private static extern int GetCurrentPackageFullName(ref int length, StringBuilder name);
|
|
public static string CurrentPackage()
|
|
{
|
|
int size = 0; int status = GetCurrentPackageFullName(ref size, null);
|
|
if (status == 15700) return null;
|
|
if (status != 122) throw new Win32Exception(status);
|
|
var value = new StringBuilder(size); status = GetCurrentPackageFullName(ref size, value);
|
|
if (status != 0) throw new Win32Exception(status);
|
|
return value.ToString();
|
|
}
|
|
[DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiEnumProductsEx(string product, string sid, uint context, uint index, StringBuilder code, out uint installedContext, StringBuilder user, ref uint size);
|
|
[DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiGetProductInfoEx(string product, string sid, uint context, string property, StringBuilder value, ref uint size);
|
|
[DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiEnumRelatedProducts(string upgrade, uint reserved, uint index, StringBuilder code);
|
|
[DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern uint MsiQueryFeatureStateEx(string product, string sid, uint context, string feature, out int state);
|
|
public static int FeatureState(CompanionInstalledProduct product, string feature)
|
|
{
|
|
int state; uint status = MsiQueryFeatureStateEx(product.ProductCode, product.UserSid, product.Context, feature, out state);
|
|
if (status != 0) throw new Win32Exception((int)status, "Cannot read Companion MSI feature state.");
|
|
return state;
|
|
}
|
|
public static CompanionInstalledProduct[] Product(string code)
|
|
{
|
|
var result = new List<CompanionInstalledProduct>();
|
|
for (uint index = 0; ; index++)
|
|
{
|
|
var product = new StringBuilder(39); var sid = new StringBuilder(256); uint size = 256, context;
|
|
uint status = MsiEnumProductsEx(code, null, 7, index, product, out context, sid, ref size);
|
|
if (status == 259) break;
|
|
if (status != 0) throw new Win32Exception((int)status, "Cannot enumerate Companion MSI context.");
|
|
string user = context == 4 ? null : sid.ToString();
|
|
result.Add(new CompanionInstalledProduct { ProductCode = product.ToString(), Context = context, UserSid = user, Version = Info(product.ToString(), user, context, "VersionString"), LocalPackage = Info(product.ToString(), user, context, "LocalPackage") });
|
|
}
|
|
return result.ToArray();
|
|
}
|
|
private static string Info(string code, string sid, uint context, string property)
|
|
{
|
|
uint size = 0; uint status = MsiGetProductInfoEx(code, sid, context, property, null, ref size);
|
|
if (status != 234 && status != 0) throw new Win32Exception((int)status, "Cannot read Companion MSI " + property);
|
|
var value = new StringBuilder((int)size + 1); size++;
|
|
status = MsiGetProductInfoEx(code, sid, context, property, value, ref size);
|
|
if (status != 0) throw new Win32Exception((int)status, "Cannot read Companion MSI " + property);
|
|
return value.ToString();
|
|
}
|
|
public static CompanionInstalledProduct[] Family(string upgrade)
|
|
{
|
|
var result = new List<CompanionInstalledProduct>();
|
|
for (uint index = 0; ; index++)
|
|
{
|
|
var code = new StringBuilder(39); uint status = MsiEnumRelatedProducts(upgrade, 0, index, code);
|
|
if (status == 259) break;
|
|
if (status != 0) throw new Win32Exception((int)status, "Cannot enumerate Companion upgrade family.");
|
|
result.AddRange(Product(code.ToString()));
|
|
}
|
|
return result.ToArray();
|
|
}
|
|
public static void RequireCurrentUser(CompanionInstalledProduct[] products, string newestVersion)
|
|
{
|
|
string sid = WindowsIdentity.GetCurrent().User.Value;
|
|
foreach (var product in products)
|
|
{
|
|
if (product.Context != 2 || product.UserSid != sid) throw new InvalidOperationException("Companion is not installed in this user's unmanaged MSI context. No installation was changed.");
|
|
if (new Version(product.Version) > new Version(newestVersion)) throw new InvalidOperationException("A newer Companion is installed. Downgrade was refused before changing it.");
|
|
}
|
|
}
|
|
}
|