public static void SetProperty(this IDispatch dispatch, string name, params object[] args) { if (args.Length < 1) { throw new ArgumentException("Invalid argument count", nameof(args)); } var dispid = dispatch.GetDispIDForName(name); using (var argVariantArrayBlock = new CoTaskMemVariantArgsBlock(args)) { using (var namedArgDispidBlock = new CoTaskMemBlock(sizeof(int))) { Marshal.WriteInt32(namedArgDispidBlock.Addr, SpecialDispIDs.PropertyPut); var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 1, rgdispidNamedArgs = namedArgDispidBlock.Addr }; var result = dispatch.Invoke(dispid, ref iid, 0, DispatchFlags.PropertyPut | DispatchFlags.PropertyPutRef, ref dispArgs, IntPtr.Zero, out _, out _); if (result == HResult.DISP_E_MEMBERNOTFOUND) { // VBScript objects can be finicky about property-put dispatch flags result = dispatch.Invoke(dispid, iid, 0, DispatchFlags.PropertyPut, ref dispArgs, IntPtr.Zero, out _, out _); if (result == HResult.DISP_E_MEMBERNOTFOUND) { result = dispatch.Invoke(dispid, iid, 0, DispatchFlags.PropertyPutRef, ref dispArgs, IntPtr.Zero, out _, out _); } } HResult.Check(result); } } }
public static object GetProperty(this IDispatch dispatch, string name, params object[] args) { if (!MiscHelpers.Try(out int dispid, () => dispatch.GetDispIDForName(name))) { return(Nonexistent.Value); } using (var argVariantArrayBlock = new CoTaskMemVariantArgsBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatch.Invoke(dispid, ref iid, 0, DispatchFlags.PropertyGet, ref dispArgs, resultVariantBlock.Addr, out _, out _)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }
public static object InvokeMethod(this IDispatch dispatch, string name, params object[] args) { var dispid = dispatch.GetDispIDForName(name); if (dispid == SpecialDispIDs.GetEnumerator) { return(dispatch.GetProperty(SpecialMemberNames.NewEnum, args)); } using (var argVariantArrayBlock = new CoTaskMemVariantArgsByRefBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatch.Invoke(dispid, iid, 0, DispatchFlags.Method, ref dispArgs, resultVariantBlock.Addr, out _, out _)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }