Esempio n. 1
0
        internal static ComTypeInfo GetDispatchTypeInfo(object comObject)
        {
            ComTypeInfo result = null;
            IDispatch   disp   = comObject as IDispatch;

            if (disp != null)
            {
                COM.ITypeInfo typeinfo = null;
                disp.GetTypeInfo(0, 0, out typeinfo);
                if (typeinfo != null)
                {
                    COM.TYPEATTR typeattr = GetTypeAttr(typeinfo);

                    if ((typeattr.typekind == COM.TYPEKIND.TKIND_INTERFACE))
                    {
                        // We have typeinfo for custom interface. Get typeinfo for Dispatch interface.
                        typeinfo = GetDispatchTypeInfoFromCustomInterfaceTypeInfo(typeinfo);
                    }

                    if ((typeattr.typekind == COM.TYPEKIND.TKIND_COCLASS))
                    {
                        // We have typeinfo for the COClass.  Find the default interface and get typeinfo for default interface.
                        typeinfo = GetDispatchTypeInfoFromCoClassTypeInfo(typeinfo);
                    }

                    result = new ComTypeInfo(typeinfo);
                }
            }

            return(result);
        }
Esempio n. 2
0
 protected override T GetMember <T>(object obj, string memberName)
 {
     using (ComAdapter.tracer.TraceMethod())
     {
         ComTypeInfo typeInfo = this.GetTypeInfo();
         if (typeInfo != null && typeInfo.Properties.ContainsKey(memberName))
         {
             ComProperty property = typeInfo.Properties[memberName];
             if (property.IsParameterized)
             {
                 if (typeof(T).IsAssignableFrom(typeof(PSParameterizedProperty)))
                 {
                     return(new PSParameterizedProperty(property.Name, (Adapter)this, obj, (object)property) as T);
                 }
             }
             else if (typeof(T).IsAssignableFrom(typeof(PSProperty)))
             {
                 return(new PSProperty(property.Name, (Adapter)this, obj, (object)property) as T);
             }
         }
         if (!typeof(T).IsAssignableFrom(typeof(PSMethod)) || typeInfo == null || !typeInfo.Methods.ContainsKey(memberName))
         {
             return(default(T));
         }
         ComMethod method = typeInfo.Methods[memberName];
         return(new PSMethod(method.Name, (Adapter)this, obj, (object)method) as T);
     }
 }
Esempio n. 3
0
        internal static ComTypeInfo GetDispatchTypeInfo(object comObject)
        {
            ComTypeInfo info = null;

            System.Management.Automation.IDispatch dispatch = comObject as System.Management.Automation.IDispatch;
            if (dispatch == null)
            {
                return(info);
            }
            ITypeInfo ppTInfo = null;

            dispatch.GetTypeInfo(0, 0, out ppTInfo);
            if (ppTInfo == null)
            {
                return(info);
            }
            System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = GetTypeAttr(ppTInfo);
            if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_INTERFACE)
            {
                ppTInfo = GetDispatchTypeInfoFromCustomInterfaceTypeInfo(ppTInfo);
            }
            if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS)
            {
                ppTInfo = GetDispatchTypeInfoFromCoClassTypeInfo(ppTInfo);
            }
            return(new ComTypeInfo(ppTInfo));
        }
Esempio n. 4
0
        private void Initialize()
        {
            using (ComTypeInfo.tracer.TraceMethod())
            {
                if (this.typeinfo == null)
                {
                    return;
                }
                System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = ComTypeInfo.GetTypeAttr(this.typeinfo);
                this.guid = typeAttr.guid;
                for (int firstUserMethod = ComTypeInfo.FindFirstUserMethod(typeAttr); firstUserMethod < (int)typeAttr.cFuncs; ++firstUserMethod)
                {
                    System.Runtime.InteropServices.ComTypes.FUNCDESC funcDesc = ComTypeInfo.GetFuncDesc(this.typeinfo, firstUserMethod);
                    string nameFromFuncDesc = ComUtil.GetNameFromFuncDesc(this.typeinfo, funcDesc);
                    switch (funcDesc.invkind)
                    {
                    case System.Runtime.InteropServices.ComTypes.INVOKEKIND.INVOKE_FUNC:
                        this.AddMethod(nameFromFuncDesc, firstUserMethod);
                        break;

                    case System.Runtime.InteropServices.ComTypes.INVOKEKIND.INVOKE_PROPERTYGET:
                    case System.Runtime.InteropServices.ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT:
                    case System.Runtime.InteropServices.ComTypes.INVOKEKIND.INVOKE_PROPERTYPUTREF:
                        this.AddProperty(nameFromFuncDesc, funcDesc, firstUserMethod);
                        break;
                    }
                }
            }
        }
Esempio n. 5
0
        internal static ITypeInfo GetDispatchTypeInfoFromCoClassTypeInfo(ITypeInfo typeinfo)
        {
            int       cImplTypes = (int)ComTypeInfo.GetTypeAttr(typeinfo).cImplTypes;
            ITypeInfo ppTI       = (ITypeInfo)null;

            for (int index = 0; index < cImplTypes; ++index)
            {
                int href;
                typeinfo.GetRefTypeOfImplType(index, out href);
                typeinfo.GetRefTypeInfo(href, out ppTI);
                System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = ComTypeInfo.GetTypeAttr(ppTI);
                if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_DISPATCH)
                {
                    return(ppTI);
                }
                if ((typeAttr.wTypeFlags & System.Runtime.InteropServices.ComTypes.TYPEFLAGS.TYPEFLAG_FDUAL) != (System.Runtime.InteropServices.ComTypes.TYPEFLAGS) 0)
                {
                    ppTI = ComTypeInfo.GetDispatchTypeInfoFromCustomInterfaceTypeInfo(ppTI);
                    if (ComTypeInfo.GetTypeAttr(ppTI).typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_DISPATCH)
                    {
                        return(ppTI);
                    }
                }
            }
            return((ITypeInfo)null);
        }
Esempio n. 6
0
 internal ComAdapter(object o, ComTypeInfo typeinfo)
 {
     using (ComAdapter.tracer.TraceConstructor((object)this))
     {
         this.comObject = o;
         this.info      = typeinfo;
     }
 }
Esempio n. 7
0
 private ComTypeInfo GetTypeInfo()
 {
     using (ComAdapter.tracer.TraceMethod())
     {
         if (this.info == null)
         {
             this.info = ComTypeInfo.GetDispatchTypeInfo(this.comObject);
         }
         return(this.info);
     }
 }
Esempio n. 8
0
        /// <summary>
        /// Try to create an enumerator for a COM object.
        /// </summary>
        /// <returns>
        /// A 'ComEnumerator' instance, or null if we cannot create an enumerator for the COM object.
        /// </returns>
        internal static ComEnumerator Create(object comObject)
        {
            if (comObject == null || !comObject.GetType().IsCOMObject)
            {
                return(null);
            }

            // The passed-in COM object could already be a IEnumVARIANT interface.
            // e.g. user call '_NewEnum()' on a COM collection interface.
            var enumVariant = comObject as COM.IEnumVARIANT;

            if (enumVariant != null)
            {
                return(new ComEnumerator(enumVariant));
            }

            // The passed-in COM object could be a collection.
            var enumerable = comObject as IEnumerable;
            var target     = comObject as IDispatch;

            if (enumerable != null && target != null)
            {
                try
                {
                    var comTypeInfo = ComTypeInfo.GetDispatchTypeInfo(comObject);
                    if (comTypeInfo != null && comTypeInfo.NewEnumInvokeKind.HasValue)
                    {
                        // The COM object is a collection and also a IDispatch interface, so we try to get a
                        // IEnumVARIANT interface out of it by invoking its '_NewEnum (DispId: -4)' function.
                        var result = ComInvoker.Invoke(target, ComTypeInfo.DISPID_NEWENUM,
                                                       args: Utils.EmptyArray <object>(), byRef: null,
                                                       invokeKind: comTypeInfo.NewEnumInvokeKind.Value);
                        enumVariant = result as COM.IEnumVARIANT;
                        if (enumVariant != null)
                        {
                            return(new ComEnumerator(enumVariant));
                        }
                    }
                }
                catch (Exception)
                {
                    // Ignore exceptions. In case of exception, no enumerator can be created
                    // for the passed-in COM object, and we will return null eventually.
                }
            }

            return(null);
        }
Esempio n. 9
0
 protected override PSMemberInfoInternalCollection <T> GetMembers <T>(
     object obj)
 {
     using (ComAdapter.tracer.TraceMethod())
     {
         ComTypeInfo typeInfo = this.GetTypeInfo();
         PSMemberInfoInternalCollection <T> internalCollection = new PSMemberInfoInternalCollection <T>();
         if (typeInfo != null)
         {
             bool flag1 = typeof(T).IsAssignableFrom(typeof(PSProperty));
             bool flag2 = typeof(T).IsAssignableFrom(typeof(PSParameterizedProperty));
             if (flag1 || flag2)
             {
                 foreach (ComProperty comProperty in typeInfo.Properties.Values)
                 {
                     if (comProperty.IsParameterized)
                     {
                         if (flag2)
                         {
                             internalCollection.Add(new PSParameterizedProperty(comProperty.Name, (Adapter)this, obj, (object)comProperty) as T);
                         }
                     }
                     else if (flag1)
                     {
                         internalCollection.Add(new PSProperty(comProperty.Name, (Adapter)this, obj, (object)comProperty) as T);
                     }
                 }
             }
             if (typeof(T).IsAssignableFrom(typeof(PSMethod)))
             {
                 foreach (ComMethod comMethod in typeInfo.Methods.Values)
                 {
                     PSMethod psMethod = new PSMethod(comMethod.Name, (Adapter)this, obj, (object)comMethod);
                     if (!internalCollection.hashedMembers.Contains((object)comMethod.Name))
                     {
                         internalCollection.Add(psMethod as T);
                     }
                 }
             }
         }
         return(internalCollection);
     }
 }
Esempio n. 10
0
        internal static ComTypeInfo GetDispatchTypeInfo(object comObject)
        {
            ComTypeInfo comTypeInfo = (ComTypeInfo)null;

            if (comObject is IDispatch dispatch)
            {
                ITypeInfo ppTInfo = (ITypeInfo)null;
                dispatch.GetTypeInfo(0, 0, out ppTInfo);
                if (ppTInfo != null)
                {
                    System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = ComTypeInfo.GetTypeAttr(ppTInfo);
                    if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_INTERFACE)
                    {
                        ppTInfo = ComTypeInfo.GetDispatchTypeInfoFromCustomInterfaceTypeInfo(ppTInfo);
                    }
                    if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS)
                    {
                        ppTInfo = ComTypeInfo.GetDispatchTypeInfoFromCoClassTypeInfo(ppTInfo);
                    }
                    comTypeInfo = new ComTypeInfo(ppTInfo);
                }
            }
            return(comTypeInfo);
        }
 internal DotNetAdapterWithComTypeName(ComTypeInfo comTypeInfo)
 {
     this.comTypeInfo = comTypeInfo;
 }
Esempio n. 12
0
 /// <summary>
 ///   Constructor for the ComAdapter
 /// </summary>
 /// <param name="typeinfo">typeinfo for the com object we are adapting</param>
 internal ComAdapter(ComTypeInfo typeinfo)
 {
     Diagnostics.Assert(typeinfo != null, "Caller to verify typeinfo is not null.");
     _comTypeInfo = typeinfo;
 }
Esempio n. 13
0
 internal ComAdapter(ComTypeInfo typeinfo)
 {
     this._comTypeInfo = typeinfo;
 }
Esempio n. 14
0
        internal static ComTypeInfo GetDispatchTypeInfo(object comObject)
        {
            ComTypeInfo result = null;
            IDispatch disp = comObject as IDispatch;
            if (disp != null)
            {
                COM.ITypeInfo typeinfo = null;
                disp.GetTypeInfo(0, 0, out typeinfo);
                if (typeinfo != null)
                {
                    COM.TYPEATTR typeattr = GetTypeAttr(typeinfo);

                    if ((typeattr.typekind == COM.TYPEKIND.TKIND_INTERFACE))
                    {
                        //We have typeinfo for custom interface. Get typeinfo for Dispatch interface.
                        typeinfo = GetDispatchTypeInfoFromCustomInterfaceTypeInfo(typeinfo);
                    }

                    if ((typeattr.typekind == COM.TYPEKIND.TKIND_COCLASS))
                    {
                        //We have typeinfo for the COClass.  Find the default interface and get typeinfo for default interface.
                        typeinfo = GetDispatchTypeInfoFromCoClassTypeInfo(typeinfo);
                    }
                    result = new ComTypeInfo(typeinfo);
                }
            }
            return result;
        }
Esempio n. 15
0
 internal DotNetAdapterWithComTypeName(ComTypeInfo comTypeInfo) => this.comTypeInfo = comTypeInfo;