示例#1
0
        /// <summary>
        /// Gets an ITypeLib object from OLE Automation compatible RCW ,
        /// reads definitions of CoClass'es and Enum's from this library
        /// and creates a IDynamicObject that allows to instantiate coclasses
        /// and get actual values for the enums.
        /// </summary>
        /// <param name="rcw">OLE automation compatible RCW</param>
        /// <returns>ComTypeLibDesc object</returns>
        public static ComTypeLibInfo CreateFromObject(object rcw)
        {
            if (Marshal.IsComObject(rcw) == false)
            {
                throw new ArgumentException("COM object is expected.");
            }

            ComTypes.ITypeInfo typeInfo = ComRuntimeHelpers.GetITypeInfoFromIDispatch(rcw as IDispatch, true);

            ComTypes.ITypeLib typeLib;
            int typeInfoIndex;

            typeInfo.GetContainingTypeLib(out typeLib, out typeInfoIndex);

            return(new ComTypeLibInfo(GetFromTypeLib(typeLib)));
        }
示例#2
0
        private static ComTypes.ITypeInfo GetCoClassTypeInfo(object rcw, ComTypes.ITypeInfo typeInfo)
        {
            Debug.Assert(typeInfo != null);

            IProvideClassInfo provideClassInfo = rcw as IProvideClassInfo;

            if (provideClassInfo != null)
            {
                IntPtr typeInfoPtr = IntPtr.Zero;
                try {
                    provideClassInfo.GetClassInfo(out typeInfoPtr);
                    if (typeInfoPtr != IntPtr.Zero)
                    {
                        return(Marshal.GetObjectForIUnknown(typeInfoPtr) as ComTypes.ITypeInfo);
                    }
                } finally {
                    if (typeInfoPtr != IntPtr.Zero)
                    {
                        Marshal.Release(typeInfoPtr);
                    }
                }
            }

            // retrieving class information through IPCI has failed -
            // we can try scanning the typelib to find the coclass

            ComTypes.ITypeLib typeLib;
            int typeInfoIndex;

            typeInfo.GetContainingTypeLib(out typeLib, out typeInfoIndex);
            string typeName = ComRuntimeHelpers.GetNameOfType(typeInfo);

            ComTypeLibDesc   typeLibDesc = ComTypeLibDesc.GetFromTypeLib(typeLib);
            ComTypeClassDesc coclassDesc = typeLibDesc.GetCoClassForInterface(typeName);

            if (coclassDesc == null)
            {
                return(null);
            }

            ComTypes.ITypeInfo typeInfoCoClass;
            Guid coclassGuid = coclassDesc.Guid;

            typeLib.GetTypeInfoOfGuid(ref coclassGuid, out typeInfoCoClass);
            return(typeInfoCoClass);
        }
        /// <summary>
        /// Returns parent library id
        /// </summary>
        /// <param name="value">core to use</param>
        /// <param name="comProxy">new created proxy</param>
        /// <returns>parent library/component id</returns>
        internal static Guid GetParentLibraryGuid(this Core value, object comProxy)
        {
            if (null == comProxy)
            {
                throw new ArgumentNullException();
            }

            IDispatch dispatcher = comProxy as IDispatch;

            if (null == dispatcher)
            {
                return(Guid.Empty);
            }

            COMTypes.ITypeInfo typeInfo      = dispatcher.GetTypeInfo(0, 0);
            COMTypes.ITypeLib  parentTypeLib = null;

            Guid typeGuid   = typeInfo.GetTypeGuid();
            Guid parentGuid = Guid.Empty;

            if (!value.HostCache.TryGetValue(typeGuid, out parentGuid))
            {
                int i = 0;
                typeInfo.GetContainingTypeLib(out parentTypeLib, out i);

                IntPtr attributesPointer = IntPtr.Zero;
                parentTypeLib.GetLibAttr(out attributesPointer);

                COMTypes.TYPELIBATTR attributes =
                    (COMTypes.TYPELIBATTR)Marshal.PtrToStructure(attributesPointer,
                                                                 typeof(COMTypes.TYPELIBATTR));
                parentGuid = attributes.guid;
                parentTypeLib.ReleaseTLibAttr(attributesPointer);
                Marshal.ReleaseComObject(parentTypeLib);

                value.HostCache.Add(typeGuid, parentGuid);
            }

            Marshal.ReleaseComObject(typeInfo);

            return(parentGuid);
        }