| |
| |
|
|
| using System.Collections.ObjectModel; |
| using System.Management.Automation.ComInterop; |
| using System.Runtime.InteropServices; |
| using System.Text; |
|
|
| using COM = System.Runtime.InteropServices.ComTypes; |
|
|
| |
| #pragma warning disable 1634, 1691 |
|
|
| namespace System.Management.Automation |
| { |
| |
| |
| |
| internal static class ComUtil |
| { |
| |
| internal const int DISP_E_MEMBERNOTFOUND = unchecked((int)0x80020003); |
| |
| internal const int DISP_E_UNKNOWNNAME = unchecked((int)0x80020006); |
| |
| internal const int TYPE_E_ELEMENTNOTFOUND = unchecked((int)0x8002802b); |
|
|
| |
| |
| |
| |
| |
| |
| |
| internal static string GetMethodSignatureFromFuncDesc(COM.ITypeInfo typeinfo, COM.FUNCDESC funcdesc, bool isPropertyPut) |
| { |
| StringBuilder builder = new StringBuilder(); |
|
|
| |
| int namesCount = funcdesc.cParams + 1; |
| string[] names = new string[funcdesc.cParams + 1]; |
| typeinfo.GetNames(funcdesc.memid, names, namesCount, out namesCount); |
|
|
| if (!isPropertyPut) |
| { |
| |
| string retstring = GetStringFromTypeDesc(typeinfo, funcdesc.elemdescFunc.tdesc); |
| builder.Append(retstring + " "); |
| } |
|
|
| |
| 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; |
|
|
| |
| |
| #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) |
| { |
| 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(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| internal static string GetNameFromFuncDesc(COM.ITypeInfo typeinfo, COM.FUNCDESC funcdesc) |
| { |
| |
| string strName, strDoc, strHelp; |
| int id; |
| typeinfo.GetDocumentation(funcdesc.memid, out strName, out strDoc, out id, out strHelp); |
| return strName; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private static string GetStringFromCustomType(COM.ITypeInfo typeinfo, IntPtr refptr) |
| { |
| COM.ITypeInfo custtypeinfo; |
| int reftype = unchecked((int)(long)refptr); |
|
|
| 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"; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| 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!"; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| internal static Type GetTypeFromTypeDesc(COM.TYPEDESC typedesc) |
| { |
| VarEnum vt = (VarEnum)typedesc.vt; |
| return VarEnumSelector.GetTypeForVarEnum(vt); |
| } |
|
|
| |
| |
| |
| 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); |
| } |
|
|
| |
| |
| |
| 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; |
| |
| |
| #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); |
|
|
| |
| Type type = ComUtil.GetTypeFromTypeDesc(ElementDescription.tdesc); |
| object defaultvalue = null; |
|
|
| |
| 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; |
| } |
|
|
| |
| |
| |
| |
| 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; |
| } |
| } |
| } |
|
|