Skip to content

Commit c11003a

Browse files
[vs17.14] Replace obsolete UCOMITypeInfo with recommended System.Runtime.InteropServices.ComTypes.ITypeInfo (#12012)
Backport of #11940 to vs17.14 --------- Co-authored-by: Sujit Nayak <[email protected]>
1 parent 65391c5 commit c11003a

File tree

2 files changed

+70
-50
lines changed

2 files changed

+70
-50
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the MIT license. See License.txt in the project root for full license information. -->
33
<Project>
44
<PropertyGroup>
5-
<VersionPrefix>17.14.13</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
5+
<VersionPrefix>17.14.14</VersionPrefix><DotNetFinalVersionKind>release</DotNetFinalVersionKind>
66
<PackageValidationBaselineVersion>17.13.9</PackageValidationBaselineVersion>
77
<AssemblyVersion>15.1.0.0</AssemblyVersion>
88
<PreReleaseVersionLabel>servicing</PreReleaseVersionLabel>

src/Tasks/ManifestUtil/ComImporter.cs

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
using System.Globalization;
88
using System.Resources;
99
using System.Runtime.InteropServices;
10-
#if RUNTIME_TYPE_NETCORE
11-
using System.Runtime.InteropServices.ComTypes;
12-
#endif
10+
using ComTypes = System.Runtime.InteropServices.ComTypes;
1311
using System.Runtime.Versioning;
1412

1513
#nullable disable
@@ -55,64 +53,86 @@ public ComImporter(string path, OutputMessageCollection outputMessages, string o
5553
catch (COMException) { }
5654

5755
#pragma warning disable 618
58-
#if RUNTIME_TYPE_NETCORE
59-
ITypeLib tlib = (ITypeLib)obj;
60-
#else
61-
UCOMITypeLib tlib = (UCOMITypeLib)obj;
62-
#endif
56+
ComTypes.ITypeLib tlib = (ComTypes.ITypeLib)obj;
6357
if (tlib != null)
6458
{
6559
IntPtr typeLibAttrPtr = IntPtr.Zero;
66-
tlib.GetLibAttr(out typeLibAttrPtr);
67-
var typeLibAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttrPtr, typeof(TYPELIBATTR));
68-
tlib.ReleaseTLibAttr(typeLibAttrPtr);
69-
Guid tlbid = typeLibAttr.guid;
70-
71-
tlib.GetDocumentation(-1, out _, out string docString, out _, out string helpFile);
72-
string helpdir = Util.FilterNonprintableChars(helpFile); // Path.GetDirectoryName(helpFile);
73-
74-
TypeLib = new TypeLib(tlbid, new Version(typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum), helpdir, typeLibAttr.lcid, Convert.ToInt32(typeLibAttr.wLibFlags, CultureInfo.InvariantCulture));
75-
76-
var comClassList = new List<ComClass>();
77-
int count = tlib.GetTypeInfoCount();
78-
for (int i = 0; i < count; ++i)
60+
try
7961
{
80-
tlib.GetTypeInfoType(i, out TYPEKIND tkind);
81-
if (tkind == TYPEKIND.TKIND_COCLASS)
82-
{
83-
#if RUNTIME_TYPE_NETCORE
84-
tlib.GetTypeInfo(i, out ITypeInfo tinfo);
85-
#else
86-
tlib.GetTypeInfo(i, out UCOMITypeInfo tinfo);
87-
#endif
62+
tlib.GetLibAttr(out typeLibAttrPtr);
63+
var typeLibAttr = (ComTypes.TYPELIBATTR)Marshal.PtrToStructure(typeLibAttrPtr, typeof(ComTypes.TYPELIBATTR));
64+
Guid tlbid = typeLibAttr.guid;
8865

89-
IntPtr tinfoAttrPtr = IntPtr.Zero;
90-
tinfo.GetTypeAttr(out tinfoAttrPtr);
91-
TYPEATTR tinfoAttr = (TYPEATTR)Marshal.PtrToStructure(tinfoAttrPtr, typeof(TYPEATTR));
92-
tinfo.ReleaseTypeAttr(tinfoAttrPtr);
93-
Guid clsid = tinfoAttr.guid;
66+
tlib.GetDocumentation(-1, out _, out string docString, out _, out string helpFile);
67+
string helpdir = Util.FilterNonprintableChars(helpFile); // Path.GetDirectoryName(helpFile);
9468

95-
tlib.GetDocumentation(i, out _, out docString, out _, out helpFile);
96-
string description = Util.FilterNonprintableChars(docString);
69+
TypeLib = new TypeLib(tlbid, new Version(typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum), helpdir, typeLibAttr.lcid, Convert.ToInt32(typeLibAttr.wLibFlags, CultureInfo.InvariantCulture));
9770

98-
ClassInfo info = GetRegisteredClassInfo(clsid);
99-
if (info == null)
71+
var comClassList = new List<ComClass>();
72+
int count = tlib.GetTypeInfoCount();
73+
for (int i = 0; i < count; ++i)
74+
{
75+
tlib.GetTypeInfoType(i, out ComTypes.TYPEKIND tkind);
76+
if (tkind == ComTypes.TYPEKIND.TKIND_COCLASS)
10077
{
101-
continue;
78+
IntPtr tinfoAttrPtr = IntPtr.Zero;
79+
tlib.GetTypeInfo(i, out ComTypes.ITypeInfo tinfo);
80+
try
81+
{
82+
tinfo.GetTypeAttr(out tinfoAttrPtr);
83+
ComTypes.TYPEATTR tinfoAttr = (ComTypes.TYPEATTR)Marshal.PtrToStructure(tinfoAttrPtr, typeof(ComTypes.TYPEATTR));
84+
Guid clsid = tinfoAttr.guid;
85+
86+
tlib.GetDocumentation(i, out _, out docString, out _, out helpFile);
87+
string description = Util.FilterNonprintableChars(docString);
88+
89+
ClassInfo info = GetRegisteredClassInfo(clsid);
90+
if (info == null)
91+
{
92+
continue;
93+
}
94+
comClassList.Add(new ComClass(tlbid, clsid, info.Progid, info.ThreadingModel, description));
95+
}
96+
finally
97+
{
98+
try
99+
{
100+
if (tinfoAttrPtr != IntPtr.Zero)
101+
{
102+
tinfo.ReleaseTypeAttr(tinfoAttrPtr);
103+
}
104+
Marshal.ReleaseComObject(tinfo);
105+
tinfo = null;
106+
}
107+
// Ignore COM exceptions when releasing type attributes.
108+
catch (COMException) {}
109+
}
102110
}
103-
104-
comClassList.Add(new ComClass(tlbid, clsid, info.Progid, info.ThreadingModel, description));
111+
}
112+
if (comClassList.Count > 0)
113+
{
114+
ComClasses = comClassList.ToArray();
115+
Success = true;
116+
}
117+
else
118+
{
119+
outputMessages.AddErrorMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.NoRegisteredClasses"));
120+
Success = false;
105121
}
106122
}
107-
if (comClassList.Count > 0)
108-
{
109-
ComClasses = comClassList.ToArray();
110-
Success = true;
111-
}
112-
else
123+
finally
113124
{
114-
outputMessages.AddErrorMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.NoRegisteredClasses"));
115-
Success = false;
125+
try
126+
{
127+
if (typeLibAttrPtr != IntPtr.Zero)
128+
{
129+
tlib.ReleaseTLibAttr(typeLibAttrPtr);
130+
}
131+
Marshal.ReleaseComObject(tlib);
132+
tlib = null;
133+
}
134+
// Ignore COM exceptions when releasing type attributes.
135+
catch (COMException) {}
116136
}
117137
}
118138
else

0 commit comments

Comments
 (0)