Skip to content

Commit 37a301c

Browse files
added TypeInfoReference.TypeLib property to obtain the already loaded ITypeLib for a VBA reference
1 parent a0d8b33 commit 37a301c

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeLibs.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
// TODO add memory address validation in ReadStructureSafe
1313
// TODO split into TypeInfos.cs
14-
// references expose the raw ITypeLibs
1514

1615
/// <summary>
1716
/// For usage examples, please see VBETypeLibsAPI
@@ -302,6 +301,9 @@ public void Document(StringLineBuilder output)
302301
/// </summary>
303302
public class TypeInfoReference
304303
{
304+
TypeLibWrapper _vbeTypeLib;
305+
int _typeLibIndex;
306+
305307
readonly string _rawString;
306308
readonly Guid _guid;
307309
readonly uint _majorVersion;
@@ -318,8 +320,11 @@ public class TypeInfoReference
318320
public string Path { get => _path; }
319321
public string Name { get => _name; }
320322

321-
public TypeInfoReference(string referenceStringRaw)
323+
public TypeInfoReference(TypeLibWrapper vbeTypeLib, int typeLibIndex, string referenceStringRaw)
322324
{
325+
_vbeTypeLib = vbeTypeLib;
326+
_typeLibIndex = typeLibIndex;
327+
323328
// Example: "*\G{000204EF-0000-0000-C000-000000000046}#4.1#9#C:\PROGRA~2\COMMON~1\MICROS~1\VBA\VBA7\VBE7.DLL#Visual Basic For Applications"
324329
// LibidReference defined at https://msdn.microsoft.com/en-us/library/dd922767(v=office.12).aspx
325330
// The string is split into 5 parts, delimited by #
@@ -346,6 +351,8 @@ public TypeInfoReference(string referenceStringRaw)
346351
_name = referenceStringParts[4];
347352
}
348353

354+
public TypeLibWrapper TypeLib { get => _vbeTypeLib.GetVBEReferenceTypeLibByIndex(_typeLibIndex); }
355+
349356
public void Document(StringLineBuilder output)
350357
{
351358
output.AppendLine("- VBE Reference: " + Name + " [path: " + Path + ", majorVersion: " + MajorVersion +
@@ -1238,7 +1245,29 @@ public TypeInfoReference GetVBEReferenceByIndex(int index)
12381245
{
12391246
if (index < target_IVBEProject.GetReferencesCount())
12401247
{
1241-
return new TypeInfoReference(target_IVBEProject.GetReferenceString(index));
1248+
return new TypeInfoReference(this, index, target_IVBEProject.GetReferenceString(index));
1249+
}
1250+
1251+
throw new ArgumentException($"Specified index not valid for the references collection {index}.");
1252+
}
1253+
else
1254+
{
1255+
throw new ArgumentException("This TypeLib does not represent a VBE project, so we cannot get reference strings from it");
1256+
}
1257+
}
1258+
1259+
public TypeLibWrapper GetVBEReferenceTypeLibByIndex(int index)
1260+
{
1261+
if (HasVBEExtensions)
1262+
{
1263+
if (index < target_IVBEProject.GetReferencesCount())
1264+
{
1265+
IntPtr referenceTypeLibPtr = target_IVBEProject.GetReferenceTypeLib(index);
1266+
if (referenceTypeLibPtr == IntPtr.Zero)
1267+
{
1268+
throw new ArgumentException("$Reference TypeLib not available - probably a missing reference.");
1269+
}
1270+
return new TypeLibWrapper(referenceTypeLibPtr);
12421271
}
12431272

12441273
throw new ArgumentException($"Specified index not valid for the references collection {index}.");
@@ -1320,7 +1349,7 @@ private void InitCommon()
13201349
public TypeLibWrapper(IntPtr rawObjectPtr)
13211350
{
13221351
target_ITypeLib = (ComTypes.ITypeLib)Marshal.GetObjectForIUnknown(rawObjectPtr);
1323-
Marshal.Release(rawObjectPtr); // _wrappedObject holds a reference to this now
1352+
Marshal.Release(rawObjectPtr); // target_ITypeLib holds a reference to this now
13241353
InitCommon();
13251354
}
13261355

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeLibsAbstract.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ interface IVBEProject
283283
void Placeholder12();
284284
void Placeholder13();
285285
int GetReferencesCount();
286-
void Placeholder15();
286+
IntPtr GetReferenceTypeLib(int ReferenceIndex);
287287
void Placeholder16();
288288
void Placeholder17();
289289
string GetReferenceString(int ReferenceIndex); // the raw reference string

0 commit comments

Comments
 (0)