Skip to content

Commit 0305aae

Browse files
committed
Refit the EnsureReferenceToAddInLibrary to use registry lookup instead of playing Assembly circus
1 parent 1727316 commit 0305aae

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

Rubberduck.Core/UnitTesting/ProjectTestExtensions.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Runtime.InteropServices;
2-
using System.Reflection;
1+
using System;
2+
using System.Runtime.InteropServices;
33
using System.IO;
4+
using System.Reflection;
5+
using Microsoft.Win32;
46
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
57

68
namespace Rubberduck.UnitTesting
@@ -10,12 +12,27 @@ public static class ProjectTestExtensions
1012
{
1113
public static void EnsureReferenceToAddInLibrary(this IVBProject project)
1214
{
13-
// FIXME should rely on the assembly containing the UnitTest components.
14-
// Those are not necessarily the same.
15-
var assembly = Assembly.GetEntryAssembly();
15+
var libFolder = IntPtr.Size == 8 ? "win64" : "win32";
16+
// TODO: This assumes the current assembly is same major/minor as the TLB!!!
17+
var libVersion = Assembly.GetExecutingAssembly().GetName().Version;
18+
const string libGuid = RubberduckGuid.RubberduckTypeLibGuid;
19+
var pathKey = Registry.ClassesRoot.OpenSubKey($@"TypeLib\{{{libGuid}}}\{libVersion.Major}.{libVersion.Minor}\0\{libFolder}");
20+
21+
var referencePath = pathKey?.GetValue(string.Empty, string.Empty) as string;
22+
string name = null;
23+
24+
if (!string.IsNullOrWhiteSpace(referencePath))
25+
{
26+
var tlbKey =
27+
Registry.ClassesRoot.OpenSubKey($@"TypeLib\{{{libGuid}}}\{libVersion.Major}.{libVersion.Minor}");
1628

17-
var name = assembly.GetName().Name.Replace('.', '_');
18-
var referencePath = Path.ChangeExtension(assembly.Location, ".tlb");
29+
name = tlbKey?.GetValue(string.Empty, string.Empty) as string;
30+
}
31+
32+
if (string.IsNullOrWhiteSpace(referencePath) || string.IsNullOrWhiteSpace(name))
33+
{
34+
throw new InvalidOperationException("Cannot locate the tlb in the registry or the entry may be corrupted. Therefore early binding is not possible");
35+
}
1936

2037
using (var references = project.References)
2138
{

0 commit comments

Comments
 (0)