internal ComMethodInformation(bool hasvarargs, bool hasoptional, ParameterInformation[] arguments, Type returnType, int dispId, COM.INVOKEKIND invokekind) : base(hasvarargs, hasoptional, arguments) { this.ReturnType = returnType; this.DispId = dispId; this.InvokeKind = invokekind; }
internal static void SetNewArgument( string methodName, object[] arguments, object[] newArguments, ParameterInformation parameter, int index) { if (arguments.Length > index) { try { newArguments[index] = Adapter.MethodArgumentConvertTo(arguments[index], parameter.isByRef, index, parameter.parameterType, (IFormatProvider)CultureInfo.InvariantCulture); } catch (InvalidCastException ex) { throw new MethodException("MethodArgumentConversionInvalidCastArgument", (Exception)ex, "ExtendedTypeSystem", "MethodArgumentConversionException", new object[5] { (object)index, arguments[index], (object)methodName, (object)parameter.parameterType, (object)ex.Message }); } } else { newArguments[index] = parameter.defaultValue; } }
protected override object MethodInvoke(PSMethod method, object[] arguments) { ParameterInformation[] arguments1 = new ParameterInformation[arguments.Length]; for (int index = 0; index < arguments.Length; ++index) { arguments1[index] = new ParameterInformation(typeof(object), false, (object)null, false); } MethodInformation[] methods = new MethodInformation[1] { new MethodInformation(false, false, arguments1) }; object[] newArguments; Adapter.GetBestMethodAndArguments(method.Name, methods, arguments, out newArguments); DirectoryEntry baseObject = (DirectoryEntry)method.baseObject; Exception exception; try { return(baseObject.Invoke(method.Name, newArguments)); } catch (DirectoryServicesCOMException ex) { exception = (Exception)ex; } catch (TargetInvocationException ex) { exception = (Exception)ex; } catch (COMException ex) { exception = (Exception)ex; } return((DirectoryEntryAdapter.dotNetAdapter.GetDotNetMethod <PSMethod>(method.baseObject, method.name) ?? throw exception).Invoke(arguments)); }
/// <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 enable 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> /// Called after a non null return from GetMember to try to call /// the method with the arguments /// </summary> /// <param name="method">the non empty return from GetMethods</param> /// <param name="arguments">the arguments to use</param> /// <returns>the return value for the method</returns> protected override object MethodInvoke(PSMethod method, object[] arguments) { ParameterInformation[] parameters = new ParameterInformation[arguments.Length]; for (int i = 0; i < arguments.Length; i++) { parameters[i] = new ParameterInformation(typeof(System.Object), false, null, false); } MethodInformation[] methodInformation = new MethodInformation[1]; methodInformation[0] = new MethodInformation(false, false, parameters); object[] newArguments; GetBestMethodAndArguments(method.Name, methodInformation, arguments, out newArguments); DirectoryEntry entry = (DirectoryEntry)method.baseObject; // First try to invoke method on the native adsi object. If the method // call fails, try to invoke dotnet method with same name, if one available. // This will ensure dotnet methods are exposed for DE objects. // The problem is in GetMember<T>(), DE adapter cannot check if a requested // method is available as it doesn't have access to native adsi object's // method metadata. So GetMember<T> returns PSMethod assuming a method // is available. This behavior will never give a chance to dotnet adapter // to resolve method call. So the DE adapter owns calling dotnet method // if one available. Exception exception; try { return(entry.Invoke(method.Name, newArguments)); } catch (DirectoryServicesCOMException dse) { exception = dse; } catch (TargetInvocationException tie) { exception = tie; } catch (COMException ce) { exception = ce; } // this code is reached only on exception // check if there is a dotnet method, invoke the dotnet method if available PSMethod dotNetmethod = s_dotNetAdapter.GetDotNetMethod <PSMethod>(method.baseObject, method.name); if (null != dotNetmethod) { return(dotNetmethod.Invoke(arguments)); } // else throw exception; }
private static ParameterInformation[] ExpandParameters( int argCount, ParameterInformation[] parameters, Type elementType) { ParameterInformation[] parameterInformationArray = new ParameterInformation[argCount]; ParameterInformation parameter = parameters[parameters.Length - 1]; Array.Copy((Array)parameters, (Array)parameterInformationArray, parameters.Length - 1); for (int index = parameters.Length - 1; index < argCount; ++index) { parameterInformationArray[index] = new ParameterInformation(elementType, false, (object)null, false); } return(parameterInformationArray); }
internal static ParameterInformation[] GetParameterInformation(System.Runtime.InteropServices.ComTypes.FUNCDESC funcdesc, bool skipLastParameter) { int cParams = funcdesc.cParams; if (skipLastParameter) { cParams--; } ParameterInformation[] informationArray = new ParameterInformation[cParams]; IntPtr lprgelemdescParam = funcdesc.lprgelemdescParam; int num2 = Marshal.SizeOf(typeof(System.Runtime.InteropServices.ComTypes.ELEMDESC)); for (int i = 0; i < cParams; i++) { IntPtr ptr2; bool isOptional = false; System.Runtime.InteropServices.ComTypes.ELEMDESC elemdesc = new System.Runtime.InteropServices.ComTypes.ELEMDESC(); int num4 = i * num2; if (IntPtr.Size == 4) { ptr2 = (IntPtr)(lprgelemdescParam.ToInt32() + num4); } else { ptr2 = (IntPtr)(lprgelemdescParam.ToInt64() + num4); } elemdesc = (System.Runtime.InteropServices.ComTypes.ELEMDESC)Marshal.PtrToStructure(ptr2, typeof(System.Runtime.InteropServices.ComTypes.ELEMDESC)); Type typeFromTypeDesc = GetTypeFromTypeDesc(elemdesc.tdesc); object defaultValue = null; if (((short)(elemdesc.desc.paramdesc.wParamFlags & (System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_NONE | System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_FOPT))) != 0) { isOptional = true; defaultValue = Type.Missing; } else { isOptional = false; } bool isByRef = false; if (((short)(elemdesc.desc.paramdesc.wParamFlags & (System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_NONE | System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_FOUT))) != 0) { isByRef = true; } informationArray[i] = new ParameterInformation(typeFromTypeDesc, isOptional, defaultValue, isByRef); } return(informationArray); }
internal static ParameterInformation[] GetParameterInformation( System.Runtime.InteropServices.ComTypes.FUNCDESC funcdesc, bool skipLastParameter) { int cParams = (int)funcdesc.cParams; if (skipLastParameter) { --cParams; } ParameterInformation[] parameterInformationArray = new ParameterInformation[cParams]; IntPtr lprgelemdescParam = funcdesc.lprgelemdescParam; int num1 = Marshal.SizeOf(typeof(System.Runtime.InteropServices.ComTypes.ELEMDESC)); for (int index = 0; index < cParams; ++index) { System.Runtime.InteropServices.ComTypes.ELEMDESC elemdesc = new System.Runtime.InteropServices.ComTypes.ELEMDESC(); int num2 = index * num1; elemdesc = (System.Runtime.InteropServices.ComTypes.ELEMDESC)Marshal.PtrToStructure(IntPtr.Size != 4 ? (IntPtr)(lprgelemdescParam.ToInt64() + (long)num2) : (IntPtr)(lprgelemdescParam.ToInt32() + num2), typeof(System.Runtime.InteropServices.ComTypes.ELEMDESC)); Type typeFromTypeDesc = ComUtil.GetTypeFromTypeDesc(elemdesc.tdesc); object defaultValue = (object)null; bool isOptional; if ((elemdesc.desc.paramdesc.wParamFlags & System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_FOPT) != System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_NONE) { isOptional = true; defaultValue = Type.Missing; } else { isOptional = false; } bool isByRef = false; if ((elemdesc.desc.paramdesc.wParamFlags & System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_FOUT) != System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_NONE) { isByRef = true; } parameterInformationArray[index] = new ParameterInformation(typeFromTypeDesc, isOptional, defaultValue, isByRef); } return(parameterInformationArray); }
protected override object MethodInvoke(PSMethod method, object[] arguments) { object[] objArray; Exception exception = null; ParameterInformation[] informationArray = new ParameterInformation[arguments.Length]; for (int i = 0; i < arguments.Length; i++) { informationArray[i] = new ParameterInformation(typeof(object), false, null, false); } MethodInformation[] methods = new MethodInformation[] { new MethodInformation(false, false, informationArray) }; Adapter.GetBestMethodAndArguments(method.Name, methods, arguments, out objArray); DirectoryEntry baseObject = (DirectoryEntry)method.baseObject; try { return(baseObject.Invoke(method.Name, objArray)); } #if !MONO catch (DirectoryServicesCOMException exception2) { exception = exception2; } #endif catch (TargetInvocationException exception3) { exception = exception3; } catch (COMException exception4) { exception = exception4; } PSMethod dotNetMethod = dotNetAdapter.GetDotNetMethod <PSMethod>(method.baseObject, method.name); if (dotNetMethod == null) { throw exception; } return(dotNetMethod.Invoke(arguments)); }
/// <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 = ClrFacade.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 enable 56515 ElementDescription = ClrFacade.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; }
internal static MethodInformation FindBestMethod( string methodName, MethodInformation[] methods, object[] arguments, out bool expandParamsOnBest) { if (methods.Length == 1 && !methods[0].hasVarArgs && methods[0].parameters.Length == arguments.Length) { expandParamsOnBest = false; return(methods[0]); } List <Adapter.OverloadCandidate> candidates = new List <Adapter.OverloadCandidate>(); for (int index1 = 0; index1 < methods.Length; ++index1) { MethodInformation method = methods[index1]; if (!method.isGeneric) { ParameterInformation[] parameters = method.parameters; if (arguments.Length != parameters.Length) { if (arguments.Length > parameters.Length) { if (!method.hasVarArgs) { continue; } } else if (method.hasOptional || method.hasVarArgs && arguments.Length + 1 == parameters.Length) { if (method.hasOptional) { int num = 0; for (int index2 = 0; index2 < parameters.Length; ++index2) { if (parameters[index2].isOptional) { ++num; } } if (arguments.Length + num < parameters.Length) { continue; } } } else { continue; } } Adapter.OverloadCandidate overloadCandidate = new Adapter.OverloadCandidate(method, arguments.Length); for (int index2 = 0; overloadCandidate != null && index2 < parameters.Length; ++index2) { ParameterInformation parameterInformation = parameters[index2]; if (!parameterInformation.isOptional || arguments.Length > index2) { if (parameterInformation.isParamArray) { Type elementType = parameterInformation.parameterType.GetElementType(); if (parameters.Length == arguments.Length) { ConversionRank argumentConversionRank1 = Adapter.GetArgumentConversionRank(arguments[index2], parameterInformation.parameterType); ConversionRank argumentConversionRank2 = Adapter.GetArgumentConversionRank(arguments[index2], elementType); if (argumentConversionRank2 > argumentConversionRank1) { overloadCandidate.expandedParameters = Adapter.ExpandParameters(arguments.Length, parameters, elementType); overloadCandidate.conversionRanks[index2] = argumentConversionRank2; } else { overloadCandidate.conversionRanks[index2] = argumentConversionRank1; } if (overloadCandidate.conversionRanks[index2] == ConversionRank.None) { overloadCandidate = (Adapter.OverloadCandidate)null; } } else { for (int index3 = index2; index3 < arguments.Length; ++index3) { overloadCandidate.conversionRanks[index3] = Adapter.GetArgumentConversionRank(arguments[index3], elementType); if (overloadCandidate.conversionRanks[index3] == ConversionRank.None) { overloadCandidate = (Adapter.OverloadCandidate)null; break; } } if (overloadCandidate != null) { overloadCandidate.expandedParameters = Adapter.ExpandParameters(arguments.Length, parameters, elementType); } } } else { overloadCandidate.conversionRanks[index2] = Adapter.GetArgumentConversionRank(arguments[index2], parameterInformation.parameterType); if (overloadCandidate.conversionRanks[index2] == ConversionRank.None) { overloadCandidate = (Adapter.OverloadCandidate)null; } } } else { break; } } if (overloadCandidate != null) { candidates.Add(overloadCandidate); } } } if (candidates.Count == 0) { throw new MethodException("MethodCountCouldNotFindBest", (Exception)null, "ExtendedTypeSystem", "MethodArgumentCountException", new object[2] { (object)methodName, (object)arguments.Length }); } Adapter.OverloadCandidate overloadCandidate1 = candidates.Count != 1 ? Adapter.FindBestCandidate(candidates, arguments) : candidates[0]; if (overloadCandidate1 != null) { expandParamsOnBest = overloadCandidate1.expandedParameters != null; return(overloadCandidate1.method); } throw new MethodException("MethodCountCouldNotFindBest", (Exception)null, "ExtendedTypeSystem", "MethodAmbiguousException", new object[2] { (object)methodName, (object)arguments.Length }); }
internal MethodInformation(bool hasvarargs, bool hasoptional, ParameterInformation[] arguments) { this.hasVarArgs = hasvarargs; this.hasOptional = hasoptional; this.parameters = arguments; }
/// <summary> /// Generate the ByRef array indicating whether the corresponding argument is by-reference. /// </summary> /// <param name="parameters">Parameters retrieved from metadata</param> /// <param name="argumentCount">Count of arguments to pass in IDispatch.Invoke</param> /// <param name="isPropertySet">Indicate if we are handling arguments for PropertyPut/PropertyPutRef</param> /// <returns></returns> internal static bool[] GetByRefArray(ParameterInformation[] parameters, int argumentCount, bool isPropertySet) { if (parameters.Length == 0) { return null; } var byRef = new bool[argumentCount]; int argsToProcess = argumentCount; if (isPropertySet) { // If it's PropertySet, then the last value in arguments is the right-hand side value. // There is no corresponding parameter for that value, so it's for sure not by-ref. // Hence, set the last item of byRef array to be false. argsToProcess = argumentCount - 1; byRef[argsToProcess] = false; } Diagnostics.Assert(parameters.Length >= argsToProcess, "There might be more parameters than argsToProcess due unspecified optional arguments"); for (int i = 0; i < argsToProcess; i++) { byRef[i] = parameters[i].isByRef; } return byRef; }
internal static ParameterModifier[] GetModifiers(ParameterInformation[] parameters) { int length = parameters.Length; if (parameters.Length == 0) { return null; } ParameterModifier modifier = new ParameterModifier(length); for (int i = 0; i < length; i++) { modifier[i] = parameters[i].isByRef; } return new ParameterModifier[] { modifier }; }
internal static ParameterInformation[] GetParameterInformation(System.Runtime.InteropServices.ComTypes.FUNCDESC funcdesc, bool skipLastParameter) { int cParams = funcdesc.cParams; if (skipLastParameter) { cParams--; } ParameterInformation[] informationArray = new ParameterInformation[cParams]; IntPtr lprgelemdescParam = funcdesc.lprgelemdescParam; int num2 = Marshal.SizeOf(typeof(System.Runtime.InteropServices.ComTypes.ELEMDESC)); for (int i = 0; i < cParams; i++) { IntPtr ptr2; bool isOptional = false; System.Runtime.InteropServices.ComTypes.ELEMDESC elemdesc = new System.Runtime.InteropServices.ComTypes.ELEMDESC(); int num4 = i * num2; if (IntPtr.Size == 4) { ptr2 = (IntPtr) (lprgelemdescParam.ToInt32() + num4); } else { ptr2 = (IntPtr) (lprgelemdescParam.ToInt64() + num4); } elemdesc = (System.Runtime.InteropServices.ComTypes.ELEMDESC) Marshal.PtrToStructure(ptr2, typeof(System.Runtime.InteropServices.ComTypes.ELEMDESC)); Type typeFromTypeDesc = GetTypeFromTypeDesc(elemdesc.tdesc); object defaultValue = null; if (((short) (elemdesc.desc.paramdesc.wParamFlags & (System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_NONE | System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_FOPT))) != 0) { isOptional = true; defaultValue = Type.Missing; } else { isOptional = false; } bool isByRef = false; if (((short) (elemdesc.desc.paramdesc.wParamFlags & (System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_NONE | System.Runtime.InteropServices.ComTypes.PARAMFLAG.PARAMFLAG_FOUT))) != 0) { isByRef = true; } informationArray[i] = new ParameterInformation(typeFromTypeDesc, isOptional, defaultValue, isByRef); } return informationArray; }
internal static object[] GetMethodArgumentsBase( string methodName, ParameterInformation[] parameters, object[] arguments, bool expandParamsOnBest) { int length1 = parameters.Length; if (length1 == 0) { return(new object[0]); } object[] newArguments = new object[length1]; for (int index = 0; index < length1 - 1; ++index) { ParameterInformation parameter = parameters[index]; Adapter.SetNewArgument(methodName, arguments, newArguments, parameter, index); } ParameterInformation parameter1 = parameters[length1 - 1]; if (!expandParamsOnBest) { Adapter.SetNewArgument(methodName, arguments, newArguments, parameter1, length1 - 1); return(newArguments); } if (arguments.Length < length1) { newArguments[length1 - 1] = (object)new ArrayList().ToArray(parameter1.parameterType.GetElementType()); return(newArguments); } int length2 = arguments.Length - length1 + 1; if (length2 == 1 && arguments[arguments.Length - 1] == null) { newArguments[length1 - 1] = (object)null; } else { object[] objArray = new object[length2]; Type elementType = parameter1.parameterType.GetElementType(); for (int index = 0; index < length2; ++index) { int parameterIndex = index + length1 - 1; try { objArray[index] = Adapter.MethodArgumentConvertTo(arguments[parameterIndex], false, parameterIndex, elementType, (IFormatProvider)CultureInfo.InvariantCulture); } catch (InvalidCastException ex) { throw new MethodException("MethodArgumentConversionInvalidCastArgument", (Exception)ex, "ExtendedTypeSystem", "MethodArgumentConversionException", new object[5] { (object)parameterIndex, arguments[parameterIndex], (object)methodName, (object)elementType, (object)ex.Message }); } } try { newArguments[length1 - 1] = Adapter.MethodArgumentConvertTo((object)objArray, parameter1.isByRef, length1 - 1, parameter1.parameterType, (IFormatProvider)CultureInfo.InvariantCulture); } catch (InvalidCastException ex) { throw new MethodException("MethodArgumentConversionParamsConversion", (Exception)ex, "ExtendedTypeSystem", "MethodArgumentConversionException", new object[5] { (object)(length1 - 1), (object)objArray, (object)methodName, (object)parameter1.parameterType, (object)ex.Message }); } } return(newArguments); }
internal ComMethodInformation(bool hasvarargs, bool hasoptional, ParameterInformation[] arguments, Type returnType) : base(hasvarargs, hasoptional, arguments) { this.returnType = returnType; }