File size: 14,126 Bytes
8c763fb | 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.ObjectModel;
using System.Management.Automation.ComInterop;
using System.Runtime.InteropServices;
using System.Text;
using COM = System.Runtime.InteropServices.ComTypes;
// Stops compiler from warning about unknown warnings. Prefast warning numbers are not recognized by C# compiler
#pragma warning disable 1634, 1691
namespace System.Management.Automation
{
/// <summary>
/// Defines a utility class that is used by COM adapter.
/// </summary>
internal static class ComUtil
{
// HResult error code '-2147352573' - Member not found.
internal const int DISP_E_MEMBERNOTFOUND = unchecked((int)0x80020003);
// HResult error code '-2147352570' - Unknown Name.
internal const int DISP_E_UNKNOWNNAME = unchecked((int)0x80020006);
// HResult error code '-2147319765' - Element not found.
internal const int TYPE_E_ELEMENTNOTFOUND = unchecked((int)0x8002802b);
/// <summary>
/// Gets Method Signature from FuncDesc describing the method.
/// </summary>
/// <param name="typeinfo">ITypeInfo interface of the object.</param>
/// <param name="funcdesc">FuncDesc which defines the method.</param>
/// <param name="isPropertyPut">True if this is a property put; these properties take their return type from their first parameter.</param>
/// <returns>Signature of the method.</returns>
internal static string GetMethodSignatureFromFuncDesc(COM.ITypeInfo typeinfo, COM.FUNCDESC funcdesc, bool isPropertyPut)
{
StringBuilder builder = new StringBuilder();
// First value is function name
int namesCount = funcdesc.cParams + 1;
string[] names = new string[funcdesc.cParams + 1];
typeinfo.GetNames(funcdesc.memid, names, namesCount, out namesCount);
if (!isPropertyPut)
{
// First get the string for return type.
string retstring = GetStringFromTypeDesc(typeinfo, funcdesc.elemdescFunc.tdesc);
builder.Append(retstring + " ");
}
// Append the function name
builder.Append(names[0]);
builder.Append(" (");
IntPtr ElementDescriptionArrayPtr = funcdesc.lprgelemdescParam;
int ElementDescriptionSize = Marshal.SizeOf<COM.ELEMDESC>();
for (int i = 0; i < funcdesc.cParams; i++)
{
COM.ELEMDESC ElementDescription;
int ElementDescriptionArrayByteOffset;
IntPtr ElementDescriptionPointer;
ElementDescription = new COM.ELEMDESC();
ElementDescriptionArrayByteOffset = i * ElementDescriptionSize;
// Disable PRefast warning for converting to int32 and converting back into intptr.
// Code below takes into account 32 bit vs 64 bit conversions
#pragma warning disable 56515
if (IntPtr.Size == 4)
{
ElementDescriptionPointer = (IntPtr)(ElementDescriptionArrayPtr.ToInt32() + ElementDescriptionArrayByteOffset);
}
else
{
ElementDescriptionPointer = (IntPtr)(ElementDescriptionArrayPtr.ToInt64() + ElementDescriptionArrayByteOffset);
}
#pragma warning restore 56515
ElementDescription = Marshal.PtrToStructure<COM.ELEMDESC>(ElementDescriptionPointer);
string paramstring = GetStringFromTypeDesc(typeinfo, ElementDescription.tdesc);
if (i == 0 && isPropertyPut) // use the type of the first argument as the return type
{
builder.Insert(0, paramstring + " ");
}
else
{
builder.Append(paramstring);
builder.Append(" " + names[i + 1]);
if (i < funcdesc.cParams - 1)
{
builder.Append(", ");
}
}
}
builder.Append(')');
return builder.ToString();
}
/// <summary>
/// Gets the name of the method or property defined by funcdesc.
/// </summary>
/// <param name="typeinfo">ITypeInfo interface of the type.</param>
/// <param name="funcdesc">FuncDesc of property of method.</param>
/// <returns>Name of the method or property.</returns>
internal static string GetNameFromFuncDesc(COM.ITypeInfo typeinfo, COM.FUNCDESC funcdesc)
{
// Get the method or property name.
string strName, strDoc, strHelp;
int id;
typeinfo.GetDocumentation(funcdesc.memid, out strName, out strDoc, out id, out strHelp);
return strName;
}
/// <summary>
/// Gets the name of the custom type defined in the type library.
/// </summary>
/// <param name="typeinfo">ITypeInfo interface of the type.</param>
/// <param name="refptr">Reference to the custom type.</param>
/// <returns>Name of the custom type.</returns>
private static string GetStringFromCustomType(COM.ITypeInfo typeinfo, IntPtr refptr)
{
COM.ITypeInfo custtypeinfo;
int reftype = unchecked((int)(long)refptr); // note that we cast to long first to prevent overflows; this cast is OK since we are only interested in the lower word
typeinfo.GetRefTypeInfo(reftype, out custtypeinfo);
if (custtypeinfo != null)
{
string strName, strDoc, strHelp;
int id;
custtypeinfo.GetDocumentation(-1, out strName, out strDoc, out id, out strHelp);
return strName;
}
return "UnknownCustomtype";
}
/// <summary>
/// This function gets a string representation of the Type Descriptor
/// This is used in generating signature for Properties and Methods.
/// </summary>
/// <param name="typeinfo">Reference to the type info to which the type descriptor belongs.</param>
/// <param name="typedesc">Reference to type descriptor which is being converted to string from.</param>
/// <returns>String representation of the type descriptor.</returns>
private static string GetStringFromTypeDesc(COM.ITypeInfo typeinfo, COM.TYPEDESC typedesc)
{
if ((VarEnum)typedesc.vt == VarEnum.VT_PTR)
{
COM.TYPEDESC refdesc = Marshal.PtrToStructure<COM.TYPEDESC>(typedesc.lpValue);
return GetStringFromTypeDesc(typeinfo, refdesc);
}
if ((VarEnum)typedesc.vt == VarEnum.VT_SAFEARRAY)
{
COM.TYPEDESC refdesc = Marshal.PtrToStructure<COM.TYPEDESC>(typedesc.lpValue);
return "SAFEARRAY(" + GetStringFromTypeDesc(typeinfo, refdesc) + ")";
}
if ((VarEnum)typedesc.vt == VarEnum.VT_USERDEFINED)
{
return GetStringFromCustomType(typeinfo, typedesc.lpValue);
}
switch ((VarEnum)typedesc.vt)
{
case VarEnum.VT_I1:
return "char";
case VarEnum.VT_I2:
return "short";
case VarEnum.VT_I4:
case VarEnum.VT_INT:
case VarEnum.VT_HRESULT:
return "int";
case VarEnum.VT_I8:
return "int64";
case VarEnum.VT_R4:
return "float";
case VarEnum.VT_R8:
return "double";
case VarEnum.VT_UI1:
return "byte";
case VarEnum.VT_UI2:
return "ushort";
case VarEnum.VT_UI4:
case VarEnum.VT_UINT:
return "uint";
case VarEnum.VT_UI8:
return "uint64";
case VarEnum.VT_BSTR:
case VarEnum.VT_LPSTR:
case VarEnum.VT_LPWSTR:
return "string";
case VarEnum.VT_DATE:
return "Date";
case VarEnum.VT_BOOL:
return "bool";
case VarEnum.VT_CY:
return "currency";
case VarEnum.VT_DECIMAL:
return "decimal";
case VarEnum.VT_CLSID:
return "clsid";
case VarEnum.VT_DISPATCH:
return "IDispatch";
case VarEnum.VT_UNKNOWN:
return "IUnknown";
case VarEnum.VT_VARIANT:
return "Variant";
case VarEnum.VT_VOID:
return "void";
case VarEnum.VT_ARRAY:
return "object[]";
case VarEnum.VT_EMPTY:
return string.Empty;
default:
return "Unknown!";
}
}
/// <summary>
/// Determine .net type for the given type descriptor.
/// </summary>
/// <param name="typedesc">COM type descriptor to convert.</param>
/// <returns>Type represented by the typedesc.</returns>
internal static Type GetTypeFromTypeDesc(COM.TYPEDESC typedesc)
{
VarEnum vt = (VarEnum)typedesc.vt;
return VarEnumSelector.GetTypeForVarEnum(vt);
}
/// <summary>
/// Converts a FuncDesc out of GetFuncDesc into a MethodInformation.
/// </summary>
private static ComMethodInformation GetMethodInformation(COM.FUNCDESC funcdesc, bool skipLastParameter)
{
Type returntype = GetTypeFromTypeDesc(funcdesc.elemdescFunc.tdesc);
ParameterInformation[] parameters = GetParameterInformation(funcdesc, skipLastParameter);
bool hasOptional = false;
foreach (ParameterInformation p in parameters)
{
if (p.isOptional)
{
hasOptional = true;
break;
}
}
return new ComMethodInformation(false, hasOptional, parameters, returntype, funcdesc.memid, funcdesc.invkind);
}
/// <summary>
/// Obtains the parameter information for a given FuncDesc.
/// </summary>
internal static ParameterInformation[] GetParameterInformation(COM.FUNCDESC funcdesc, bool skipLastParameter)
{
int cParams = funcdesc.cParams;
if (skipLastParameter)
{
Diagnostics.Assert(cParams > 0, "skipLastParameter is only true for property setters where there is at least one parameter");
cParams--;
}
ParameterInformation[] parameters = new ParameterInformation[cParams];
IntPtr ElementDescriptionArrayPtr = funcdesc.lprgelemdescParam;
int ElementDescriptionSize = Marshal.SizeOf<COM.ELEMDESC>();
for (int i = 0; i < cParams; i++)
{
COM.ELEMDESC ElementDescription;
int ElementDescriptionArrayByteOffset;
IntPtr ElementDescriptionPointer;
bool fOptional = false;
ElementDescription = new COM.ELEMDESC();
ElementDescriptionArrayByteOffset = i * ElementDescriptionSize;
// Disable PRefast warning for converting to int32 and converting back into intptr.
// Code below takes into account 32 bit vs 64 bit conversions
#pragma warning disable 56515
if (IntPtr.Size == 4)
{
ElementDescriptionPointer = (IntPtr)(ElementDescriptionArrayPtr.ToInt32() + ElementDescriptionArrayByteOffset);
}
else
{
ElementDescriptionPointer = (IntPtr)(ElementDescriptionArrayPtr.ToInt64() + ElementDescriptionArrayByteOffset);
}
#pragma warning restore 56515
ElementDescription = Marshal.PtrToStructure<COM.ELEMDESC>(ElementDescriptionPointer);
// get the type of parameter
Type type = ComUtil.GetTypeFromTypeDesc(ElementDescription.tdesc);
object defaultvalue = null;
// check is this parameter is optional.
if ((ElementDescription.desc.paramdesc.wParamFlags & COM.PARAMFLAG.PARAMFLAG_FOPT) != 0)
{
fOptional = true;
defaultvalue = Type.Missing;
}
bool fByRef = (ElementDescription.desc.paramdesc.wParamFlags & COM.PARAMFLAG.PARAMFLAG_FOUT) != 0;
parameters[i] = new ParameterInformation(type, fOptional, defaultvalue, fByRef);
}
return parameters;
}
/// <summary>
/// Converts a MethodBase[] into a MethodInformation[]
/// </summary>
/// <returns>The ComMethodInformation[] corresponding to methods.</returns>
internal static ComMethodInformation[] GetMethodInformationArray(COM.ITypeInfo typeInfo, Collection<int> methods, bool skipLastParameters)
{
int methodCount = methods.Count;
int count = 0;
ComMethodInformation[] returnValue = new ComMethodInformation[methodCount];
foreach (int index in methods)
{
IntPtr pFuncDesc;
typeInfo.GetFuncDesc(index, out pFuncDesc);
COM.FUNCDESC funcdesc = Marshal.PtrToStructure<COM.FUNCDESC>(pFuncDesc);
returnValue[count++] = ComUtil.GetMethodInformation(funcdesc, skipLastParameters);
typeInfo.ReleaseFuncDesc(pFuncDesc);
}
return returnValue;
}
}
}
|