Exemplo n.º 1
0
 public static void Set(string key, Common.DbDataTableInfo value)
 {
     if (string.IsNullOrEmpty(key))
     {
         throw new Exception("key不能为空");
     }
     key = key.ToLower();
     if (CacheDic.ContainsKey(key))
     {
         CacheDic[key] = new StructCache {
             CacheTime = DateTime.Now, CacheData = value
         };
     }
     else
     {
         CacheDic.Add(key, new StructCache {
             CacheTime = DateTime.Now, CacheData = value
         });
     }
 }
Exemplo n.º 2
0
        private static Guid GetGuidForTypeInfo(UnsafeNativeMethods.ITypeInfo typeInfo, StructCache structCache, int[] versions)
        {
            IntPtr zero     = IntPtr.Zero;
            int    typeAttr = typeInfo.GetTypeAttr(ref zero);

            if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr))
            {
                throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
            }
            Guid empty = Guid.Empty;

            System.Windows.Forms.NativeMethods.tagTYPEATTR data = null;
            try
            {
                if (structCache == null)
                {
                    data = new System.Windows.Forms.NativeMethods.tagTYPEATTR();
                }
                else
                {
                    data = (System.Windows.Forms.NativeMethods.tagTYPEATTR)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
                }
                UnsafeNativeMethods.PtrToStructure(zero, data);
                empty = data.guid;
                if (versions != null)
                {
                    versions[0] = data.wMajorVerNum;
                    versions[1] = data.wMinorVerNum;
                }
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(zero);
                if ((structCache != null) && (data != null))
                {
                    structCache.ReleaseStruct(data);
                }
            }
            return(empty);
        }
Exemplo n.º 3
0
        private static void ProcessVariables(UnsafeNativeMethods.ITypeInfo typeInfo, IDictionary propInfoList, int dispidToGet, int nameDispID, StructCache structCache)
        {
            IntPtr zero     = IntPtr.Zero;
            int    typeAttr = typeInfo.GetTypeAttr(ref zero);

            if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr) || (zero == IntPtr.Zero))
            {
                throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
            }
            System.Windows.Forms.NativeMethods.tagTYPEATTR data = (System.Windows.Forms.NativeMethods.tagTYPEATTR)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
            UnsafeNativeMethods.PtrToStructure(zero, data);
            try
            {
                if (data != null)
                {
                    System.Windows.Forms.NativeMethods.tagVARDESC gvardesc = (System.Windows.Forms.NativeMethods.tagVARDESC)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagVARDESC));
                    for (int i = 0; i < data.cVars; i++)
                    {
                        IntPtr pVarDesc = IntPtr.Zero;
                        if (System.Windows.Forms.NativeMethods.Succeeded(typeInfo.GetVarDesc(i, ref pVarDesc)) && (pVarDesc != IntPtr.Zero))
                        {
                            UnsafeNativeMethods.PtrToStructure(pVarDesc, gvardesc);
                            try
                            {
                                if ((gvardesc.varkind != 2) && ((dispidToGet == -1) || (gvardesc.memid == dispidToGet)))
                                {
                                    PropInfo info = ProcessDataCore(typeInfo, propInfoList, gvardesc.memid, nameDispID, gvardesc.elemdescVar.tdesc, gvardesc.wVarFlags, structCache);
                                    if (info.ReadOnly != 1)
                                    {
                                        info.ReadOnly = 2;
                                    }
                                }
                            }
                            finally
                            {
                                if (pVarDesc != IntPtr.Zero)
                                {
                                    typeInfo.ReleaseVarDesc(pVarDesc);
                                }
                            }
                        }
                    }
                    structCache.ReleaseStruct(gvardesc);
                }
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(zero);
                structCache.ReleaseStruct(data);
            }
        }
Exemplo n.º 4
0
 private static System.Type ProcessTypeInfoEnum(UnsafeNativeMethods.ITypeInfo enumTypeInfo, StructCache structCache)
 {
     if (enumTypeInfo != null)
     {
         try
         {
             IntPtr zero     = IntPtr.Zero;
             int    typeAttr = enumTypeInfo.GetTypeAttr(ref zero);
             if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr) || (zero == IntPtr.Zero))
             {
                 throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
             }
             System.Windows.Forms.NativeMethods.tagTYPEATTR data = (System.Windows.Forms.NativeMethods.tagTYPEATTR)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
             UnsafeNativeMethods.PtrToStructure(zero, data);
             if (zero == IntPtr.Zero)
             {
                 return(null);
             }
             try
             {
                 int       cVars = data.cVars;
                 ArrayList list  = new ArrayList();
                 ArrayList list2 = new ArrayList();
                 System.Windows.Forms.NativeMethods.tagVARDESC gvardesc = (System.Windows.Forms.NativeMethods.tagVARDESC)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagVARDESC));
                 object objectForNativeVariant = null;
                 string pBstrName      = null;
                 string pBstrDocString = null;
                 enumTypeInfo.GetDocumentation(-1, ref pBstrName, ref pBstrDocString, null, null);
                 for (int i = 0; i < cVars; i++)
                 {
                     IntPtr pVarDesc = IntPtr.Zero;
                     if (System.Windows.Forms.NativeMethods.Succeeded(enumTypeInfo.GetVarDesc(i, ref pVarDesc)) && (pVarDesc != IntPtr.Zero))
                     {
                         try
                         {
                             UnsafeNativeMethods.PtrToStructure(pVarDesc, gvardesc);
                             if (((gvardesc != null) && (gvardesc.varkind == 2)) && (gvardesc.unionMember != IntPtr.Zero))
                             {
                                 str2 = (string)(pBstrDocString = null);
                                 objectForNativeVariant = null;
                                 if (System.Windows.Forms.NativeMethods.Succeeded(enumTypeInfo.GetDocumentation(gvardesc.memid, null, ref pBstrDocString, null, null)))
                                 {
                                     string str4;
                                     try
                                     {
                                         objectForNativeVariant = Marshal.GetObjectForNativeVariant(gvardesc.unionMember);
                                     }
                                     catch (Exception)
                                     {
                                     }
                                     list2.Add(objectForNativeVariant);
                                     if (pBstrDocString != null)
                                     {
                                         str4 = pBstrDocString;
                                     }
                                     else
                                     {
                                         str4 = str2;
                                     }
                                     list.Add(str4);
                                 }
                             }
                         }
                         finally
                         {
                             if (pVarDesc != IntPtr.Zero)
                             {
                                 enumTypeInfo.ReleaseVarDesc(pVarDesc);
                             }
                         }
                     }
                 }
                 structCache.ReleaseStruct(gvardesc);
                 if (list.Count > 0)
                 {
                     IntPtr iUnknownForObject = Marshal.GetIUnknownForObject(enumTypeInfo);
                     try
                     {
                         pBstrName = iUnknownForObject.ToString() + "_" + pBstrName;
                         if (builtEnums == null)
                         {
                             builtEnums = new Hashtable();
                         }
                         else if (builtEnums.ContainsKey(pBstrName))
                         {
                             return((System.Type)builtEnums[pBstrName]);
                         }
                         System.Type underlyingType = typeof(int);
                         if ((list2.Count > 0) && (list2[0] != null))
                         {
                             underlyingType = list2[0].GetType();
                         }
                         EnumBuilder builder = ModuleBuilder.DefineEnum(pBstrName, TypeAttributes.Public, underlyingType);
                         for (int j = 0; j < list.Count; j++)
                         {
                             builder.DefineLiteral((string)list[j], list2[j]);
                         }
                         System.Type type2 = builder.CreateType();
                         builtEnums[pBstrName] = type2;
                         return(type2);
                     }
                     finally
                     {
                         if (iUnknownForObject != IntPtr.Zero)
                         {
                             Marshal.Release(iUnknownForObject);
                         }
                     }
                 }
             }
             finally
             {
                 enumTypeInfo.ReleaseTypeAttr(zero);
                 structCache.ReleaseStruct(data);
             }
         }
         catch
         {
         }
     }
     return(null);
 }
Exemplo n.º 5
0
        private static void ProcessFunctions(UnsafeNativeMethods.ITypeInfo typeInfo, IDictionary propInfoList, int dispidToGet, int nameDispID, ref bool addAboutBox, StructCache structCache)
        {
            IntPtr zero     = IntPtr.Zero;
            int    typeAttr = typeInfo.GetTypeAttr(ref zero);

            if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr) || (zero == IntPtr.Zero))
            {
                throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
            }
            System.Windows.Forms.NativeMethods.tagTYPEATTR data = (System.Windows.Forms.NativeMethods.tagTYPEATTR)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
            UnsafeNativeMethods.PtrToStructure(zero, data);
            try
            {
                if (data != null)
                {
                    System.Windows.Forms.NativeMethods.tagFUNCDESC gfuncdesc = (System.Windows.Forms.NativeMethods.tagFUNCDESC)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagFUNCDESC));
                    System.Windows.Forms.NativeMethods.tagELEMDESC structure = (System.Windows.Forms.NativeMethods.tagELEMDESC)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagELEMDESC));
                    for (int i = 0; i < data.cFuncs; i++)
                    {
                        IntPtr pFuncDesc = IntPtr.Zero;
                        if (System.Windows.Forms.NativeMethods.Succeeded(typeInfo.GetFuncDesc(i, ref pFuncDesc)) && (pFuncDesc != IntPtr.Zero))
                        {
                            UnsafeNativeMethods.PtrToStructure(pFuncDesc, gfuncdesc);
                            try
                            {
                                if ((gfuncdesc.invkind == 1) || ((dispidToGet != -1) && (gfuncdesc.memid != dispidToGet)))
                                {
                                    if (gfuncdesc.memid == -552)
                                    {
                                        addAboutBox = true;
                                    }
                                }
                                else
                                {
                                    System.Windows.Forms.NativeMethods.tagTYPEDESC tdesc;
                                    bool flag = gfuncdesc.invkind == 2;
                                    if (flag)
                                    {
                                        if (gfuncdesc.cParams != 0)
                                        {
                                            continue;
                                        }
                                        tdesc = gfuncdesc.elemdescFunc.tdesc;
                                    }
                                    else
                                    {
                                        if ((gfuncdesc.lprgelemdescParam == IntPtr.Zero) || (gfuncdesc.cParams != 1))
                                        {
                                            continue;
                                        }
                                        Marshal.PtrToStructure(gfuncdesc.lprgelemdescParam, structure);
                                        tdesc = structure.tdesc;
                                    }
                                    PropInfo info = ProcessDataCore(typeInfo, propInfoList, gfuncdesc.memid, nameDispID, tdesc, gfuncdesc.wFuncFlags, structCache);
                                    if ((info != null) && !flag)
                                    {
                                        info.ReadOnly = 2;
                                    }
                                }
                            }
                            finally
                            {
                                typeInfo.ReleaseFuncDesc(pFuncDesc);
                            }
                        }
                    }
                    structCache.ReleaseStruct(gfuncdesc);
                    structCache.ReleaseStruct(structure);
                }
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(zero);
                structCache.ReleaseStruct(data);
            }
        }
Exemplo n.º 6
0
        private static PropInfo ProcessDataCore(UnsafeNativeMethods.ITypeInfo typeInfo, IDictionary propInfoList, int dispid, int nameDispID, System.Windows.Forms.NativeMethods.tagTYPEDESC typeDesc, int flags, StructCache structCache)
        {
            string pBstrName             = null;
            string pBstrDocString        = null;
            int    hr                    = typeInfo.GetDocumentation(dispid, ref pBstrName, ref pBstrDocString, null, null);
            ComNativeDescriptor instance = ComNativeDescriptor.Instance;

            if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
            {
                throw new COMException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetDocumentationFailed", new object[] { dispid, hr, instance.GetClassName(typeInfo) }), hr);
            }
            if (pBstrName == null)
            {
                return(null);
            }
            PropInfo info = (PropInfo)propInfoList[pBstrName];

            if (info == null)
            {
                info = new PropInfo {
                    Index = propInfoList.Count
                };
                propInfoList[pBstrName] = info;
                info.Name   = pBstrName;
                info.DispId = dispid;
                info.Attributes.Add(new DispIdAttribute(info.DispId));
            }
            if (pBstrDocString != null)
            {
                info.Attributes.Add(new DescriptionAttribute(pBstrDocString));
            }
            if (info.ValueType == null)
            {
                object[] typeData = new object[1];
                try
                {
                    info.ValueType = GetValueTypeFromTypeDesc(typeDesc, typeInfo, typeData, structCache);
                }
                catch (Exception)
                {
                }
                if (info.ValueType == null)
                {
                    info.NonBrowsable = true;
                }
                if (info.NonBrowsable)
                {
                    flags |= 0x400;
                }
                if (typeData[0] != null)
                {
                    info.TypeData = typeData[0];
                }
            }
            if ((flags & 1) != 0)
            {
                info.ReadOnly = 1;
            }
            if ((((flags & 0x40) != 0) || ((flags & 0x400) != 0)) || ((info.Name[0] == '_') || (dispid == -515)))
            {
                info.Attributes.Add(new BrowsableAttribute(false));
                info.NonBrowsable = true;
            }
            if ((flags & 0x200) != 0)
            {
                info.IsDefault = true;
            }
            if (((flags & 4) != 0) && ((flags & 0x10) != 0))
            {
                info.Attributes.Add(new BindableAttribute(true));
            }
            if (dispid == nameDispID)
            {
                info.Attributes.Add(new ParenthesizePropertyNameAttribute(true));
                info.Attributes.Add(new MergablePropertyAttribute(false));
            }
            return(info);
        }
Exemplo n.º 7
0
        private static PropertyDescriptor[] InternalGetProperties(object obj, UnsafeNativeMethods.ITypeInfo typeInfo, int dispidToGet, ref int defaultIndex)
        {
            if (typeInfo == null)
            {
                return(null);
            }
            Hashtable   propInfoList = new Hashtable();
            int         nameDispId   = GetNameDispId((UnsafeNativeMethods.IDispatch)obj);
            bool        addAboutBox  = false;
            StructCache structCache  = new StructCache();

            try
            {
                ProcessFunctions(typeInfo, propInfoList, dispidToGet, nameDispId, ref addAboutBox, structCache);
            }
            catch (ExternalException)
            {
            }
            try
            {
                ProcessVariables(typeInfo, propInfoList, dispidToGet, nameDispId, structCache);
            }
            catch (ExternalException)
            {
            }
            typeInfo = null;
            int count = propInfoList.Count;

            if (addAboutBox)
            {
                count++;
            }
            PropertyDescriptor[] descriptorArray = new PropertyDescriptor[count];
            int hr = 0;

            object[]            retval   = new object[1];
            ComNativeDescriptor instance = ComNativeDescriptor.Instance;

            foreach (PropInfo info in propInfoList.Values)
            {
                if (!info.NonBrowsable)
                {
                    try
                    {
                        hr = instance.GetPropertyValue(obj, info.DispId, retval);
                    }
                    catch (ExternalException exception)
                    {
                        hr = exception.ErrorCode;
                    }
                    if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
                    {
                        info.Attributes.Add(new BrowsableAttribute(false));
                        info.NonBrowsable = true;
                    }
                }
                else
                {
                    hr = 0;
                }
                Attribute[] array = new Attribute[info.Attributes.Count];
                info.Attributes.CopyTo(array, 0);
                descriptorArray[info.Index] = new Com2PropertyDescriptor(info.DispId, info.Name, array, info.ReadOnly != 2, info.ValueType, info.TypeData, !System.Windows.Forms.NativeMethods.Succeeded(hr));
                if (info.IsDefault)
                {
                    int index = info.Index;
                }
            }
            if (addAboutBox)
            {
                descriptorArray[descriptorArray.Length - 1] = new Com2AboutBoxPropertyDescriptor();
            }
            return(descriptorArray);
        }
Exemplo n.º 8
0
        private static System.Type GetValueTypeFromTypeDesc(System.Windows.Forms.NativeMethods.tagTYPEDESC typeDesc, UnsafeNativeMethods.ITypeInfo typeInfo, object[] typeData, StructCache structCache)
        {
            IntPtr unionMember;
            int    hr = 0;

            switch (((System.Windows.Forms.NativeMethods.tagVT)typeDesc.vt))
            {
            case System.Windows.Forms.NativeMethods.tagVT.VT_DISPATCH:
            case System.Windows.Forms.NativeMethods.tagVT.VT_UNKNOWN:
                typeData[0] = GetGuidForTypeInfo(typeInfo, structCache, null);
                return(VTToType((System.Windows.Forms.NativeMethods.tagVT)typeDesc.vt));

            case System.Windows.Forms.NativeMethods.tagVT.VT_PTR:
            {
                System.Windows.Forms.NativeMethods.tagTYPEDESC data = (System.Windows.Forms.NativeMethods.tagTYPEDESC)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEDESC));
                try
                {
                    try
                    {
                        UnsafeNativeMethods.PtrToStructure(typeDesc.unionMember, data);
                    }
                    catch
                    {
                        data = new System.Windows.Forms.NativeMethods.tagTYPEDESC {
                            unionMember = (IntPtr)Marshal.ReadInt32(typeDesc.unionMember),
                            vt          = Marshal.ReadInt16(typeDesc.unionMember, 4)
                        };
                    }
                    if (data.vt == 12)
                    {
                        return(VTToType((System.Windows.Forms.NativeMethods.tagVT)data.vt));
                    }
                    unionMember = data.unionMember;
                }
                finally
                {
                    structCache.ReleaseStruct(data);
                }
                break;
            }

            case System.Windows.Forms.NativeMethods.tagVT.VT_USERDEFINED:
                unionMember = typeDesc.unionMember;
                break;

            default:
                return(VTToType((System.Windows.Forms.NativeMethods.tagVT)typeDesc.vt));
            }
            UnsafeNativeMethods.ITypeInfo pTypeInfo = null;
            hr = typeInfo.GetRefTypeInfo(unionMember, ref pTypeInfo);
            if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
            {
                throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetRefTypeInfoFailed", new object[] { hr }), hr);
            }
            try
            {
                if (pTypeInfo != null)
                {
                    IntPtr zero = IntPtr.Zero;
                    hr = pTypeInfo.GetTypeAttr(ref zero);
                    if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
                    {
                        throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { hr }), hr);
                    }
                    System.Windows.Forms.NativeMethods.tagTYPEATTR gtypeattr = (System.Windows.Forms.NativeMethods.tagTYPEATTR)structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
                    UnsafeNativeMethods.PtrToStructure(zero, gtypeattr);
                    try
                    {
                        Guid g = gtypeattr.guid;
                        if (!Guid.Empty.Equals(g))
                        {
                            typeData[0] = g;
                        }
                        switch (gtypeattr.typekind)
                        {
                        case 0:
                            return(ProcessTypeInfoEnum(pTypeInfo, structCache));

                        case 3:
                        case 5:
                            return(VTToType(System.Windows.Forms.NativeMethods.tagVT.VT_UNKNOWN));

                        case 4:
                            return(VTToType(System.Windows.Forms.NativeMethods.tagVT.VT_DISPATCH));

                        case 6:
                            return(GetValueTypeFromTypeDesc(gtypeattr.Get_tdescAlias(), pTypeInfo, typeData, structCache));
                        }
                        return(null);
                    }
                    finally
                    {
                        pTypeInfo.ReleaseTypeAttr(zero);
                        structCache.ReleaseStruct(gtypeattr);
                    }
                }
            }
            finally
            {
                pTypeInfo = null;
            }
            return(null);
        }
 private static void ProcessVariables(UnsafeNativeMethods.ITypeInfo typeInfo, IDictionary propInfoList, int dispidToGet, int nameDispID, StructCache structCache)
 {
     IntPtr zero = IntPtr.Zero;
     int typeAttr = typeInfo.GetTypeAttr(ref zero);
     if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr) || (zero == IntPtr.Zero))
     {
         throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
     }
     System.Windows.Forms.NativeMethods.tagTYPEATTR data = (System.Windows.Forms.NativeMethods.tagTYPEATTR) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
     UnsafeNativeMethods.PtrToStructure(zero, data);
     try
     {
         if (data != null)
         {
             System.Windows.Forms.NativeMethods.tagVARDESC gvardesc = (System.Windows.Forms.NativeMethods.tagVARDESC) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagVARDESC));
             for (int i = 0; i < data.cVars; i++)
             {
                 IntPtr pVarDesc = IntPtr.Zero;
                 if (System.Windows.Forms.NativeMethods.Succeeded(typeInfo.GetVarDesc(i, ref pVarDesc)) && (pVarDesc != IntPtr.Zero))
                 {
                     UnsafeNativeMethods.PtrToStructure(pVarDesc, gvardesc);
                     try
                     {
                         if ((gvardesc.varkind != 2) && ((dispidToGet == -1) || (gvardesc.memid == dispidToGet)))
                         {
                             PropInfo info = ProcessDataCore(typeInfo, propInfoList, gvardesc.memid, nameDispID, gvardesc.elemdescVar.tdesc, gvardesc.wVarFlags, structCache);
                             if (info.ReadOnly != 1)
                             {
                                 info.ReadOnly = 2;
                             }
                         }
                     }
                     finally
                     {
                         if (pVarDesc != IntPtr.Zero)
                         {
                             typeInfo.ReleaseVarDesc(pVarDesc);
                         }
                     }
                 }
             }
             structCache.ReleaseStruct(gvardesc);
         }
     }
     finally
     {
         typeInfo.ReleaseTypeAttr(zero);
         structCache.ReleaseStruct(data);
     }
 }
 private static Guid GetGuidForTypeInfo(UnsafeNativeMethods.ITypeInfo typeInfo, StructCache structCache, int[] versions)
 {
     IntPtr zero = IntPtr.Zero;
     int typeAttr = typeInfo.GetTypeAttr(ref zero);
     if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr))
     {
         throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
     }
     Guid empty = Guid.Empty;
     System.Windows.Forms.NativeMethods.tagTYPEATTR data = null;
     try
     {
         if (structCache == null)
         {
             data = new System.Windows.Forms.NativeMethods.tagTYPEATTR();
         }
         else
         {
             data = (System.Windows.Forms.NativeMethods.tagTYPEATTR) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
         }
         UnsafeNativeMethods.PtrToStructure(zero, data);
         empty = data.guid;
         if (versions != null)
         {
             versions[0] = data.wMajorVerNum;
             versions[1] = data.wMinorVerNum;
         }
     }
     finally
     {
         typeInfo.ReleaseTypeAttr(zero);
         if ((structCache != null) && (data != null))
         {
             structCache.ReleaseStruct(data);
         }
     }
     return empty;
 }
 private static System.Type ProcessTypeInfoEnum(UnsafeNativeMethods.ITypeInfo enumTypeInfo, StructCache structCache)
 {
     if (enumTypeInfo != null)
     {
         try
         {
             IntPtr zero = IntPtr.Zero;
             int typeAttr = enumTypeInfo.GetTypeAttr(ref zero);
             if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr) || (zero == IntPtr.Zero))
             {
                 throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
             }
             System.Windows.Forms.NativeMethods.tagTYPEATTR data = (System.Windows.Forms.NativeMethods.tagTYPEATTR) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
             UnsafeNativeMethods.PtrToStructure(zero, data);
             if (zero == IntPtr.Zero)
             {
                 return null;
             }
             try
             {
                 int cVars = data.cVars;
                 ArrayList list = new ArrayList();
                 ArrayList list2 = new ArrayList();
                 System.Windows.Forms.NativeMethods.tagVARDESC gvardesc = (System.Windows.Forms.NativeMethods.tagVARDESC) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagVARDESC));
                 object objectForNativeVariant = null;
                 string pBstrName = null;
                 string pBstrDocString = null;
                 enumTypeInfo.GetDocumentation(-1, ref pBstrName, ref pBstrDocString, null, null);
                 for (int i = 0; i < cVars; i++)
                 {
                     IntPtr pVarDesc = IntPtr.Zero;
                     if (System.Windows.Forms.NativeMethods.Succeeded(enumTypeInfo.GetVarDesc(i, ref pVarDesc)) && (pVarDesc != IntPtr.Zero))
                     {
                         try
                         {
                             UnsafeNativeMethods.PtrToStructure(pVarDesc, gvardesc);
                             if (((gvardesc != null) && (gvardesc.varkind == 2)) && (gvardesc.unionMember != IntPtr.Zero))
                             {
                                 str2 = (string) (pBstrDocString = null);
                                 objectForNativeVariant = null;
                                 if (System.Windows.Forms.NativeMethods.Succeeded(enumTypeInfo.GetDocumentation(gvardesc.memid, null, ref pBstrDocString, null, null)))
                                 {
                                     string str4;
                                     try
                                     {
                                         objectForNativeVariant = Marshal.GetObjectForNativeVariant(gvardesc.unionMember);
                                     }
                                     catch (Exception)
                                     {
                                     }
                                     list2.Add(objectForNativeVariant);
                                     if (pBstrDocString != null)
                                     {
                                         str4 = pBstrDocString;
                                     }
                                     else
                                     {
                                         str4 = str2;
                                     }
                                     list.Add(str4);
                                 }
                             }
                         }
                         finally
                         {
                             if (pVarDesc != IntPtr.Zero)
                             {
                                 enumTypeInfo.ReleaseVarDesc(pVarDesc);
                             }
                         }
                     }
                 }
                 structCache.ReleaseStruct(gvardesc);
                 if (list.Count > 0)
                 {
                     IntPtr iUnknownForObject = Marshal.GetIUnknownForObject(enumTypeInfo);
                     try
                     {
                         pBstrName = iUnknownForObject.ToString() + "_" + pBstrName;
                         if (builtEnums == null)
                         {
                             builtEnums = new Hashtable();
                         }
                         else if (builtEnums.ContainsKey(pBstrName))
                         {
                             return (System.Type) builtEnums[pBstrName];
                         }
                         System.Type underlyingType = typeof(int);
                         if ((list2.Count > 0) && (list2[0] != null))
                         {
                             underlyingType = list2[0].GetType();
                         }
                         EnumBuilder builder = ModuleBuilder.DefineEnum(pBstrName, TypeAttributes.Public, underlyingType);
                         for (int j = 0; j < list.Count; j++)
                         {
                             builder.DefineLiteral((string) list[j], list2[j]);
                         }
                         System.Type type2 = builder.CreateType();
                         builtEnums[pBstrName] = type2;
                         return type2;
                     }
                     finally
                     {
                         if (iUnknownForObject != IntPtr.Zero)
                         {
                             Marshal.Release(iUnknownForObject);
                         }
                     }
                 }
             }
             finally
             {
                 enumTypeInfo.ReleaseTypeAttr(zero);
                 structCache.ReleaseStruct(data);
             }
         }
         catch
         {
         }
     }
     return null;
 }
 private static void ProcessFunctions(UnsafeNativeMethods.ITypeInfo typeInfo, IDictionary propInfoList, int dispidToGet, int nameDispID, ref bool addAboutBox, StructCache structCache)
 {
     IntPtr zero = IntPtr.Zero;
     int typeAttr = typeInfo.GetTypeAttr(ref zero);
     if (!System.Windows.Forms.NativeMethods.Succeeded(typeAttr) || (zero == IntPtr.Zero))
     {
         throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { typeAttr }), typeAttr);
     }
     System.Windows.Forms.NativeMethods.tagTYPEATTR data = (System.Windows.Forms.NativeMethods.tagTYPEATTR) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
     UnsafeNativeMethods.PtrToStructure(zero, data);
     try
     {
         if (data != null)
         {
             System.Windows.Forms.NativeMethods.tagFUNCDESC gfuncdesc = (System.Windows.Forms.NativeMethods.tagFUNCDESC) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagFUNCDESC));
             System.Windows.Forms.NativeMethods.tagELEMDESC structure = (System.Windows.Forms.NativeMethods.tagELEMDESC) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagELEMDESC));
             for (int i = 0; i < data.cFuncs; i++)
             {
                 IntPtr pFuncDesc = IntPtr.Zero;
                 if (System.Windows.Forms.NativeMethods.Succeeded(typeInfo.GetFuncDesc(i, ref pFuncDesc)) && (pFuncDesc != IntPtr.Zero))
                 {
                     UnsafeNativeMethods.PtrToStructure(pFuncDesc, gfuncdesc);
                     try
                     {
                         if ((gfuncdesc.invkind == 1) || ((dispidToGet != -1) && (gfuncdesc.memid != dispidToGet)))
                         {
                             if (gfuncdesc.memid == -552)
                             {
                                 addAboutBox = true;
                             }
                         }
                         else
                         {
                             System.Windows.Forms.NativeMethods.tagTYPEDESC tdesc;
                             bool flag = gfuncdesc.invkind == 2;
                             if (flag)
                             {
                                 if (gfuncdesc.cParams != 0)
                                 {
                                     continue;
                                 }
                                 tdesc = gfuncdesc.elemdescFunc.tdesc;
                             }
                             else
                             {
                                 if ((gfuncdesc.lprgelemdescParam == IntPtr.Zero) || (gfuncdesc.cParams != 1))
                                 {
                                     continue;
                                 }
                                 Marshal.PtrToStructure(gfuncdesc.lprgelemdescParam, structure);
                                 tdesc = structure.tdesc;
                             }
                             PropInfo info = ProcessDataCore(typeInfo, propInfoList, gfuncdesc.memid, nameDispID, tdesc, gfuncdesc.wFuncFlags, structCache);
                             if ((info != null) && !flag)
                             {
                                 info.ReadOnly = 2;
                             }
                         }
                     }
                     finally
                     {
                         typeInfo.ReleaseFuncDesc(pFuncDesc);
                     }
                 }
             }
             structCache.ReleaseStruct(gfuncdesc);
             structCache.ReleaseStruct(structure);
         }
     }
     finally
     {
         typeInfo.ReleaseTypeAttr(zero);
         structCache.ReleaseStruct(data);
     }
 }
 private static PropInfo ProcessDataCore(UnsafeNativeMethods.ITypeInfo typeInfo, IDictionary propInfoList, int dispid, int nameDispID, System.Windows.Forms.NativeMethods.tagTYPEDESC typeDesc, int flags, StructCache structCache)
 {
     string pBstrName = null;
     string pBstrDocString = null;
     int hr = typeInfo.GetDocumentation(dispid, ref pBstrName, ref pBstrDocString, null, null);
     ComNativeDescriptor instance = ComNativeDescriptor.Instance;
     if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
     {
         throw new COMException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetDocumentationFailed", new object[] { dispid, hr, instance.GetClassName(typeInfo) }), hr);
     }
     if (pBstrName == null)
     {
         return null;
     }
     PropInfo info = (PropInfo) propInfoList[pBstrName];
     if (info == null)
     {
         info = new PropInfo {
             Index = propInfoList.Count
         };
         propInfoList[pBstrName] = info;
         info.Name = pBstrName;
         info.DispId = dispid;
         info.Attributes.Add(new DispIdAttribute(info.DispId));
     }
     if (pBstrDocString != null)
     {
         info.Attributes.Add(new DescriptionAttribute(pBstrDocString));
     }
     if (info.ValueType == null)
     {
         object[] typeData = new object[1];
         try
         {
             info.ValueType = GetValueTypeFromTypeDesc(typeDesc, typeInfo, typeData, structCache);
         }
         catch (Exception)
         {
         }
         if (info.ValueType == null)
         {
             info.NonBrowsable = true;
         }
         if (info.NonBrowsable)
         {
             flags |= 0x400;
         }
         if (typeData[0] != null)
         {
             info.TypeData = typeData[0];
         }
     }
     if ((flags & 1) != 0)
     {
         info.ReadOnly = 1;
     }
     if ((((flags & 0x40) != 0) || ((flags & 0x400) != 0)) || ((info.Name[0] == '_') || (dispid == -515)))
     {
         info.Attributes.Add(new BrowsableAttribute(false));
         info.NonBrowsable = true;
     }
     if ((flags & 0x200) != 0)
     {
         info.IsDefault = true;
     }
     if (((flags & 4) != 0) && ((flags & 0x10) != 0))
     {
         info.Attributes.Add(new BindableAttribute(true));
     }
     if (dispid == nameDispID)
     {
         info.Attributes.Add(new ParenthesizePropertyNameAttribute(true));
         info.Attributes.Add(new MergablePropertyAttribute(false));
     }
     return info;
 }
 private static PropertyDescriptor[] InternalGetProperties(object obj, UnsafeNativeMethods.ITypeInfo typeInfo, int dispidToGet, ref int defaultIndex)
 {
     if (typeInfo == null)
     {
         return null;
     }
     Hashtable propInfoList = new Hashtable();
     int nameDispId = GetNameDispId((UnsafeNativeMethods.IDispatch) obj);
     bool addAboutBox = false;
     StructCache structCache = new StructCache();
     try
     {
         ProcessFunctions(typeInfo, propInfoList, dispidToGet, nameDispId, ref addAboutBox, structCache);
     }
     catch (ExternalException)
     {
     }
     try
     {
         ProcessVariables(typeInfo, propInfoList, dispidToGet, nameDispId, structCache);
     }
     catch (ExternalException)
     {
     }
     typeInfo = null;
     int count = propInfoList.Count;
     if (addAboutBox)
     {
         count++;
     }
     PropertyDescriptor[] descriptorArray = new PropertyDescriptor[count];
     int hr = 0;
     object[] retval = new object[1];
     ComNativeDescriptor instance = ComNativeDescriptor.Instance;
     foreach (PropInfo info in propInfoList.Values)
     {
         if (!info.NonBrowsable)
         {
             try
             {
                 hr = instance.GetPropertyValue(obj, info.DispId, retval);
             }
             catch (ExternalException exception)
             {
                 hr = exception.ErrorCode;
             }
             if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
             {
                 info.Attributes.Add(new BrowsableAttribute(false));
                 info.NonBrowsable = true;
             }
         }
         else
         {
             hr = 0;
         }
         Attribute[] array = new Attribute[info.Attributes.Count];
         info.Attributes.CopyTo(array, 0);
         descriptorArray[info.Index] = new Com2PropertyDescriptor(info.DispId, info.Name, array, info.ReadOnly != 2, info.ValueType, info.TypeData, !System.Windows.Forms.NativeMethods.Succeeded(hr));
         if (info.IsDefault)
         {
             int index = info.Index;
         }
     }
     if (addAboutBox)
     {
         descriptorArray[descriptorArray.Length - 1] = new Com2AboutBoxPropertyDescriptor();
     }
     return descriptorArray;
 }
        private static System.Type GetValueTypeFromTypeDesc(System.Windows.Forms.NativeMethods.tagTYPEDESC typeDesc, UnsafeNativeMethods.ITypeInfo typeInfo, object[] typeData, StructCache structCache)
        {
            IntPtr unionMember;
            int hr = 0;
            switch (((System.Windows.Forms.NativeMethods.tagVT) typeDesc.vt))
            {
                case System.Windows.Forms.NativeMethods.tagVT.VT_DISPATCH:
                case System.Windows.Forms.NativeMethods.tagVT.VT_UNKNOWN:
                    typeData[0] = GetGuidForTypeInfo(typeInfo, structCache, null);
                    return VTToType((System.Windows.Forms.NativeMethods.tagVT) typeDesc.vt);

                case System.Windows.Forms.NativeMethods.tagVT.VT_PTR:
                {
                    System.Windows.Forms.NativeMethods.tagTYPEDESC data = (System.Windows.Forms.NativeMethods.tagTYPEDESC) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEDESC));
                    try
                    {
                        try
                        {
                            UnsafeNativeMethods.PtrToStructure(typeDesc.unionMember, data);
                        }
                        catch
                        {
                            data = new System.Windows.Forms.NativeMethods.tagTYPEDESC {
                                unionMember = (IntPtr) Marshal.ReadInt32(typeDesc.unionMember),
                                vt = Marshal.ReadInt16(typeDesc.unionMember, 4)
                            };
                        }
                        if (data.vt == 12)
                        {
                            return VTToType((System.Windows.Forms.NativeMethods.tagVT) data.vt);
                        }
                        unionMember = data.unionMember;
                    }
                    finally
                    {
                        structCache.ReleaseStruct(data);
                    }
                    break;
                }
                case System.Windows.Forms.NativeMethods.tagVT.VT_USERDEFINED:
                    unionMember = typeDesc.unionMember;
                    break;

                default:
                    return VTToType((System.Windows.Forms.NativeMethods.tagVT) typeDesc.vt);
            }
            UnsafeNativeMethods.ITypeInfo pTypeInfo = null;
            hr = typeInfo.GetRefTypeInfo(unionMember, ref pTypeInfo);
            if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
            {
                throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetRefTypeInfoFailed", new object[] { hr }), hr);
            }
            try
            {
                if (pTypeInfo != null)
                {
                    IntPtr zero = IntPtr.Zero;
                    hr = pTypeInfo.GetTypeAttr(ref zero);
                    if (!System.Windows.Forms.NativeMethods.Succeeded(hr))
                    {
                        throw new ExternalException(System.Windows.Forms.SR.GetString("TYPEINFOPROCESSORGetTypeAttrFailed", new object[] { hr }), hr);
                    }
                    System.Windows.Forms.NativeMethods.tagTYPEATTR gtypeattr = (System.Windows.Forms.NativeMethods.tagTYPEATTR) structCache.GetStruct(typeof(System.Windows.Forms.NativeMethods.tagTYPEATTR));
                    UnsafeNativeMethods.PtrToStructure(zero, gtypeattr);
                    try
                    {
                        Guid g = gtypeattr.guid;
                        if (!Guid.Empty.Equals(g))
                        {
                            typeData[0] = g;
                        }
                        switch (gtypeattr.typekind)
                        {
                            case 0:
                                return ProcessTypeInfoEnum(pTypeInfo, structCache);

                            case 3:
                            case 5:
                                return VTToType(System.Windows.Forms.NativeMethods.tagVT.VT_UNKNOWN);

                            case 4:
                                return VTToType(System.Windows.Forms.NativeMethods.tagVT.VT_DISPATCH);

                            case 6:
                                return GetValueTypeFromTypeDesc(gtypeattr.Get_tdescAlias(), pTypeInfo, typeData, structCache);
                        }
                        return null;
                    }
                    finally
                    {
                        pTypeInfo.ReleaseTypeAttr(zero);
                        structCache.ReleaseStruct(gtypeattr);
                    }
                }
            }
            finally
            {
                pTypeInfo = null;
            }
            return null;
        }