|
7 | 7 | using System.Globalization;
|
8 | 8 | using System.Resources;
|
9 | 9 | using System.Runtime.InteropServices;
|
10 |
| -#if RUNTIME_TYPE_NETCORE |
11 |
| -using System.Runtime.InteropServices.ComTypes; |
12 |
| -#endif |
| 10 | +using ComTypes = System.Runtime.InteropServices.ComTypes; |
13 | 11 | using System.Runtime.Versioning;
|
14 | 12 |
|
15 | 13 | #nullable disable
|
@@ -55,64 +53,86 @@ public ComImporter(string path, OutputMessageCollection outputMessages, string o
|
55 | 53 | catch (COMException) { }
|
56 | 54 |
|
57 | 55 | #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; |
63 | 57 | if (tlib != null)
|
64 | 58 | {
|
65 | 59 | 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 |
79 | 61 | {
|
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; |
88 | 65 |
|
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); |
94 | 68 |
|
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)); |
97 | 70 |
|
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) |
100 | 77 | {
|
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 | + } |
102 | 110 | }
|
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; |
105 | 121 | }
|
106 | 122 | }
|
107 |
| - if (comClassList.Count > 0) |
108 |
| - { |
109 |
| - ComClasses = comClassList.ToArray(); |
110 |
| - Success = true; |
111 |
| - } |
112 |
| - else |
| 123 | + finally |
113 | 124 | {
|
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) {} |
116 | 136 | }
|
117 | 137 | }
|
118 | 138 | else
|
|
0 commit comments