关于wix:从PackageID获取显示名称

Getting Display Name from PackageID

浏览Wix Standard Bootstrapper应用程序的源代码,似乎每个软件包都有一个DisplayName属性:

1
pPackage->sczDisplayName

但是,WiX安装程序项目中使用的BootstrapperCore dll没有此属性。有什么方法可以从托管代码中的捆绑软件中提取此属性?


我将Bal代码移植到C#中,试图使其完全像C代码一样工作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.XPath;

public class BootstrapperApplicationData
{
    public const string defaultFileName ="BootstrapperApplicationData.xml";
    public const string xmlNamespace =
       "http://schemas.microsoft.com/wix/2010/BootstrapperApplicationData";

    private static DirectoryInfo defaultFolder;
    public static DirectoryInfo DefaultFolder
    {
        get
        {
            if (defaultFolder == null)
            {
                defaultFolder = (new FileInfo(Assembly.GetExecutingAssembly().Location)).Directory;
            }
            return defaultFolder;
        }
    }

    private static FileInfo defaultFile;
    public static FileInfo DefaultFile
    {
        get
        {
            if (defaultFile == null)
            {
                defaultFile = new FileInfo(Path.Combine(DefaultFolder.FullName, defaultFileName));
            }
            return defaultFile;
        }
    }

    public FileInfo DataFile { get; protected set; }
    public Bundle Data { get; protected set; }

    public BootstrapperApplicationData() : this(DefaultFile) { }

    public BootstrapperApplicationData(FileInfo fiBootstrapperApplicationData)
    {
        DataFile = fiBootstrapperApplicationData;
        using (FileStream fs = DataFile.OpenRead())
        {
            Data = ParseBundleFromStream(fs);
        }
    }

    public static Bundle ParseBundleFromStream(Stream stream)
    {
        XPathDocument manifest = new XPathDocument(stream);
        XPathNavigator root = manifest.CreateNavigator();
        return ParseBundleFromXml(root);
    }

    public static Bundle ParseBundleFromXml(XPathNavigator root)
    {
        Bundle bundle = new Bundle();

        XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable);
        namespaceManager.AddNamespace("p", xmlNamespace);
        XPathNavigator bundleNode = root.SelectSingleNode("/p:BootstrapperApplicationData/p:WixBundleProperties", namespaceManager);

        if (bundleNode == null)
        {
            throw new Exception("Failed to select bundle information");
        }

        bool? perMachine = GetYesNoAttribute(bundleNode,"PerMachine");
        if (perMachine.HasValue)
        {
            bundle.PerMachine = perMachine.Value;
        }

        string name = GetAttribute(bundleNode,"DisplayName");
        if (name != null)
        {
            bundle.Name = name;
        }

        string logVariable = GetAttribute(bundleNode,"LogPathVariable");
        if (logVariable != null)
        {
            bundle.LogVariable = logVariable;
        }
        else
        {
            //wix would actually debug"Failed to select bundle information" and return with E_NOTFOUND, but I think it's a (harmless) bug
        }

        Package[] packages = ParsePackagesFromXml(root);
        bundle.Packages = packages;

        return bundle;
    }

    public static Package[] ParsePackagesFromXml(XPathNavigator root)
    {
        List<Package> packages = new List<Package>();

        XmlNamespaceManager namespaceManager = new XmlNamespaceManager(root.NameTable);
        namespaceManager.AddNamespace("p", xmlNamespace);
        XPathNodeIterator nodes = root.Select("/p:BootstrapperApplicationData/p:WixPackageProperties", namespaceManager);

        foreach (XPathNavigator node in nodes)
        {
            Package package = new Package();

            string id = GetAttribute(node,"Package");
            if (id == null)
            {
                throw new Exception("Failed to get package identifier for package");
            }
            package.Id = id;

            string displayName = GetAttribute(node,"DisplayName");
            if (displayName != null)
            {
                package.DisplayName = displayName;
            }

            string description = GetAttribute(node,"Description");
            if (description != null)
            {
                package.Description = description;
            }

            PackageType? packageType = GetPackageTypeAttribute(node,"PackageType");
            if (!packageType.HasValue)
            {
                throw new Exception("Failed to get package type for package");
            }
            package.Type = packageType.Value;

            bool? permanent = GetYesNoAttribute(node,"Permanent");
            if (!permanent.HasValue)
            {
                throw new Exception("Failed to get permanent settings for package");
            }
            package.Permanent = permanent.Value;

            bool? vital = GetYesNoAttribute(node,"Vital");
            if (!vital.HasValue)
            {
                throw new Exception("Failed to get vital setting for package");
            }
            package.Vital = vital.Value;

            bool? displayInternalUI = GetYesNoAttribute(node,"DisplayInternalUI");
            if (!displayInternalUI.HasValue)
            {
                throw new Exception("Failed to get DisplayInternalUI setting for package");
            }
            package.DisplayInternalUI = displayInternalUI.Value;

            string productCode = GetAttribute(node,"ProductCode");
            if (productCode != null)
            {
                package.ProductCode = productCode;
            }

            string upgradeCode = GetAttribute(node,"UpgradeCode");
            if (upgradeCode != null)
            {
                package.UpgradeCode = upgradeCode;
            }

            string version = GetAttribute(node,"Version");
            if (version != null)
            {
                package.Version = version;
            }

            packages.Add(package);
        }

        return packages.ToArray();
    }

    public static string GetAttribute(XPathNavigator node, string attributeName)
    {
        XPathNavigator attribute = node.SelectSingleNode("@" + attributeName);

        if (attribute == null)
        {
            return null;
        }

        return attribute.Value;
    }

    public static bool? GetYesNoAttribute(XPathNavigator node, string attributeName)
    {
        string attributeValue = GetAttribute(node, attributeName);

        if (attributeValue == null)
        {
            return null;
        }

        return attributeValue.Equals("yes", StringComparison.InvariantCulture);
    }

    public static PackageType? GetPackageTypeAttribute(XPathNavigator node, string attributeName)
    {
        string attributeValue = GetAttribute(node, attributeName);

        if (attributeValue == null)
        {
            return null;
        }

        if (attributeValue.Equals("Exe", StringComparison.InvariantCulture))
        {
            return PackageType.EXE;
        }
        else if (attributeValue.Equals("Msi", StringComparison.InvariantCulture))
        {
            return PackageType.MSI;
        }
        else if (attributeValue.Equals("Msp", StringComparison.InvariantCulture))
        {
            return PackageType.MSP;
        }
        else if (attributeValue.Equals("Msu", StringComparison.InvariantCulture))
        {
            return PackageType.MSU;
        }
        else
        {
            return 0;
        }
    }

    public enum PackageType
    {
        EXE,
        MSI,
        MSP,
        MSU,
    }

    public class Package
    {
        public string Id;
        public string DisplayName;
        public string Description;
        public PackageType Type;
        public bool Permanent;
        public bool Vital;
        public bool DisplayInternalUI;

        //not available until WiX 3.9.421.0
        public string ProductCode;
        public string UpgradeCode;
        public string Version;
    }

    public class Bundle
    {
        public bool PerMachine;
        public string Name;
        public string LogVariable;
        public Package[] Packages;
    }
}

在生成过程中生成的BootstrapperApplicationData.xml文件位于BA .dll的旁边。您可以加载该XML文件以获取有关捆绑软件和捆绑软件中软件包的大量信息。

要以本机代码加载BootstrapperApplicationData.xml,请使用WiX工具集随附的balutil.lib中的BalManifestLoad()方法。您可以在src\\ext\\BalExtension\\balutil\\balutil.cpp中查看代码。然后,您也可以在balutil.lib中使用BalInfoParseFromXml()将XML文件解析为一堆方便的结构。您可以在src\\ext\\BalExtension\\balutil\\balinfo.cpp中看到代码。