From ae7e9817bb10d635e031e51496f2e529595a9cfe Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sun, 11 Apr 2021 12:23:19 -0700 Subject: Add RemoveRegistryKey --- src/ca/RemoveRegistryKeysEx.cpp | 114 +++++++++++++++++++++ src/ca/utilca.def | 2 + src/ca/utilca.vcxproj | 1 + .../TestData/RemoveRegistryKeyEx/Module.wxs | 13 +++ .../RemoveRegistryKeyEx/ModuleComponents.wxs | 10 ++ .../WixToolsetTest.Util/UtilExtensionFixture.cs | 15 +++ src/wixext/Symbols/UtilSymbolDefinitions.cs | 4 + src/wixext/Symbols/WixRemoveRegistryKeyExSymbol.cs | 86 ++++++++++++++++ src/wixext/UtilCompiler.cs | 103 +++++++++++++++++-- src/wixext/UtilTableDefinitions.cs | 16 +++ src/wixlib/UtilExtension_Platform.wxi | 8 ++ 11 files changed, 366 insertions(+), 6 deletions(-) create mode 100644 src/ca/RemoveRegistryKeysEx.cpp create mode 100644 src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/Module.wxs create mode 100644 src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/ModuleComponents.wxs create mode 100644 src/wixext/Symbols/WixRemoveRegistryKeyExSymbol.cs (limited to 'src') diff --git a/src/ca/RemoveRegistryKeysEx.cpp b/src/ca/RemoveRegistryKeysEx.cpp new file mode 100644 index 00000000..478c0779 --- /dev/null +++ b/src/ca/RemoveRegistryKeysEx.cpp @@ -0,0 +1,114 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +#include "precomp.h" + +LPCWSTR vcsRemoveRegistryKeyExQuery = + L"SELECT `Wix4RemoveRegistryKeyEx`, `Component_`, `Root`, `Key`, `InstallMode`, `Condition` FROM `Wix4RemoveRegistryKeyEx`"; +enum eRemoveRegistryKeyExQuery { rrxqId = 1, rrxqComponent, rrxqRoot, rrxqKey, rrxqMode, rrxqCondition }; + +extern "C" UINT WINAPI WixRemoveRegistryKeysEx( + __in MSIHANDLE hInstall +) +{ + //AssertSz(FALSE, "debug WixRemoveRegistryKeyEx"); + + HRESULT hr = S_OK; + PMSIHANDLE hView; + PMSIHANDLE hRec; + LPWSTR sczId = NULL; + LPWSTR sczComponent = NULL; + LPWSTR sczCondition = NULL; + LPWSTR sczKey = NULL; + int iRoot = 0; + int iMode = 0; + MSIHANDLE hTable = NULL; + MSIHANDLE hColumns = NULL; + + hr = WcaInitialize(hInstall, __FUNCTION__); + ExitOnFailure(hr, "Failed to initialize " __FUNCTION__); + + // anything to do? + if (S_OK != WcaTableExists(L"Wix4RemoveRegistryKeyEx")) + { + WcaLog(LOGMSG_STANDARD, "Wix4RemoveRegistryKeyEx table doesn't exist, so there are no registry keys to remove."); + ExitFunction(); + } + + hr = WcaOpenExecuteView(vcsRemoveRegistryKeyExQuery, &hView); + ExitOnFailure(hr, "Failed to open view on Wix4RemoveRegistryKeyEx table"); + + while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) + { + hr = WcaGetRecordString(hRec, rrxqId, &sczId); + ExitOnFailure(hr, "Failed to get Wix4RemoveRegistryKeyEx identity."); + + hr = WcaGetRecordString(hRec, rrxqCondition, &sczCondition); + ExitOnFailure(hr, "Failed to get Wix4RemoveRegistryKeyEx condition."); + + if (sczCondition && *sczCondition) + { + MSICONDITION condition = ::MsiEvaluateConditionW(hInstall, sczCondition); + if (MSICONDITION_TRUE == condition) + { + WcaLog(LOGMSG_STANDARD, "True condition for row %S: %S; processing.", sczId, sczCondition); + } + else + { + WcaLog(LOGMSG_STANDARD, "False or invalid condition for row %S: %S; skipping.", sczId, sczCondition); + continue; + } + } + + hr = WcaGetRecordString(hRec, rrxqComponent, &sczComponent); + ExitOnFailure(hr, "Failed to get Wix4RemoveRegistryKeyEx component."); + + hr = WcaGetRecordInteger(hRec, rrxqRoot, &iRoot); + ExitOnFailure(hr, "Failed to get Wix4RemoveRegistryKeyEx root."); + + hr = WcaGetRecordString(hRec, rrxqKey, &sczKey); + ExitOnFailure(hr, "Failed to get Wix4RemoveRegistryKeyEx key."); + + hr = WcaGetRecordInteger(hRec, rrxqMode, &iMode); + ExitOnFailure(hr, "Failed to get Wix4RemoveRegistryKeyEx mode."); + + switch (iMode) + { + case 1: // remove on install + WcaLog(LOGMSG_STANDARD, "Adding RemoveRegistry row: %ls/%d/%ls/-/%ls", sczId, iRoot, sczKey, sczComponent); + hr = WcaAddTempRecord(&hTable, &hColumns, L"RemoveRegistry", NULL, 0, 5, sczId, iRoot, sczKey, L"-", sczComponent); + ExitOnFailure(hr, "Failed to add RemoveRegistry row for remove-on-install Wix4RemoveRegistryKeyEx row: %ls:", sczId); + break; + case 2: // remove on uninstall + WcaLog(LOGMSG_STANDARD, "Adding Registry row: %ls/%d/%ls/-/null/%ls", sczId, iRoot, sczKey, sczComponent); + hr = WcaAddTempRecord(&hTable, &hColumns, L"Registry", NULL, 0, 6, sczId, iRoot, sczKey, L"-", NULL, sczComponent); + ExitOnFailure(hr, "Failed to add Registry row for remove-on-uninstall Wix4RemoveRegistryKeyEx row: %ls:", sczId); + break; + } + } + + // reaching the end of the list is actually a good thing, not an error + if (E_NOMOREITEMS == hr) + { + hr = S_OK; + } + ExitOnFailure(hr, "Failure occured while processing Wix4RemoveRegistryKeyEx table."); + +LExit: + if (hColumns) + { + ::MsiCloseHandle(hColumns); + } + + if (hTable) + { + ::MsiCloseHandle(hTable); + } + + ReleaseStr(sczKey); + ReleaseStr(sczComponent); + ReleaseStr(sczCondition); + ReleaseStr(sczId); + + DWORD er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE; + return WcaFinalize(er); +} diff --git a/src/ca/utilca.def b/src/ca/utilca.def index 97d5776f..337c3a68 100644 --- a/src/ca/utilca.def +++ b/src/ca/utilca.def @@ -35,6 +35,8 @@ EXPORTS WixSilentExec64 ; RemoveFoldersEx.cpp WixRemoveFoldersEx +; RemoveRegistryKeysEx.cpp + WixRemoveRegistryKeysEx ;scaexec.cpp RegisterPerfCounterData UnregisterPerfCounterData diff --git a/src/ca/utilca.vcxproj b/src/ca/utilca.vcxproj index 4fafba68..6a076f2f 100644 --- a/src/ca/utilca.vcxproj +++ b/src/ca/utilca.vcxproj @@ -59,6 +59,7 @@ + diff --git a/src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/Module.wxs b/src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/Module.wxs new file mode 100644 index 00000000..32b246f4 --- /dev/null +++ b/src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/Module.wxs @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/ModuleComponents.wxs b/src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/ModuleComponents.wxs new file mode 100644 index 00000000..0a0c8cb6 --- /dev/null +++ b/src/test/WixToolsetTest.Util/TestData/RemoveRegistryKeyEx/ModuleComponents.wxs @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/test/WixToolsetTest.Util/UtilExtensionFixture.cs b/src/test/WixToolsetTest.Util/UtilExtensionFixture.cs index 9c32ebc2..883f9794 100644 --- a/src/test/WixToolsetTest.Util/UtilExtensionFixture.cs +++ b/src/test/WixToolsetTest.Util/UtilExtensionFixture.cs @@ -129,6 +129,21 @@ namespace WixToolsetTest.Util }, results.OrderBy(s => s).ToArray()); } + [Fact] + public void CanBuildRemoveRegistryKeyExInMergeModule() + { + var folder = TestData.Get(@"TestData", "RemoveRegistryKeyEx"); + var build = new Builder(folder, typeof(UtilExtensionFactory), new[] { folder }, "test.msm"); + + var results = build.BuildAndQuery(BuildX64, "Binary", "CustomAction", "RemoveRegistry", "Wix4RemoveRegistryKeyEx"); + WixAssert.CompareLineByLine(new[] + { + "Binary:Wix4UtilCA_X64.047730A5_30FE_4A62_A520_DA9381B8226A\t[Binary data]", + "CustomAction:Wix4RemoveRegistryKeysEx_X64.047730A5_30FE_4A62_A520_DA9381B8226A\t65\tWix4UtilCA_X64.047730A5_30FE_4A62_A520_DA9381B8226A\tWixRemoveRegistryKeysEx\t", + "Wix4RemoveRegistryKeyEx:rrxfcDhR4HhE3v3rYiQcNtQjyahQNg.047730A5_30FE_4A62_A520_DA9381B8226A\tfilh4juyUVjoUcWWtcQmd5L07FoON4.047730A5_30FE_4A62_A520_DA9381B8226A\t2\tSOFTWARE\\Example\t1\t", + }, results.OrderBy(s => s).ToArray()); + } + [Fact] public void CanBuildRemoveFolderExInMergeModule() { diff --git a/src/wixext/Symbols/UtilSymbolDefinitions.cs b/src/wixext/Symbols/UtilSymbolDefinitions.cs index 5f062676..72091c3b 100644 --- a/src/wixext/Symbols/UtilSymbolDefinitions.cs +++ b/src/wixext/Symbols/UtilSymbolDefinitions.cs @@ -23,6 +23,7 @@ namespace WixToolset.Util WixFormatFiles, WixInternetShortcut, WixRemoveFolderEx, + WixRemoveRegistryKeyEx, WixRestartResource, WixTouchFile, WixWindowsFeatureSearch, @@ -93,6 +94,9 @@ namespace WixToolset.Util case UtilSymbolDefinitionType.WixRemoveFolderEx: return UtilSymbolDefinitions.WixRemoveFolderEx; + case UtilSymbolDefinitionType.WixRemoveRegistryKeyEx: + return UtilSymbolDefinitions.WixRemoveRegistryKeyEx; + case UtilSymbolDefinitionType.WixRestartResource: return UtilSymbolDefinitions.WixRestartResource; diff --git a/src/wixext/Symbols/WixRemoveRegistryKeyExSymbol.cs b/src/wixext/Symbols/WixRemoveRegistryKeyExSymbol.cs new file mode 100644 index 00000000..8e4bd212 --- /dev/null +++ b/src/wixext/Symbols/WixRemoveRegistryKeyExSymbol.cs @@ -0,0 +1,86 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Util +{ + using WixToolset.Data; + using WixToolset.Util.Symbols; + + public static partial class UtilSymbolDefinitions + { + public static readonly IntermediateSymbolDefinition WixRemoveRegistryKeyEx = new IntermediateSymbolDefinition( + UtilSymbolDefinitionType.WixRemoveRegistryKeyEx.ToString(), + new[] + { + new IntermediateFieldDefinition(nameof(WixRemoveRegistryKeyExSymbolFields.ComponentRef), IntermediateFieldType.String), + new IntermediateFieldDefinition(nameof(WixRemoveRegistryKeyExSymbolFields.Root), IntermediateFieldType.Number), + new IntermediateFieldDefinition(nameof(WixRemoveRegistryKeyExSymbolFields.Key), IntermediateFieldType.String), + new IntermediateFieldDefinition(nameof(WixRemoveRegistryKeyExSymbolFields.InstallMode), IntermediateFieldType.Number), + new IntermediateFieldDefinition(nameof(WixRemoveRegistryKeyExSymbolFields.Condition), IntermediateFieldType.String), + }, + typeof(WixRemoveRegistryKeyExSymbol)); + } +} + +namespace WixToolset.Util.Symbols +{ + using WixToolset.Data; + using WixToolset.Data.Symbols; + + public enum WixRemoveRegistryKeyExSymbolFields + { + ComponentRef, + Root, + Key, + InstallMode, + Condition, + } + + public enum WixRemoveRegistryKeyExInstallMode + { + Install = 1, + Uninstall = 2, + } + + public class WixRemoveRegistryKeyExSymbol : IntermediateSymbol + { + public WixRemoveRegistryKeyExSymbol() : base(UtilSymbolDefinitions.WixRemoveRegistryKeyEx, null, null) + { + } + + public WixRemoveRegistryKeyExSymbol(SourceLineNumber sourceLineNumber, Identifier id = null) : base(UtilSymbolDefinitions.WixRemoveRegistryKeyEx, sourceLineNumber, id) + { + } + + public IntermediateField this[WixRemoveRegistryKeyExSymbolFields index] => this.Fields[(int)index]; + + public string ComponentRef + { + get => this.Fields[(int)WixRemoveRegistryKeyExSymbolFields.ComponentRef].AsString(); + set => this.Set((int)WixRemoveRegistryKeyExSymbolFields.ComponentRef, value); + } + + public RegistryRootType Root + { + get => (RegistryRootType)this.Fields[(int)WixRemoveRegistryKeyExSymbolFields.Root].AsNumber(); + set => this.Set((int)WixRemoveRegistryKeyExSymbolFields.Root, (int)value); + } + + public string Key + { + get => (string)this.Fields[(int)WixRemoveRegistryKeyExSymbolFields.Key]; + set => this.Set((int)WixRemoveRegistryKeyExSymbolFields.Key, value); + } + + public WixRemoveRegistryKeyExInstallMode InstallMode + { + get => (WixRemoveRegistryKeyExInstallMode)this.Fields[(int)WixRemoveRegistryKeyExSymbolFields.InstallMode].AsNumber(); + set => this.Set((int)WixRemoveRegistryKeyExSymbolFields.InstallMode, (int)value); + } + + public string Condition + { + get => this.Fields[(int)WixRemoveRegistryKeyExSymbolFields.Condition].AsString(); + set => this.Set((int)WixRemoveRegistryKeyExSymbolFields.Condition, value); + } + } +} diff --git a/src/wixext/UtilCompiler.cs b/src/wixext/UtilCompiler.cs index 63f2b469..45079150 100644 --- a/src/wixext/UtilCompiler.cs +++ b/src/wixext/UtilCompiler.cs @@ -35,12 +35,6 @@ namespace WixToolset.Util internal const int UserDontCreateUser = 0x00000200; internal const int UserNonVital = 0x00000400; - internal enum WixRegistrySearchFormat - { - Raw, - Compatible, - } - private static readonly Regex FindPropertyBrackets = new Regex(@"\[(?!\\|\])|(? "http://wixtoolset.org/schemas/v4/wxs/util"; @@ -135,6 +129,9 @@ namespace WixToolset.Util case "RemoveFolderEx": this.ParseRemoveFolderExElement(intermediate, section, element, componentId); break; + case "RemoveRegistryKey": + this.ParseRemoveRegistryKeyExElement(intermediate, section, element, componentId); + break; case "RestartResource": this.ParseRestartResourceElement(intermediate, section, element, componentId); break; @@ -2887,6 +2884,100 @@ namespace WixToolset.Util } } + /// + /// Parses a RemoveRegistryKeyEx element. + /// + /// Element to parse. + /// Identifier of parent component. + private void ParseRemoveRegistryKeyExElement(Intermediate intermediate, IntermediateSection section, XElement element, string componentId) + { + var sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(element); + Identifier id = null; + var mode = WixRemoveRegistryKeyExInstallMode.Uninstall; + string condition = null; + RegistryRootType? root = null; + string key = null; + + foreach (var attrib in element.Attributes()) + { + if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace) + { + switch (attrib.Name.LocalName) + { + case "Condition": + condition = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "Id": + id = this.ParseHelper.GetAttributeIdentifier(sourceLineNumbers, attrib); + break; + case "On": + var actionValue = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib); + switch (actionValue) + { + case "": + break; + case "install": + mode = WixRemoveRegistryKeyExInstallMode.Install; + break; + case "uninstall": + mode = WixRemoveRegistryKeyExInstallMode.Uninstall; + break; + default: + this.Messaging.Write(ErrorMessages.IllegalAttributeValue(sourceLineNumbers, element.Name.LocalName, "On", actionValue, "install", "uninstall")); + break; + } + break; + case "Root": + root = this.ParseHelper.GetAttributeRegistryRootValue(sourceLineNumbers, attrib, false); + break; + case "Key": + key = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib); + break; + default: + this.ParseHelper.UnexpectedAttribute(element, attrib); + break; + } + } + else + { + this.ParseHelper.ParseExtensionAttribute(this.Context.Extensions, intermediate, section, element, attrib); + } + } + + if (!root.HasValue) + { + this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "Root")); + } + + if (key == null) + { + this.Messaging.Write(ErrorMessages.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "Key")); + } + + if (id == null) + { + id = this.ParseHelper.CreateIdentifier("rrx", componentId, condition, root.ToString(), key, mode.ToString()); + } + + this.ParseHelper.ParseForExtensionElements(this.Context.Extensions, intermediate, section, element); + + if (!this.Messaging.EncounteredError) + { + this.ParseHelper.EnsureTable(section, sourceLineNumbers, "Registry"); + this.ParseHelper.EnsureTable(section, sourceLineNumbers, "RemoveRegistry"); + this.ParseHelper.CreateCustomActionReference(sourceLineNumbers, section, "Wix4RemoveRegistryKeysEx", this.Context.Platform, CustomActionPlatforms.X86 | CustomActionPlatforms.X64 | CustomActionPlatforms.ARM64); + + section.AddSymbol(new WixRemoveRegistryKeyExSymbol(sourceLineNumbers, id) + { + ComponentRef = componentId, + Root = root.Value, + Key = key, + InstallMode = mode, + Condition = condition + }); + } + } + /// /// Parses a RestartResource element. /// diff --git a/src/wixext/UtilTableDefinitions.cs b/src/wixext/UtilTableDefinitions.cs index fd09367a..12f423cc 100644 --- a/src/wixext/UtilTableDefinitions.cs +++ b/src/wixext/UtilTableDefinitions.cs @@ -38,6 +38,21 @@ namespace WixToolset.Util symbolIdIsPrimaryKey: true ); + public static readonly TableDefinition Wix4RemoveRegistryKeyEx = new TableDefinition( + "Wix4RemoveRegistryKeyEx", + UtilSymbolDefinitions.WixRemoveRegistryKeyEx, + new[] + { + new ColumnDefinition("Wix4RemoveRegistryKeyEx", ColumnType.String, 72, primaryKey: true, nullable: false, ColumnCategory.Identifier, description: "Identifier for the Wix4RemoveRegistryKeyEx row in the package.", modularizeType: ColumnModularizeType.Column), + new ColumnDefinition("Component_", ColumnType.String, 72, primaryKey: false, nullable: false, ColumnCategory.Identifier, keyTable: "Component", keyColumn: 1, description: "Foreign key into the Component table used to determine install state", modularizeType: ColumnModularizeType.Column), + new ColumnDefinition("Root", ColumnType.Number, 2, primaryKey: false, nullable: false, ColumnCategory.Unknown, minValue: -1, maxValue: 3, description: "The predefined root key for the registry value, one of rrkEnum."), + new ColumnDefinition("Key", ColumnType.Localized, 255, primaryKey: false, nullable: false, ColumnCategory.RegPath, description: "The key for the registry value.", modularizeType: ColumnModularizeType.Property), + new ColumnDefinition("InstallMode", ColumnType.Number, 2, primaryKey: false, nullable: false, ColumnCategory.Unknown, minValue: 1, maxValue: 3, description: "1 == Remove only when the associated component is being installed (msiInstallStateLocal or msiInstallStateSource), 2 == Remove only when the associated component is being removed (msiInstallStateAbsent), 3 = Remove in either of the above cases."), + new ColumnDefinition("Condition", ColumnType.String, 0, primaryKey: false, nullable: true, ColumnCategory.Condition, description: "Optional expression to control whether the registry key is removed.", modularizeType: ColumnModularizeType.Condition, forceLocalizable: true), + }, + symbolIdIsPrimaryKey: true + ); + public static readonly TableDefinition Wix4RestartResource = new TableDefinition( "Wix4RestartResource", UtilSymbolDefinitions.WixRestartResource, @@ -281,6 +296,7 @@ namespace WixToolset.Util { Wix4CloseApplication, Wix4RemoveFolderEx, + Wix4RemoveRegistryKeyEx, Wix4RestartResource, Wix4FileShare, Wix4FileSharePermissions, diff --git a/src/wixlib/UtilExtension_Platform.wxi b/src/wixlib/UtilExtension_Platform.wxi index 974169ff..d88b2a57 100644 --- a/src/wixlib/UtilExtension_Platform.wxi +++ b/src/wixlib/UtilExtension_Platform.wxi @@ -44,6 +44,14 @@ + + + + + + + + -- cgit v1.2.3-55-g6feb