Skip to content

Commit

Permalink
MP1-5185: DeployTool: Fix x64 uninstall registry path
Browse files Browse the repository at this point in the history
  • Loading branch information
epbk committed Dec 5, 2023
1 parent 7cb1c14 commit bd60b2c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 32 deletions.
4 changes: 4 additions & 0 deletions Tools/MediaPortal.DeployTool/DeployTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public DeployTool()
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) +
@"\Team MediaPortal\MediaPortal TV Server");


//Fix old MediaPortal x64 registry path
Utils.FixMediaPortal64RegistryPath();

if (Utils.Is64bit())
{
InstallationProperties.Instance.Set("ProgramFiles", Environment.GetEnvironmentVariable("ProgramFiles"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.IO;
using System.Diagnostics;
Expand Down Expand Up @@ -180,14 +182,15 @@ public bool UnInstall()
return true;
}

string[] UninstKeys = {
"MediaPortal" + (Utils.Is64bit() ? " (x64)" : string.Empty), // 1.x - x86/x64
"MediaPortal 0.2.3.0" // 0.2.3.0
};
object[][] keys =
{
new object[] { "MediaPortal", false}, // 1.x - x86/x64
new object[] { "MediaPortal 0.2.3.0", true } // 0.2.3.0
};

foreach (string UnistKey in UninstKeys)
foreach (object[] key in keys)
{
string keyUninstall = Utils.CheckUninstallString(UnistKey, true);
string keyUninstall = Utils.CheckUninstallString((string)key[0], true, (bool)key[1]);
if (keyUninstall != null && File.Exists(keyUninstall))
{
Utils.UninstallNSIS(keyUninstall);
Expand All @@ -214,7 +217,7 @@ public CheckResult CheckStatus()
result.state = CheckState.NOT_INSTALLED;

string[] UninstKeys = {
"MediaPortal" + (Utils.Is64bit() ? " (x64)" : string.Empty), // 1.x - x86/x64
"MediaPortal", // 1.x - x86/x64
"MediaPortal 0.2.3.0" // 0.2.3.0
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public bool UnInstall()
return true;
}

string keyUninstall = Utils.CheckUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), true);
string keyUninstall = Utils.CheckUninstallString("MediaPortal TV Server", true, false);
if (keyUninstall != null && File.Exists(keyUninstall))
{
Utils.UninstallNSIS(keyUninstall);
Expand All @@ -98,7 +98,7 @@ public CheckResult CheckStatus()
result.needsDownload = true;
FileInfo tvPluginFile = new FileInfo(_fileName);

result = Utils.CheckNSISUninstallString("Mediaportal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecClient");
result = Utils.CheckNSISUninstallString("Mediaportal TV Server", "MementoSection_SecClient");

if (tvPluginFile.Exists && tvPluginFile.Length != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public bool UnInstall()
return true;
}

string keyUninstall = Utils.CheckUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), true);
string keyUninstall = Utils.CheckUninstallString("MediaPortal TV Server", true, false);
if (keyUninstall != null && File.Exists(keyUninstall))
{
Utils.UninstallNSIS(keyUninstall);
Expand All @@ -114,7 +114,7 @@ public CheckResult CheckStatus()
result.needsDownload = true;
FileInfo tvServerFile = new FileInfo(_fileName);

result = Utils.CheckNSISUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecServer");
result = Utils.CheckNSISUninstallString("MediaPortal TV Server", "MementoSection_SecServer");

if (tvServerFile.Exists && tvServerFile.Length != 0)
{
Expand All @@ -131,7 +131,7 @@ public CheckResult CheckStatus()
return result;
}

CheckResult already = Utils.CheckNSISUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? string.Empty : " (x64)"), "MementoSection_SecServer");
CheckResult already = Utils.CheckNSISUninstallString("MediaPortal TV Server", "MementoSection_SecServer");
if (already.state != CheckState.NOT_INSTALLED)
{
result.state = CheckState.INSTALLED;
Expand Down
6 changes: 3 additions & 3 deletions Tools/MediaPortal.DeployTool/Sections/DownloadOnlyDlg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public override DeployDialog GetNextDialog()
// Download only
return DialogFlowHandler.Instance.GetDialogInstance(DialogType.DownloadSettings);
}
CheckResult resultTvServer = Utils.CheckNSISUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecServer");
CheckResult resultTvClient = Utils.CheckNSISUninstallString("Mediaportal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecClient");
CheckResult resultTvServer = Utils.CheckNSISUninstallString("MediaPortal TV Server", "MementoSection_SecServer");
CheckResult resultTvClient = Utils.CheckNSISUninstallString("Mediaportal TV Server", "MementoSection_SecClient");
// "NoRepair" key is not the best choice but we need a key returing 1 as value ;)
CheckResult resultMp = Utils.CheckNSISUninstallString("MediaPortal" + (Utils.Is64bit() ? " (x64)" : string.Empty), "NoRepair");
CheckResult resultMp = Utils.CheckNSISUninstallString("MediaPortal", "NoRepair");

bool TvServer = resultTvServer.state != CheckState.NOT_INSTALLED;
bool TvClient = resultTvClient.state != CheckState.NOT_INSTALLED;
Expand Down
12 changes: 6 additions & 6 deletions Tools/MediaPortal.DeployTool/Sections/UpgradeDlg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override void UpdateUI()
string MpBuild = "0";
string MpDisplayVer = string.Empty;

RegistryKey key = Utils.GetUninstallKey("MediaPortal" + (Utils.Is64bit() ? " (x64)" : string.Empty));
RegistryKey key = Utils.GetUninstallKey("MediaPortal", false);
if (key != null)
{
MpBuild = key.GetValue("VersionBuild").ToString();
Expand All @@ -96,7 +96,7 @@ public override void UpdateUI()
string Tv3Build = "0";
string Tv3DisplayVer = string.Empty;

key = Utils.GetUninstallKey("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty));
key = Utils.GetUninstallKey("MediaPortal TV Server", false);
if (key != null)
{
Tv3Build = key.GetValue("VersionBuild").ToString();
Expand Down Expand Up @@ -248,8 +248,8 @@ private void SelectUpdate()
freshForce = false;
InstallationProperties.Instance.Set("UpdateMode", "yes");

CheckResult resultTvServer = Utils.CheckNSISUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecServer");
CheckResult resultTvClient = Utils.CheckNSISUninstallString("Mediaportal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecClient");
CheckResult resultTvServer = Utils.CheckNSISUninstallString("MediaPortal TV Server", "MementoSection_SecServer");
CheckResult resultTvClient = Utils.CheckNSISUninstallString("Mediaportal TV Server", "MementoSection_SecClient");

bool TvServer = resultTvServer.state != CheckState.NOT_INSTALLED;
bool TvClient = resultTvClient.state != CheckState.NOT_INSTALLED;
Expand All @@ -272,8 +272,8 @@ private void SelectReinstall()
freshForce = false;
InstallationProperties.Instance.Set("UpdateMode", "yes");

CheckResult resultTvServer = Utils.CheckNSISUninstallString("MediaPortal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecServer");
CheckResult resultTvClient = Utils.CheckNSISUninstallString("Mediaportal TV Server" + (Utils.Is64bit() ? " (x64)" : string.Empty), "MementoSection_SecClient");
CheckResult resultTvServer = Utils.CheckNSISUninstallString("MediaPortal TV Server", "MementoSection_SecServer");
CheckResult resultTvClient = Utils.CheckNSISUninstallString("Mediaportal TV Server", "MementoSection_SecClient");

bool TvServer = resultTvServer.state != CheckState.NOT_INSTALLED;
bool TvClient = resultTvClient.state != CheckState.NOT_INSTALLED;
Expand Down
72 changes: 61 additions & 11 deletions Tools/MediaPortal.DeployTool/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,15 @@ private static RegistryKey PointerToRegistryKey(IntPtr hKey, bool pWritable, boo
return result;
}

public static RegistryKey LMOpenSubKey(string keyPath, bool writable = false)
public static RegistryKey LMOpenSubKey(string keyPath, bool writable = false, bool bIncludeWow6432 = true)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(keyPath, writable);

if (key == null)
{
if (!bIncludeWow6432)
return null;

try
{
key = OpenSubKey(Registry.LocalMachine, keyPath, writable, eRegWow64Options.KEY_WOW64_32KEY);
Expand Down Expand Up @@ -392,14 +395,14 @@ public static void UninstallMSI(string clsid)
CheckUninstallString(clsid, true);
}

public static RegistryKey GetUninstallKey(string clsid)
public static RegistryKey GetUninstallKey(string clsid, bool bIncludeWow6432 = true)
{
return LMOpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + clsid);
return LMOpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + clsid, bIncludeWow6432: bIncludeWow6432);
}

public static string CheckUninstallString(string clsid, string section)
public static string CheckUninstallString(string clsid, string section, bool bIncludeWow6432 = true)
{
RegistryKey key = GetUninstallKey(clsid);
RegistryKey key = GetUninstallKey(clsid, bIncludeWow6432);
if (key != null)
{
string strSection = key.GetValue(section).ToString();
Expand All @@ -413,9 +416,9 @@ public static string CheckUninstallString(string clsid, string section)
return null;
}

public static string CheckUninstallString(string clsid, bool delete)
public static string CheckUninstallString(string clsid, bool delete, bool bIncludeWow6432 = true)
{
string strUninstall = CheckUninstallString(clsid, "UninstallString");
string strUninstall = CheckUninstallString(clsid, "UninstallString", bIncludeWow6432);
if (!string.IsNullOrEmpty(strUninstall))
{
if (File.Exists(strUninstall))
Expand All @@ -426,7 +429,7 @@ public static string CheckUninstallString(string clsid, bool delete)

if (delete)
{
RegistryKey key = GetUninstallKey(clsid);
RegistryKey key = GetUninstallKey(clsid, bIncludeWow6432);
if (key != null)
{
key.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + clsid);
Expand All @@ -443,7 +446,7 @@ public static CheckResult CheckNSISUninstallString(string RegistryPath, string M
state = CheckState.NOT_INSTALLED
};

RegistryKey key = GetUninstallKey(RegistryPath);
RegistryKey key = GetUninstallKey(RegistryPath, false);
if (key != null)
{
int _IsInstalled = (int)key.GetValue(MementoSection, 0);
Expand Down Expand Up @@ -809,7 +812,7 @@ public static bool IsCurrentPackageUpdatabled(Version pkgVer)

public static string PathFromRegistry(string regkey)
{
RegistryKey key = LMOpenSubKey(regkey);
RegistryKey key = LMOpenSubKey(regkey, bIncludeWow6432: false);

string Tv3Path = null;
if (key != null)
Expand All @@ -823,7 +826,7 @@ public static string PathFromRegistry(string regkey)

public static Version VersionFromRegistry(string regkey)
{
RegistryKey key = LMOpenSubKey(regkey);
RegistryKey key = LMOpenSubKey(regkey, bIncludeWow6432: false);

int major = 0;
int minor = 0;
Expand Down Expand Up @@ -890,6 +893,53 @@ public static void SetDeployXml(string section, string entry, string value)
deployXml.Save(deployXmlLocation);
}

/// <summary>
/// Fixes MediaPortal 1.32 uninstall registry path
/// </summary>
public static void FixMediaPortal64RegistryPath()
{
if (Is64bit())
{
const string NAME_OLD = "MediaPortal (x64)";
const string PATH_UNINSTALL = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
const string PATH_UNINSTALL_WOW6432 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
const string PATH_MP = PATH_UNINSTALL + @"\MediaPortal";
const string PATH_MP_OLD = PATH_UNINSTALL_WOW6432 + @"\" + NAME_OLD;

RegistryKey keyOld = Registry.LocalMachine.OpenSubKey(PATH_MP_OLD);
if (keyOld != null)
{
//Existing old registry path

RegistryKey key = Registry.LocalMachine.OpenSubKey(PATH_MP);

if (key == null)
{
//Create new registry path
using (RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default))
{
key = localKey.CreateSubKey(PATH_MP);
}
}

//Copy all values from the old key to the new key
foreach (string strName in keyOld.GetValueNames())
{
key.SetValue(strName, keyOld.GetValue(strName), keyOld.GetValueKind(strName));
}

//Delete old key
using (RegistryKey k = Registry.LocalMachine.OpenSubKey(PATH_UNINSTALL_WOW6432, true))
{
k.DeleteSubKeyTree(NAME_OLD);
}

key.Dispose();
keyOld.Dispose();
}
}
}

#endregion
}
}

0 comments on commit bd60b2c

Please sign in to comment.