aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/CLR/Interop/CLRInterop.cs
blob: 4157f23aeaaa2be42e22a8884644fbd98fecf251 (plain)
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
// 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.Clr.Interop
{
    using System;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Interop class for mscorwks.dll assembly name APIs.
    /// </summary>
    internal sealed class ClrInterop
    {
        private static readonly Guid referenceIdentityGuid = new Guid("6eaf5ace-7917-4f3c-b129-e046a9704766");

        /// <summary>
        /// Protect the constructor.
        /// </summary>
        private ClrInterop()
        {
        }

        /// <summary>
        /// Represents a reference to the unique signature of a code object.
        /// </summary>
        [ComImport]
        [Guid("6eaf5ace-7917-4f3c-b129-e046a9704766")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IReferenceIdentity
        {
            /// <summary>
            /// Get an assembly attribute.
            /// </summary>
            /// <param name="attributeNamespace">Attribute namespace.</param>
            /// <param name="attributeName">Attribute name.</param>
            /// <returns>The assembly attribute.</returns>
            [return: MarshalAs(UnmanagedType.LPWStr)]
            string GetAttribute(
                [In, MarshalAs(UnmanagedType.LPWStr)] string attributeNamespace,
                [In, MarshalAs(UnmanagedType.LPWStr)] string attributeName);

            /// <summary>
            /// Set an assembly attribute.
            /// </summary>
            /// <param name="attributeNamespace">Attribute namespace.</param>
            /// <param name="attributeName">Attribute name.</param>
            /// <param name="attributeValue">Attribute value.</param>
            void SetAttribute(
                [In, MarshalAs(UnmanagedType.LPWStr)] string attributeNamespace,
                [In, MarshalAs(UnmanagedType.LPWStr)] string attributeName,
                [In, MarshalAs(UnmanagedType.LPWStr)] string attributeValue);

            /// <summary>
            /// Get an iterator for the assembly's attributes.
            /// </summary>
            /// <returns>Assembly attribute enumerator.</returns>
            IEnumIDENTITY_ATTRIBUTE EnumAttributes();

            /// <summary>
            /// Clone an IReferenceIdentity.
            /// </summary>
            /// <param name="countOfDeltas">Count of deltas.</param>
            /// <param name="deltas">The deltas.</param>
            /// <returns>Cloned IReferenceIdentity.</returns>
            IReferenceIdentity Clone(
                [In] IntPtr /*SIZE_T*/ countOfDeltas,
                [In, MarshalAs(UnmanagedType.LPArray)] IDENTITY_ATTRIBUTE[] deltas);
        }

        /// <summary>
        /// IEnumIDENTITY_ATTRIBUTE interface.
        /// </summary>
        [ComImport]
        [Guid("9cdaae75-246e-4b00-a26d-b9aec137a3eb")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IEnumIDENTITY_ATTRIBUTE
        {
            /// <summary>
            /// Gets the next attributes.
            /// </summary>
            /// <param name="celt">Count of elements.</param>
            /// <param name="attributes">Array of attributes being returned.</param>
            /// <returns>The next attribute.</returns>
            uint Next(
                [In] uint celt,
                [Out, MarshalAs(UnmanagedType.LPArray)] IDENTITY_ATTRIBUTE[] attributes);

            /// <summary>
            /// Copy the current attribute into a buffer.
            /// </summary>
            /// <param name="available">Number of available bytes.</param>
            /// <param name="data">Buffer into which attribute should be written.</param>
            /// <returns>Pointer to buffer containing the attribute.</returns>
            IntPtr CurrentIntoBuffer(
                [In] IntPtr /*SIZE_T*/ available,
                [Out, MarshalAs(UnmanagedType.LPArray)] byte[] data);

            /// <summary>
            /// Skip past a number of elements.
            /// </summary>
            /// <param name="celt">Count of elements to skip.</param>
            void Skip([In] uint celt);

            /// <summary>
            /// Reset the enumeration to the beginning.
            /// </summary>
            void Reset();

            /// <summary>
            /// Clone this attribute enumeration.
            /// </summary>
            /// <returns>Clone of a IEnumIDENTITY_ATTRIBUTE.</returns>
            IEnumIDENTITY_ATTRIBUTE Clone();
        }

        /// <summary>
        /// Gets the guid.
        /// </summary>
        public static Guid ReferenceIdentityGuid
        {
            get { return referenceIdentityGuid; }
        }

        /// <summary>
        /// Gets an interface pointer to an object with the specified IID, in the assembly at the specified file path.
        /// </summary>
        /// <param name="wszAssemblyPath">A valid path to the requested assembly.</param>
        /// <param name="riid">The IID of the interface to return.</param>
        /// <param name="i">The returned interface pointer.</param>
        /// <returns>The error code.</returns>
        [DllImport("mscorwks.dll", CharSet = CharSet.Unicode, EntryPoint = "GetAssemblyIdentityFromFile")]
        internal static extern uint GetAssemblyIdentityFromFile(System.String wszAssemblyPath, ref Guid riid, out IReferenceIdentity i);

        /// <summary>
        /// Assembly attributes. Contains data about an IReferenceIdentity.
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        internal struct IDENTITY_ATTRIBUTE
        {
            [MarshalAs(UnmanagedType.LPWStr)]
            public string AttributeNamespace;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string AttributeName;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string AttributeValue;
        }
    }
}