コード例 #1
0
        private static int GetDispIDForName(this IDispatchEx dispatchEx, string name, bool ensure, bool ignoreCase)
        {
            var flags = ignoreCase ? DispatchNameFlags.CaseInsensitive : DispatchNameFlags.CaseSensitive;

            if (ensure)
            {
                flags |= DispatchNameFlags.Ensure;
            }

            int dispid;
            var result = dispatchEx.GetDispID(name, flags, out dispid);

            if (ensure && (result == HResult.DISP_E_UNKNOWNNAME))
            {
                throw new NotSupportedException("The object does not support dynamic properties");
            }

            if (HResult.Succeeded(result))
            {
                return(dispid);
            }

            if (name.IsDispIDName(out dispid))
            {
                return(dispid);
            }

            var member = dispatchEx.GetMembers().FirstOrDefault(testMember => testMember.Name.Equals(name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));

            if (member != null)
            {
                return(member.DispID);
            }

            throw new MissingMemberException(MiscHelpers.FormatInvariant("The object has no property named '{0}'", name));
        }
コード例 #2
0
        public static void SetProperty(this IDispatch dispatch, string name, params object[] args)
        {
            if (args.Length < 1)
            {
                throw new ArgumentException("Invalid argument count", "args");
            }

            var dispid = dispatch.GetDispIDForName(name);

            using (var argVariantArrayBlock = new CoTaskMemVariantArgsBlock(args))
            {
                using (var namedArgDispidBlock = new CoTaskMemBlock(sizeof(int)))
                {
                    EXCEPINFO excepInfo;
                    Marshal.WriteInt32(namedArgDispidBlock.Addr, SpecialDispIDs.PropertyPut);
                    var dispArgs = new DISPPARAMS {
                        cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 1, rgdispidNamedArgs = namedArgDispidBlock.Addr
                    };

                    uint argErr;
                    var  result = dispatch.Invoke(dispid, ref iid, 0, DispatchFlags.PropertyPut | DispatchFlags.PropertyPutRef, ref dispArgs, IntPtr.Zero, out excepInfo, out argErr);
                    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 excepInfo, out argErr);
                        if (result == HResult.DISP_E_MEMBERNOTFOUND)
                        {
                            result = dispatch.Invoke(dispid, iid, 0, DispatchFlags.PropertyPutRef, ref dispArgs, IntPtr.Zero, out excepInfo, out argErr);
                        }
                    }

                    HResult.Check(result);
                }
            }
        }
コード例 #3
0
        private static int GetDispIDForName(this IDispatch dispatch, string name)
        {
            var dispids = new int[1];
            var names   = new[] { name };

            if (HResult.Succeeded(dispatch.GetIDsOfNames(ref iid, names, 1, 0, dispids)))
            {
                return(dispids[0]);
            }

            if (name.IsDispIDName(out dispids[0]))
            {
                return(dispids[0]);
            }

            var member = dispatch.GetMembers().FirstOrDefault(testMember => testMember.Name == name);

            if (member == null)
            {
                throw new MissingMemberException(MiscHelpers.FormatInvariant("The object has no property named '{0}'", name));
            }

            return(member.DispID);
        }