コード例 #1
0
        private unsafe long GetTypeInfoVersion(UnsafeNativeMethods.ITypeInfo pTypeInfo)
        {
            IntPtr  pTypeAttr = IntPtr.Zero;
            HRESULT hr        = pTypeInfo.GetTypeAttr(ref pTypeAttr);

            if (!hr.Succeeded())
            {
                return(0);
            }

            Runtime.InteropServices.ComTypes.TYPEATTR pTAStruct;
            try
            {
                try
                {
                    // just access directly...no marshalling needed!
                    //
                    pTAStruct = *(Runtime.InteropServices.ComTypes.TYPEATTR *)pTypeAttr;
                }
                catch
                {
                    return(0);
                }

                long result = 0;

                // we pull two things out of the struct: the
                // number of functions and variables, and the version.
                // since they are next to each other, we just pull the memory directly.
                //
                // the cFuncs and cVars are both shorts, so we read them as one block of ints.
                //
                //
                int * pResult  = (int *)&result;
                byte *pbStruct = (byte *)&pTAStruct;

                // in the low byte, pull the number of props.
                //
                *pResult = *(int *)(pbStruct + CountMemberOffset);

                // move up to the high word of the long.
                //
                pResult++;

                // now pull out the version info.
                //
                *pResult = *(int *)(pbStruct + VersionOffset);

                // return that composite long.
                //
                return(result);
            }
            finally
            {
                pTypeInfo.ReleaseTypeAttr(pTypeAttr);
            }
        }
コード例 #2
0
        private static Guid GetGuidForTypeInfo(UnsafeNativeMethods.ITypeInfo typeInfo, int[] versions)
        {
            IntPtr pTypeAttr = IntPtr.Zero;
            int    hr        = typeInfo.GetTypeAttr(ref pTypeAttr);

            if (!NativeMethods.Succeeded(hr))
            {
                throw new ExternalException(string.Format(SR.TYPEINFOPROCESSORGetTypeAttrFailed, hr), hr);
            }

            try {
                ref readonly NativeMethods.tagTYPEATTR typeAttr = ref UnsafeNativeMethods.PtrToRef <NativeMethods.tagTYPEATTR>(pTypeAttr);
コード例 #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);
            }
        }
コード例 #4
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);
        }
コード例 #5
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);
 }
コード例 #6
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);
            }
        }
コード例 #7
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);
        }