Skip to content

Commit f59e0fa

Browse files
authored
Merge pull request #5256 from bclothier/MiscBugFixes
Move the extension class and use constant range instead.
2 parents ffddbae + 11f1843 commit f59e0fa

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

Rubberduck.Parsing/ComReflection/ComModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using VARDESC = System.Runtime.InteropServices.ComTypes.VARDESC;
1313
using CALLCONV = System.Runtime.InteropServices.ComTypes.CALLCONV;
1414
using Rubberduck.VBEditor.ComManagement.TypeLibs.Abstract;
15-
using Rubberduck.JunkDrawer.Hacks;
15+
using Rubberduck.VBEditor.ComManagement.TypeLibs.Utility;
1616

1717
namespace Rubberduck.Parsing.ComReflection
1818
{

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeInfoConstantsCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Collections.Generic;
22
using System.Runtime.InteropServices.ComTypes;
3-
using Rubberduck.JunkDrawer.Hacks;
43
using Rubberduck.VBEditor.ComManagement.TypeLibs.Abstract;
54
using Rubberduck.VBEditor.ComManagement.TypeLibs.Unmanaged;
5+
using Rubberduck.VBEditor.ComManagement.TypeLibs.Utility;
66
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
77
using VARDESC = System.Runtime.InteropServices.ComTypes.VARDESC;
88

Rubberduck.JunkDrawer/Hacks/VarDescExtensions.cs renamed to Rubberduck.VBEEditor/ComManagement/TypeLibs/Utility/VarDescExtensions.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.Runtime.InteropServices.ComTypes;
1+
using System;
2+
using System.Runtime.InteropServices.ComTypes;
23

3-
namespace Rubberduck.JunkDrawer.Hacks
4+
namespace Rubberduck.VBEditor.ComManagement.TypeLibs.Utility
45
{
56
public static class VarDescExtensions
67
{
@@ -16,13 +17,22 @@ public static class VarDescExtensions
1617
///
1718
/// There is a small but non-zero chance that there might be a valid pointer that happens to be only in high half of the address...
1819
/// in that case, it'll be wrong but since VBA is always writing <see cref="VARKIND.VAR_STATIC"/> to the <see cref="VARDESC.varkind"/>
19-
/// field, we're kind of stuck...
20+
/// field. To accommodate this unlikely possibility, we take advantage of VBA's other failure to comply with MS-OAUT specifications:
21+
/// None of its constants has a valid member ID. Normally they are assigned <c>MEMBER_NIL</c> which makes them useless for enumeration. To
22+
/// accommodate this, the TypeInfoWrapper in Rubberduck.VBEditor project will generate unique member IDs for those constants. Thus, we can use
23+
/// the same range to infer that it's a constant assigned by the TypeInfoWrapper.
2024
/// </remarks>
2125
/// <param name="varDesc">The <see cref="VARDESC"/> from a VBA <see cref="ITypeInfo"/></param>
2226
/// <returns>True if this is most likely a constant. False when it's definitely not.</returns>
2327
public static bool IsValidVBAConstant(this VARDESC varDesc)
2428
{
25-
return varDesc.varkind == VARKIND.VAR_STATIC && varDesc.desc.oInst != 0;
29+
// TODO: Move the function to a better home and avoid duplication of constants (see TypeInfoWrapper)
30+
const int _ourConstantsDispatchMemberIDRangeStart = unchecked((int)0xFEDC0000);
31+
const int _ourConstantsDispatchMemberIDRangeBitmaskCheck = unchecked((int)0xFFFF0000);
32+
33+
return (varDesc.memid & _ourConstantsDispatchMemberIDRangeBitmaskCheck) >= _ourConstantsDispatchMemberIDRangeStart
34+
&& varDesc.varkind == VARKIND.VAR_STATIC
35+
&& varDesc.desc.lpvarValue != IntPtr.Zero;
2636
}
2737
}
2838
}

0 commit comments

Comments
 (0)