-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathTypeLibQueryService.cs
200 lines (167 loc) · 6.71 KB
/
TypeLibQueryService.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.Win32;
using Rubberduck.InternalApi.Common;
using Rubberduck.VBEditor.Utility;
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
namespace Rubberduck.Parsing.ComReflection.TypeLibReflection
{
public interface ITypeLibQueryService
{
bool TryGetTypeFromITypeInfo(ITypeInfo typeInfo, out Type type);
bool TryGetProgIdFromClsid(Guid clsid, out string progId);
bool TryGetTypeInfoFromProgId(string progId, out ITypeInfo typeInfo);
string GetOrCreateProgIdFromITypeInfo(ITypeInfo typeInfo);
}
public class TypeLibQueryService : ITypeLibQueryService
{
[DllImport("ole32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]
private static extern int CLSIDFromProgID(string lpszProgID, out Guid lpclsid);
[DllImport("ole32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]
private static extern int ProgIDFromCLSID([In]ref Guid clsid, [MarshalAs(UnmanagedType.LPWStr)]out string lplpszProgID);
[DllImport("oleaut32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]
private static extern int LoadTypeLib(string fileName, out ITypeLib typeLib);
private static readonly Lazy<TypeLibQueryService> LazyInstance = new Lazy<TypeLibQueryService>();
private static readonly RegisteredLibraryFinderService Finder = new RegisteredLibraryFinderService();
/// <summary>
/// Provided primarily for uses outside the CW's DI, mainly within Rubberduck.Main.
/// </summary>
public static ITypeLibQueryService Instance => LazyInstance.Value;
public bool TryGetTypeFromITypeInfo(ITypeInfo typeInfo, out Type type)
{
type = null;
var ptr = Marshal.GetComInterfaceForObject(typeInfo, typeof(ITypeInfo));
if (ptr == IntPtr.Zero)
{
return false;
}
using (DisposalActionContainer.Create(ptr, x => Marshal.Release(x)))
{
type = Marshal.GetTypeForITypeInfo(ptr);
}
return type != null;
}
public string GetOrCreateProgIdFromITypeInfo(ITypeInfo typeInfo)
{
typeInfo.GetTypeAttr(out var pAttr);
if (pAttr != IntPtr.Zero)
{
using (DisposalActionContainer.Create(pAttr, typeInfo.ReleaseTypeAttr))
{
var attr = Marshal.PtrToStructure<TYPEATTR>(pAttr);
var clsid = attr.guid;
if (TryGetProgIdFromClsid(clsid, out var progId))
{
return progId;
}
}
}
var typeName = Marshal.GetTypeInfoName(typeInfo);
typeInfo.GetContainingTypeLib(out var typeLib, out _);
var libName = Marshal.GetTypeLibName(typeLib);
return string.Concat(libName, ".", typeName);
}
public bool TryGetProgIdFromClsid(Guid clsid, out string progId)
{
return ProgIDFromCLSID(ref clsid, out progId) == 0;
}
public bool TryGetTypeInfoFromProgId(string progId, out ITypeInfo typeInfo)
{
typeInfo = null;
if (CLSIDFromProgID(progId, out var clsid) != 0)
{
return false;
}
if (!TryGetTypeLibFromClsid(clsid, out var lib))
{
return false;
}
lib.GetTypeInfoOfGuid(ref clsid, out typeInfo);
return typeInfo != null;
}
private static bool TryGetTypeLibFromClsid(Guid clsid, out ITypeLib lib)
{
lib = null;
using (var clsidKey = Registry.ClassesRoot.OpenSubKey($"CLSID\\{clsid:B}"))
{
if (clsidKey == null)
{
return false;
}
if (!TryLoadTypeLibFromPath(TryGetTypeLibPath, clsidKey, out lib))
{
return true;
}
if (TryLoadTypeLibFromPath(TryGetInProcServerPath, clsidKey, out lib))
{
return true;
}
if (TryLoadTypeLibFromPath(TryGetLocalServerPath, clsidKey, out lib))
{
return true;
}
return false;
}
}
private delegate bool GetPathFunction(RegistryKey clsidKey, out string path);
private static bool TryLoadTypeLibFromPath(GetPathFunction getPathFunction, RegistryKey clsidKey, out ITypeLib lib)
{
lib = null;
if (!getPathFunction(clsidKey, out var path))
{
return false;
}
if (LoadTypeLib(path, out lib) == 0)
{
return true;
}
var file = FileSystemProvider.FileSystem.Path.GetFileName(path);
return LoadTypeLib(file, out lib) == 0;
}
private static bool TryGetTypeLibPath(RegistryKey clsidKey, out string path)
{
path = null;
using (var clsidTypeLibKey = clsidKey.OpenSubKey("TypeLib"))
{
if (clsidTypeLibKey == null)
{
return false;
}
if (Guid.TryParseExact(((string) clsidTypeLibKey.GetValue(null)).ToLowerInvariant(), "B", out var libGuid)
&& Finder.TryGetRegisteredLibraryInfo(libGuid, out var info))
{
path = info.FullPath;
}
}
return !string.IsNullOrWhiteSpace(path);
}
private static bool TryGetInProcServerPath(RegistryKey clsidKey, out string path)
{
using (var procServerKey = clsidKey.OpenSubKey("InprocServer32"))
{
if (procServerKey != null)
{
path = procServerKey.GetValue(null) as string;
return true;
}
path = null;
return false;
}
}
private static bool TryGetLocalServerPath(RegistryKey clsidKey, out string path)
{
using (var localServerKey = clsidKey.OpenSubKey("LocalServer32"))
{
if (localServerKey != null)
{
path = localServerKey.GetValue(null) as string;
return true;
}
path = null;
return false;
}
}
}
}