forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComHost.cs
55 lines (49 loc) · 2.47 KB
/
ComHost.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
// 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.IO.MemoryMappedFiles;
using System.Text;
namespace Microsoft.NET.Build.Tasks
{
internal static class ComHost
{
// These need to match RESOURCEID_CLISDMAP and RESOURCETYPE_CLSIDMAP in core-setup, defined in comhost.h.
private const int ClsidmapResourceId = 64;
private const int ClsidmapResourceType = 1024;
/// <summary>
/// Create an ComHost with an embedded CLSIDMap file to map CLSIDs to .NET Classes.
/// </summary>
/// <param name="comHostSourceFilePath">The path of Apphost template, which has the place holder</param>
/// <param name="comHostDestinationFilePath">The destination path for desired location to place, including the file name</param>
/// <param name="intermediateAssembly">Path to the intermediate assembly, used for copying resources to PE apphosts.</param>
/// <param name="clsidmap">The path to the *.clsidmap file.</param>
/// <param name="log">Specify the logger used to log warnings and messages. If null, no logging is done.</param>
public static void Create(
string comHostSourceFilePath,
string comHostDestinationFilePath,
string clsidmapFilePath)
{
var destinationDirectory = new FileInfo(comHostDestinationFilePath).Directory.FullName;
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
// Copy apphost to destination path so it inherits the same attributes/permissions.
File.Copy(comHostSourceFilePath, comHostDestinationFilePath, overwrite: true);
if (ResourceUpdater.IsSupportedOS())
{
string clsidMap = File.ReadAllText(clsidmapFilePath);
byte[] clsidMapBytes = Encoding.UTF8.GetBytes(clsidMap);
ResourceUpdater updater = new ResourceUpdater(comHostDestinationFilePath);
updater.AddResource(clsidMapBytes, (IntPtr)ClsidmapResourceType, (IntPtr)ClsidmapResourceId);
updater.Update();
}
else
{
throw new BuildErrorException(Strings.CannotEmbedClsidMapIntoComhost);
}
}
}
}