forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegFreeComManifest.cs
65 lines (57 loc) · 2.79 KB
/
RegFreeComManifest.cs
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.NET.Build.Tasks
{
internal static class RegFreeComManifest
{
/// <summary>
/// Generates a side-by-side application manifest to enable reg-free COM.
/// </summary>
/// <param name="assemblyName">The name of the assembly.</param>
/// <param name="comHostName">The name of the comhost library.</param>
/// <param name="assemblyVersion">The version of the assembly.</param>
/// <param name="clsidMapPath">The path to the clsidmap file.</param>
/// <param name="comManifestPath">The path to which to write the manifest.</param>
public static void CreateManifestFromClsidmap(string assemblyName, string comHostName, string assemblyVersion, string clsidMapPath, string comManifestPath)
{
XNamespace ns = "urn:schemas-microsoft-com:asm.v1";
XElement manifest = new XElement(ns + "assembly", new XAttribute("manifestVersion", "1.0"));
manifest.Add(new XElement(ns + "assemblyIdentity",
new XAttribute("type", "win32"),
new XAttribute("name", $"{assemblyName}.X"),
new XAttribute("version", assemblyVersion)));
XElement fileElement = new XElement(ns + "file", new XAttribute("name", comHostName));
JObject clsidMap;
string clsidMapText = File.ReadAllText(clsidMapPath);
using (StreamReader clsidMapReader = File.OpenText(clsidMapPath))
using (JsonTextReader jsonReader = new JsonTextReader(clsidMapReader))
{
clsidMap = JObject.Load(jsonReader);
}
foreach (JProperty property in clsidMap.Properties())
{
string guidMaybe = property.Name;
Guid guid = Guid.Parse(guidMaybe);
fileElement.Add(new XElement(ns + "comClass", new XAttribute("clsid", guid.ToString("B")), new XAttribute("threadingModel", "Both")));
}
manifest.Add(fileElement);
XDocument manifestDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), manifest);
XmlWriterSettings settings = new XmlWriterSettings()
{
Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)
};
using (XmlWriter manifestWriter = XmlWriter.Create(comManifestPath, settings))
{
manifestDocument.WriteTo(manifestWriter);
}
}
}
}