File size: 5,209 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.ObjectModel;
using System.Management.Automation.Internal;
using System.Reflection;
using System.Runtime.InteropServices;
using COM = System.Runtime.InteropServices.ComTypes;
namespace System.Management.Automation
{
internal class ComMethodInformation : MethodInformation
{
internal readonly Type ReturnType;
internal readonly int DispId;
internal readonly COM.INVOKEKIND InvokeKind;
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;
}
}
/// <summary>
/// Defines a method in the COM object.
/// </summary>
internal class ComMethod
{
private readonly Collection<int> _methods = new Collection<int>();
private readonly COM.ITypeInfo _typeInfo;
/// <summary>
/// Initializes new instance of ComMethod class.
/// </summary>
internal ComMethod(COM.ITypeInfo typeinfo, string name)
{
_typeInfo = typeinfo;
Name = name;
}
/// <summary>
/// Defines the name of the method.
/// </summary>
internal string Name { get; }
/// <summary>
/// Updates funcdesc for method information.
/// </summary>
/// <param name="index">Index of funcdesc for method in type information.</param>
internal void AddFuncDesc(int index)
{
_methods.Add(index);
}
/// <summary>
/// Returns the different method overloads signatures.
/// </summary>
/// <returns></returns>
internal Collection<string> MethodDefinitions()
{
Collection<string> result = new Collection<string>();
foreach (int index in _methods)
{
IntPtr pFuncDesc;
_typeInfo.GetFuncDesc(index, out pFuncDesc);
COM.FUNCDESC funcdesc = Marshal.PtrToStructure<COM.FUNCDESC>(pFuncDesc);
string signature = ComUtil.GetMethodSignatureFromFuncDesc(_typeInfo, funcdesc, false);
result.Add(signature);
_typeInfo.ReleaseFuncDesc(pFuncDesc);
}
return result;
}
/// <summary>
/// Invokes the method on object.
/// </summary>
/// <param name="method">Represents the instance of the method we want to invoke.</param>
/// <param name="arguments">Parameters to be passed to the method.</param>
/// <returns>Returns the value of method call.</returns>
internal object InvokeMethod(PSMethod method, object[] arguments)
{
try
{
object[] newarguments;
var methods = ComUtil.GetMethodInformationArray(_typeInfo, _methods, false);
var bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);
object returnValue = ComInvoker.Invoke(method.baseObject as IDispatch,
bestMethod.DispId, newarguments,
ComInvoker.GetByRefArray(bestMethod.parameters,
newarguments.Length,
isPropertySet: false),
COM.INVOKEKIND.INVOKE_FUNC);
Adapter.SetReferences(newarguments, bestMethod, arguments);
return bestMethod.ReturnType != typeof(void) ? returnValue : AutomationNull.Value;
}
catch (TargetInvocationException te)
{
// First check if this is a severe exception.
var innerCom = te.InnerException as COMException;
if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
{
string message = te.InnerException == null ? te.Message : te.InnerException.Message;
throw new MethodInvocationException(
"ComMethodTargetInvocation",
te,
ExtendedTypeSystem.MethodInvocationException,
method.Name, arguments.Length, message);
}
}
catch (COMException ce)
{
if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
{
throw new MethodInvocationException(
"ComMethodCOMException",
ce,
ExtendedTypeSystem.MethodInvocationException,
method.Name, arguments.Length, ce.Message);
}
}
return null;
}
}
}
|