1
- using System . Runtime . InteropServices ;
2
- using System . Reflection ;
1
+ using System ;
2
+ using System . Runtime . InteropServices ;
3
3
using System . IO ;
4
+ using System . Reflection ;
5
+ using Microsoft . Win32 ;
4
6
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
5
7
6
8
namespace Rubberduck . UnitTesting
@@ -10,12 +12,27 @@ public static class ProjectTestExtensions
10
12
{
11
13
public static void EnsureReferenceToAddInLibrary ( this IVBProject project )
12
14
{
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 } ") ;
16
28
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
+ }
19
36
20
37
using ( var references = project . References )
21
38
{
0 commit comments