/
ComParameter.cs
135 lines (122 loc) · 5.34 KB
/
ComParameter.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
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Rubberduck.VBEditor.Utility;
using ELEMDESC = System.Runtime.InteropServices.ComTypes.ELEMDESC;
using PARAMFLAG = System.Runtime.InteropServices.ComTypes.PARAMFLAG;
using TYPEATTR = System.Runtime.InteropServices.ComTypes.TYPEATTR;
using TYPEDESC = System.Runtime.InteropServices.ComTypes.TYPEDESC;
using TYPEKIND = System.Runtime.InteropServices.ComTypes.TYPEKIND;
namespace Rubberduck.Parsing.ComReflection
{
[DebuggerDisplay("{DeclarationName}")]
public class ComParameter
{
public string Name { get; private set; }
public string DeclarationName
{
get
{
return string.Format("{0}{1} {2} As {3}{4}{5}",
IsOptional ? "Optional " : string.Empty,
IsByRef ? "ByRef" : "ByVal",
Name,
TypeName,
IsOptional && DefaultValue != null ? " = " : string.Empty,
IsOptional && DefaultValue != null ?
IsEnumMember ? DefaultAsEnum : DefaultValue
: string.Empty);
}
}
public bool IsArray { get; private set; }
public bool IsByRef { get; private set; }
public bool IsOptional { get; }
public bool IsReturnValue { get; }
public bool IsParamArray { get; set; }
private Guid _enumGuid = Guid.Empty;
public bool IsEnumMember => !_enumGuid.Equals(Guid.Empty);
public object DefaultValue { get; }
public string DefaultAsEnum { get; }
private string _type = "Object";
public string TypeName => IsArray ? $"{_type}()" : _type;
public ComParameter(ELEMDESC elemDesc, ITypeInfo info, string name)
{
Debug.Assert(name != null, "Parameter name is null");
Name = name;
var paramDesc = elemDesc.desc.paramdesc;
GetParameterType(elemDesc.tdesc, info);
IsOptional = paramDesc.wParamFlags.HasFlag(PARAMFLAG.PARAMFLAG_FOPT);
IsReturnValue = paramDesc.wParamFlags.HasFlag(PARAMFLAG.PARAMFLAG_FRETVAL);
if (!paramDesc.wParamFlags.HasFlag(PARAMFLAG.PARAMFLAG_FHASDEFAULT) || string.IsNullOrEmpty(name))
{
DefaultAsEnum = string.Empty;
return;
}
//lpVarValue points to a PARAMDESCEX structure, but we don't care about the cBytes here at all.
//Offset and dereference the VARIANTARG directly.
var defValue = new ComVariant(paramDesc.lpVarValue + Marshal.SizeOf(typeof(ulong)));
DefaultValue = defValue.Value;
if (!IsEnumMember || !ComProject.KnownEnumerations.TryGetValue(_enumGuid, out ComEnumeration enumType))
{
return;
}
var member = enumType.Members.FirstOrDefault(m => m.Value == (int)DefaultValue);
DefaultAsEnum = member != null ? member.Name : string.Empty;
}
//This overload should only be used for retrieving the TypeName from a random TYPEATTR. TODO: Should be a base class of ComParameter instead.
public ComParameter(TYPEATTR attributes, ITypeInfo info)
{
GetParameterType(attributes.tdescAlias, info);
}
private void GetParameterType(TYPEDESC desc, ITypeInfo info)
{
var vt = (VarEnum)desc.vt;
TYPEDESC tdesc;
switch (vt)
{
case VarEnum.VT_PTR:
tdesc = (TYPEDESC)Marshal.PtrToStructure(desc.lpValue, typeof(TYPEDESC));
GetParameterType(tdesc, info);
IsByRef = true;
break;
case VarEnum.VT_USERDEFINED:
int href;
unchecked
{
href = (int)(desc.lpValue.ToInt64() & 0xFFFFFFFF);
}
try
{
info.GetRefTypeInfo(href, out ITypeInfo refTypeInfo);
refTypeInfo.GetTypeAttr(out IntPtr attribPtr);
using (DisposalActionContainer.Create(attribPtr, refTypeInfo.ReleaseTypeAttr))
{
var attribs = Marshal.PtrToStructure<TYPEATTR>(attribPtr);
if (attribs.typekind == TYPEKIND.TKIND_ENUM)
{
_enumGuid = attribs.guid;
}
_type = new ComDocumentation(refTypeInfo, -1).Name;
}
}
catch (COMException) { }
break;
case VarEnum.VT_SAFEARRAY:
case VarEnum.VT_CARRAY:
case VarEnum.VT_ARRAY:
tdesc = (TYPEDESC)Marshal.PtrToStructure(desc.lpValue, typeof(TYPEDESC));
GetParameterType(tdesc, info);
IsArray = true;
break;
default:
if (ComVariant.TypeNames.TryGetValue(vt, out string result))
{
_type = result;
}
break;
}
}
}
}