Пример #1
0
        private unsafe static Guid GetGuidForTypeInfo(ITypeInfo typeInfo, uint[] versions)
        {
            TYPEATTR *pTypeAttr = null;
            HRESULT   hr        = typeInfo.GetTypeAttr(&pTypeAttr);

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

            try
            {
                if (versions is not null)
                {
                    versions[0] = pTypeAttr->wMajorVerNum;
                    versions[1] = pTypeAttr->wMinorVerNum;
                }

                return(pTypeAttr->guid);
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(pTypeAttr);
            }
        }
Пример #2
0
        public override void BuildIDLInto(IDLFormatter ih)
        {
            EnterElement();
            var lprops = _data.Attributes;

            if (lprops.Count > 0)
            {
                ih.AddString("[" + string.Join(", ", lprops.ToArray()) + "] ");
            }
            var attr = _ti.GetTypeAttr();

            if (TypeAttr.TypeFlags.TYPEFLAG_FDUAL != (attr.wTypeFlags & TypeAttr.TypeFlags.TYPEFLAG_FDUAL) &&
                TypeAttr.TypeKind.TKIND_DISPATCH == (attr.typekind & TypeAttr.TypeKind.TKIND_DISPATCH))
            {
                ih.AddString("dispinterface ");
                ih.AddLink(_data.ShortName, "di");
            }
            else
            {
                ih.AddString("interface ");
                ih.AddLink(_data.ShortName, "i");
            }
            ih.AppendLine(";");

            ExitElement();
        }
Пример #3
0
        private static IntPtr GetTypeAttr_Proxy(ITypeInfo typeInfo)
        {
            IntPtr typeAttr;

            typeInfo.GetTypeAttr(out typeAttr);
            return(typeAttr);
        }
Пример #4
0
        public IntPtr GetTypeAttr(ITypeInfo typeInfo)
        {
            IntPtr ret;

            typeInfo.GetTypeAttr(out ret);
            return(ret);
        }
Пример #5
0
        public unsafe void GetTypeAttributesForIUnknown()
        {
            TypeLibTests.LoadStdOle2(out ITypeLib typeLib);
            ITypeInfo typeInfo = typeLib.GetTypeInfoByName("IUnknown");

            HResult result = typeInfo.GetTypeAttr(out TYPEATTR * attributes);

            result.Should().Be(HResult.S_OK);
            Assert.True(attributes != null);

            attributes->cFuncs.Should().Be(3);
            attributes->cImplTypes.Should().Be(0);
            attributes->cVars.Should().Be(0);
            attributes->cbAlignment.Should().Be(8);
            attributes->cbSizeInstance.Should().Be(8);
            attributes->cbSizeVft.Should().Be(24);
            attributes->wMajorVerNum.Should().Be(0);
            attributes->wMinorVerNum.Should().Be(0);
            attributes->typekind.Should().Be(TypeKind.Interface);
            attributes->wTypeFlags.Should().Be(TypeFlags.Hidden);
            attributes->idldescType.Flags.Should().Be(IdlFlag.None);
            attributes->guid.Should().Be(new Guid("{00000000-0000-0000-c000-000000000046}"));
            attributes->lcid.Should().Be((LocaleId)0);
            attributes->memidConstructor.Should().Be(MemberId.Nil);
            attributes->memidDestructor.Should().Be(MemberId.Nil);
            attributes->tdescAlias.vt.Should().Be(VariantType.Empty);

            typeInfo.ReleaseTypeAttr(attributes);
        }
Пример #6
0
 public IntPtr GetTypeAttr(ITypeInfo typeInfo)
 {
     if (typeInfo == null) throw new ArgumentNullException(nameof(typeInfo));
     IntPtr ret;
     typeInfo.GetTypeAttr(out ret);
     return ret;
 }
Пример #7
0
        private void LoadTypes()
        {
            _typeInfos = new List <ComTypeInfo>();

            int count = _typeLib.GetTypeInfoCount();

            for (int i = 0; i < count; i++)
            {
                ITypeInfo typeInfo = null;
                _typeLib.GetTypeInfo(i, out typeInfo);

                IntPtr pTypeAttr = IntPtr.Zero;
                typeInfo.GetTypeAttr(out pTypeAttr);
                System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = pTypeAttr.ToStructure <System.Runtime.InteropServices.ComTypes.TYPEATTR>();

                switch (typeAttr.typekind)
                {
                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_ALIAS:
                    _typeInfos.Add(new ComAliasInfo(this, typeInfo, pTypeAttr));
                    break;

                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS:
                    _typeInfos.Add(new ComCoClassInfo(this, typeInfo, pTypeAttr));
                    break;

                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_DISPATCH:
                    _typeInfos.Add(new ComDispatchInfo(this, typeInfo, pTypeAttr));
                    break;

                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_ENUM:
                    _typeInfos.Add(new ComEnumInfo(this, typeInfo, pTypeAttr));
                    break;

                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_INTERFACE:
                    _typeInfos.Add(new ComInterfaceInfo(this, typeInfo, pTypeAttr));
                    break;

                //case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_MAX:
                //    _typeInfos.Add(new ComMaxInfo(this, typeInfo, pTypeAttr));
                //    break;
                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_MODULE:
                    _typeInfos.Add(new ComModuleInfo(this, typeInfo, pTypeAttr));
                    break;

                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_RECORD:
                    _typeInfos.Add(new ComRecordInfo(this, typeInfo, pTypeAttr));
                    break;

                case System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_UNION:
                    _typeInfos.Add(new ComUnionInfo(this, typeInfo, pTypeAttr));
                    break;
                }
            }

            _typeInfos.Sort(delegate(ComTypeInfo a, ComTypeInfo b)
            {
                return(a.Name.CompareTo(b.Name));
            });
        }
Пример #8
0
        internal static System.Runtime.InteropServices.ComTypes.TYPEATTR GetTypeAttr(ITypeInfo typeinfo)
        {
            IntPtr ptr;

            typeinfo.GetTypeAttr(out ptr);
            System.Runtime.InteropServices.ComTypes.TYPEATTR typeattr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(ptr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));
            typeinfo.ReleaseTypeAttr(ptr);
            return(typeattr);
        }
Пример #9
0
        public static TYPEATTR GetTypeAttr(ITypeInfo typeInfo)
        {
            IntPtr ppAttr;
            typeInfo.GetTypeAttr(out ppAttr);
            TYPEATTR typeAttr = (TYPEATTR)Marshal.PtrToStructure(ppAttr, typeof(TYPEATTR));
            typeInfo.ReleaseTypeAttr(ppAttr);

            return typeAttr;
        }
Пример #10
0
        /// <summary>
        /// Using IDispatch, determine the managed type of the specified object.
        /// </summary>
        /// <param name="comObject"></param>
        /// <returns></returns>
        public static Type GetType(object comObject)
        {
            if (Marshal.IsComObject(comObject) == false)
            {
                throw new InvalidComObjectException();
            }

            Type      type      = null;
            var       dispatch  = comObject as IDispatch;
            ITypeInfo typeInfo  = null;
            var       pTypeAttr = IntPtr.Zero;
            var       typeAttr  = default(System.Runtime.InteropServices.ComTypes.TYPEATTR);

            try
            {
                if (dispatch != null)
                {
                    typeInfo = dispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT);
                    typeInfo.GetTypeAttr(out pTypeAttr);
                    typeAttr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                    // Type can technically be defined in any loaded assembly.
                    var assemblies = AppDomain.CurrentDomain.GetAssemblies();

                    // Scan each assembly for a type with a matching GUID.
                    foreach (var assembly in assemblies)
                    {
                        type = assembly.GetTypes()
                               .Where(x => x.IsInterface)
                               .Where(x => x.GUID.Equals(typeAttr.guid))
                               .FirstOrDefault();

                        if (type != null)
                        {
                            // Found what we're looking for so break out of the loop.
                            break;
                        }
                    }
                }
            }
            finally
            {
                if (typeInfo != null)
                {
                    typeInfo.ReleaseTypeAttr(pTypeAttr);
                    Marshal.ReleaseComObject(typeInfo);
                }
            }

            return(type);
        }
Пример #11
0
        private static TYPEATTR GetTypeAttr(this ITypeInfo typeInfo)
        {
            IntPtr pTypeAttr;

            typeInfo.GetTypeAttr(out pTypeAttr);
            try
            {
                return((TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(TYPEATTR)));
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(pTypeAttr);
            }
        }
Пример #12
0
        private static TYPEFLAGS GetTypeInfoFlags(ITypeInfo typeInfo)
        {
            IntPtr pAttr;

            typeInfo.GetTypeAttr(out pAttr);
            try
            {
                var attr = (TYPEATTR)Marshal.PtrToStructure(pAttr, typeof(TYPEATTR));
                return(attr.wTypeFlags);
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(pAttr);
            }
        }
Пример #13
0
        private static Guid GetTypeInfoGuid(ITypeInfo typeInfo)
        {
            IntPtr pAttr;

            typeInfo.GetTypeAttr(out pAttr);
            try
            {
                var attr = (TYPEATTR)Marshal.PtrToStructure(pAttr, typeof(TYPEATTR));
                return(attr.guid);
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(pAttr);
            }
        }
        public static Version GetVersion(this ITypeInfo typeInfo)
        {
            System.Runtime.InteropServices.ComTypes.TYPEATTR attr;
            IntPtr p = IntPtr.Zero;

            typeInfo.GetTypeAttr(out p);

            try
            {
                attr = p.ToStructure <System.Runtime.InteropServices.ComTypes.TYPEATTR>();
                return(new Version(attr.wMajorVerNum, attr.wMinorVerNum));
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(p);
            }
        }
        public static Guid GetGuid(this ITypeInfo typeInfo)
        {
            System.Runtime.InteropServices.ComTypes.TYPEATTR attr;
            IntPtr p = IntPtr.Zero;

            typeInfo.GetTypeAttr(out p);

            try
            {
                attr = p.ToStructure <System.Runtime.InteropServices.ComTypes.TYPEATTR>();
                return(attr.guid);
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(p);
            }
        }
Пример #16
0
        internal static ObjectType GetObjectType(ITypeInfo typeInfo)
        {
            Debug.Assert(typeInfo != null, "typeInfo != null");

            IntPtr ptr = IntPtr.Zero;

            typeInfo.GetTypeAttr(out ptr);

            try
            {
                return(GetObjectType((TYPEATTR)Marshal.PtrToStructure(ptr, typeof(TYPEATTR))));
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(ptr);
            }
        }
 internal static void GetTypeAttrForTypeInfo(ITypeInfo typeInfo, out System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr)
 {
     IntPtr zero = IntPtr.Zero;
     typeInfo.GetTypeAttr(out zero);
     if (zero == IntPtr.Zero)
     {
         throw new COMException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("ResolveComReference.CannotRetrieveTypeInformation", new object[0]));
     }
     try
     {
         typeAttr = (System.Runtime.InteropServices.ComTypes.TYPEATTR) Marshal.PtrToStructure(zero, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));
     }
     finally
     {
         typeInfo.ReleaseTypeAttr(zero);
     }
 }
        internal static void GetTypeAttrForTypeInfo(ITypeInfo typeInfo, out System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr)
        {
            IntPtr zero = IntPtr.Zero;

            typeInfo.GetTypeAttr(out zero);
            if (zero == IntPtr.Zero)
            {
                throw new COMException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("ResolveComReference.CannotRetrieveTypeInformation", new object[0]));
            }
            try
            {
                typeAttr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(zero, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(zero);
            }
        }
Пример #19
0
        public static IEnumerable <DispatchMember> GetDispatchMembers(this ITypeInfo typeInfo)
        {
            var funcCount    = typeInfo.GetTypeAttr().cFuncs;
            var isEnumerable = false;

            var names = new string[1];

            for (var index = 0; index < funcCount; index++)
            {
                IntPtr pDesc;
                typeInfo.GetFuncDesc(index, out pDesc);
                try
                {
                    var desc = (FUNCDESC)Marshal.PtrToStructure(pDesc, typeof(FUNCDESC));
                    if (desc.memid == SpecialDispIDs.NewEnum)
                    {
                        isEnumerable = true;
                    }

                    if ((desc.wFuncFlags & (short)FUNCFLAGS.FUNCFLAG_FRESTRICTED) != 0)
                    {
                        continue;
                    }

                    int nameCount;
                    typeInfo.GetNames(desc.memid, names, 1, out nameCount);
                    if (nameCount > 0)
                    {
                        yield return(new DispatchMember(names[0], desc.memid, desc.invkind));
                    }
                }
                finally
                {
                    typeInfo.ReleaseFuncDesc(pDesc);
                }

                if (isEnumerable)
                {
                    yield return(new DispatchMember("GetEnumerator", SpecialDispIDs.GetEnumerator, INVOKEKIND.INVOKE_FUNC));
                }
            }
        }
Пример #20
0
        public static System.Guid GetTypeIID(ITypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                throw new Exceptions.NullParameterException(typeof(ComInterop), "GetTypeIID", "typeInfo");
            }

            System.IntPtr ptr;
            typeInfo.GetTypeAttr(out ptr);

            try
            {
                TYPEATTR typeAttr = (TYPEATTR)Marshal.PtrToStructure(ptr, typeof(TYPEATTR));
                return(typeAttr.guid);
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(ptr);
            }
        }
Пример #21
0
        public static int GetFuncCount(ITypeInfo typeInfo)
        {
            if (typeInfo == null)
            {
                throw new Exceptions.NullParameterException(typeof(ComInterop), "GetFuncCount", "typeInfo");
            }

            System.IntPtr ptr = System.IntPtr.Zero;
            typeInfo.GetTypeAttr(out ptr);

            try
            {
                TYPEATTR typeAttr = (TYPEATTR)Marshal.PtrToStructure(ptr, typeof(TYPEATTR));
                return(typeAttr.cFuncs);
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(ptr);
            }
        }
Пример #22
0
        /// <summary>
        /// Helper method for retrieving type attributes for a given type info
        /// </summary>
        /// <param name="typeInfo"></param>
        /// <param name="typeAttr"></param>
        /// <returns></returns>
        internal static void GetTypeAttrForTypeInfo(ITypeInfo typeInfo, out TYPEATTR typeAttr)
        {
            IntPtr pAttrs = IntPtr.Zero;

            typeInfo.GetTypeAttr(out pAttrs);

            // GetTypeAttr should never return null, this is just to be safe
            if (pAttrs == IntPtr.Zero)
            {
                throw new COMException(
                          ResourceUtilities.FormatResourceString("ResolveComReference.CannotRetrieveTypeInformation"));
            }

            try
            {
                typeAttr = (TYPEATTR)Marshal.PtrToStructure(pAttrs, typeof(TYPEATTR));
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(pAttrs);
            }
        }
Пример #23
0
        public void CommonBuildTlibNode(ITlibNode parent, ITypeInfo ti, bool topLevel, bool swapfordispatch, List <ITlibNode> res)
        {
            var ta = ti.GetTypeAttr();

            switch (ta.typekind)
            {
            case TypeAttr.TypeKind.TKIND_DISPATCH:
                res.Add(new OWDispInterface(this, ti, ta, topLevel));
                if (swapfordispatch && ITypeInfoXtra.SwapForInterface(ref ti, ref ta))
                {
                    res.Add(new OWInterface(this, ti, ta, topLevel));
                }
                break;

            case TypeAttr.TypeKind.TKIND_INTERFACE:
                res.Add(new OWInterface(this, ti, ta, topLevel));
                break;

            case TypeAttr.TypeKind.TKIND_ALIAS:
                res.Add(new OWTypeDef(this, ti, ta));
                break;

            case TypeAttr.TypeKind.TKIND_ENUM:
                res.Add(new OWEnum(this, ti, ta));
                break;

            case TypeAttr.TypeKind.TKIND_COCLASS:
                res.Add(new OWCoClass(this, ti, ta));
                break;

            case TypeAttr.TypeKind.TKIND_RECORD:
                res.Add(new OWRecord(this, ti, ta));
                break;

            case TypeAttr.TypeKind.TKIND_MODULE:
                res.Add(new OWModule(this, ti, ta));
                break;
            }
        }
Пример #24
0
        public static void ParseTypeLib(string filePath)
        {
            string   fileNameOnly = Path.GetFileNameWithoutExtension(filePath);
            ITypeLib typeLib      = LoadTypeLib(filePath);

            int count = typeLib.GetTypeInfoCount();

            Console.WriteLine($"typeLib count is {count}");
            IntPtr ipLibAtt = IntPtr.Zero;

            typeLib.GetLibAttr(out ipLibAtt);

            var typeLibAttr = (System.Runtime.InteropServices.ComTypes.TYPELIBATTR)
                              Marshal.PtrToStructure(ipLibAtt, typeof(System.Runtime.InteropServices.ComTypes.TYPELIBATTR));
            Guid tlbId = typeLibAttr.guid;

            for (int i = 0; i < count; i++)
            {
                ITypeInfo typeInfo = null;
                typeLib.GetTypeInfo(i, out typeInfo);

                //figure out what guids, typekind, and names of the thing we're dealing with
                IntPtr ipTypeAttr = IntPtr.Zero;
                typeInfo.GetTypeAttr(out ipTypeAttr);

                //unmarshal the pointer into a structure into something we can read
                var typeattr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)
                               Marshal.PtrToStructure(ipTypeAttr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                System.Runtime.InteropServices.ComTypes.TYPEKIND typeKind = typeattr.typekind;
                Guid typeId = typeattr.guid;

                //get the name of the type
                string strName, strDocString, strHelpFile;
                int    dwHelpContext;
                typeLib.GetDocumentation(i, out strName, out strDocString, out dwHelpContext, out strHelpFile);


                if (typeKind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS)
                {
                    string xmlComClassFormat = "<comClass clsid=\"{0}\" tlbid=\"{1}\" description=\"{2}\" progid=\"{3}.{4}\"></comClass>";
                    string comClassXml       = String.Format(xmlComClassFormat,
                                                             typeId.ToString("B").ToUpper(),
                                                             tlbId.ToString("B").ToUpper(),
                                                             strDocString,
                                                             fileNameOnly, strName
                                                             );
                    Console.WriteLine(comClassXml);
                }
                else if (typeKind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_INTERFACE)
                {
                    string xmlProxyStubFormat = "<comInterfaceExternalProxyStub name=\"{0}\" iid=\"{1}\" tlbid=\"{2}\" proxyStubClsid32=\"{3}\"></comInterfaceExternalProxyStub>";
                    string proxyStubXml       = String.Format(xmlProxyStubFormat,
                                                              strName,
                                                              typeId.ToString("B").ToUpper(),
                                                              tlbId.ToString("B").ToUpper(),
                                                              "{00020424-0000-0000-C000-000000000046}"
                                                              );
                    Console.WriteLine(proxyStubXml);
                }
            }

            return;
        }
Пример #25
0
        /// <summary>
        /// Helper method for retrieving type attributes for a given type info
        /// </summary>
        /// <param name="typeInfo"></param>
        /// <param name="typeAttr"></param>
        /// <returns></returns>
        internal static void GetTypeAttrForTypeInfo(ITypeInfo typeInfo, out TYPEATTR typeAttr)
        {
            IntPtr pAttrs = IntPtr.Zero;
            typeInfo.GetTypeAttr(out pAttrs);

            // GetTypeAttr should never return null, this is just to be safe
            if (pAttrs == IntPtr.Zero)
            {
                throw new COMException(
                    ResourceUtilities.FormatResourceString("ResolveComReference.CannotRetrieveTypeInformation"));
            }

            try
            {
                typeAttr = (TYPEATTR)Marshal.PtrToStructure(pAttrs, typeof(TYPEATTR));
            }
            finally
            {
                typeInfo.ReleaseTypeAttr(pAttrs);
            }
        }
Пример #26
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Processes one type info. We get the necessary information from the type library
		/// and also from the registry.
		/// </summary>
		/// <param name="parent">The parent element.</param>
		/// <param name="tlbGuid">The guid of the type library.</param>
		/// <param name="typeInfo">The type info.</param>
		/// ------------------------------------------------------------------------------------
		private void ProcessTypeInfo(XmlNode parent, Guid tlbGuid, ITypeInfo typeInfo)
		{
// ReSharper disable EmptyGeneralCatchClause
			try
			{
				IntPtr pTypeAttr;
				typeInfo.GetTypeAttr(out pTypeAttr);
				var typeAttr = (TYPEATTR)
					System.Runtime.InteropServices.Marshal.PtrToStructure(pTypeAttr, typeof(TYPEATTR));
				typeInfo.ReleaseTypeAttr(pTypeAttr);
				if (typeAttr.typekind == TYPEKIND.TKIND_COCLASS)
				{
					string keyString = string.Format(@"CLSID\{{{0}}}", typeAttr.guid);
					RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(keyString);
					if (typeKey == null)
						return;

					RegistryKey inprocServer = typeKey.OpenSubKey("InprocServer32");
					if (inprocServer == null)
						return;

					// Try to get the file element for the server
					var bldr = new StringBuilder(255);
					GetLongPathName((string)inprocServer.GetValue(null), bldr, 255);
					string serverFullPath = bldr.ToString();
					string server = Path.GetFileName(serverFullPath);
					if (!File.Exists(serverFullPath) &&
						!File.Exists(Path.Combine(m_BaseDirectory, server)))
					{
						if (!m_NonExistingServers.Contains(server))
						{
							Console.WriteLine("{0} is referenced in the TLB but is not in current directory", server);
							m_NonExistingServers.Add(server);
						}
						return;
					}

					XmlElement file = GetOrCreateFileNode(parent, server);
					//// Check to see that the DLL we're processing is really the DLL that can
					//// create this class. Otherwise we better not claim that we know how to do it!
					//if (keyString == null || keyString == string.Empty ||
					//    server.ToLower() != Path.GetFileName(m_FileName))
					//{
					//    return;
					//}

					var threadingModel = (string)inprocServer.GetValue("ThreadingModel");
					var progIdKey = typeKey.OpenSubKey("ProgID");
					if (progIdKey == null)
						return;
					var progId = (string)progIdKey.GetValue(null);

					XmlElement elem;
					if (!m_CoClasses.ContainsKey(progId))
					{
						// <comClass clsid="{2f0fccc2-c160-11d3-8da2-005004defec4}" threadingModel="Apartment"
						//		tlbid="{2f0fccc0-c160-11d3-8da2-005004defec4}" progid="FieldWorks.FwXmlData" />
						elem = m_Doc.CreateElement("comClass", UrnAsmv1);
						elem.SetAttribute("clsid", string.Format("{{{0}}}", typeAttr.guid));
						elem.SetAttribute("threadingModel", threadingModel);
						elem.SetAttribute("tlbid", string.Format("{{{0}}}", tlbGuid));
						elem.SetAttribute("progid", progId);
						m_CoClasses.Add(progId, elem);
						XmlNode oldChild = file.SelectSingleNode(string.Format("comClass[clsid='{{{0}}}']",
							typeAttr.guid));
						if (oldChild != null)
							file.ReplaceChild(elem, oldChild);
						else
							file.AppendChild(elem);
					}

					Debug.WriteLine(string.Format(@"Coclass: clsid=""{0}"", threadingModel=""{1}"", tlbid=""{2}"", progid=""{3}""",
						typeAttr.guid, threadingModel, tlbGuid, progId));

				}
			}
			catch
			{
				// just ignore any errors
			}
// ReSharper restore EmptyGeneralCatchClause
		}
Пример #27
0
        public static IPropertyBag GetTypeLibEnums(this ITypeInfo typeInfo)
        {
            var typeLib     = typeInfo.GetContainingTypeLib();
            var typeLibName = typeLib.GetName();

            var rootNode = new PropertyBag(true);

            var typeInfoCount = typeLib.GetTypeInfoCount();

            for (var typeInfoIndex = 0; typeInfoIndex < typeInfoCount; typeInfoIndex++)
            {
                typeLib.GetTypeInfo(typeInfoIndex, out typeInfo);
                var typeInfoName = typeInfo.GetName();

                var typeAttr = typeInfo.GetTypeAttr();
                if (typeAttr.typekind == TYPEKIND.TKIND_ALIAS)
                {
                    ITypeInfo refTypeInfo;
                    typeInfo.GetRefTypeInfo(unchecked ((int)(long)typeAttr.tdescAlias.lpValue), out refTypeInfo);

                    typeInfo = refTypeInfo;
                    typeAttr = typeInfo.GetTypeAttr();
                }

                if (typeAttr.typekind == TYPEKIND.TKIND_ENUM)
                {
                    var varCount = typeAttr.cVars;
                    for (var varIndex = 0; varIndex < varCount; varIndex++)
                    {
                        IntPtr pVarDesc;
                        typeInfo.GetVarDesc(varIndex, out pVarDesc);
                        try
                        {
                            var varDesc = (VARDESC)Marshal.PtrToStructure(pVarDesc, typeof(VARDESC));
                            if (varDesc.varkind == VARKIND.VAR_CONST)
                            {
                                var varName = typeInfo.GetMemberName(varDesc.memid);

                                object typeLibNodeObj;
                                if (!rootNode.TryGetValue(typeLibName, out typeLibNodeObj) || !(typeLibNodeObj is PropertyBag))
                                {
                                    typeLibNodeObj = new PropertyBag(true);
                                    rootNode.SetPropertyNoCheck(typeLibName, typeLibNodeObj);
                                }

                                object typeInfoNodeObj;
                                var    typeLibNode = (PropertyBag)typeLibNodeObj;
                                if (!typeLibNode.TryGetValue(typeInfoName, out typeInfoNodeObj) || !(typeInfoNodeObj is PropertyBag))
                                {
                                    typeInfoNodeObj = new PropertyBag(true);
                                    typeLibNode.SetPropertyNoCheck(typeInfoName, typeInfoNodeObj);
                                }

                                var typeInfoNode = (PropertyBag)typeInfoNodeObj;
                                typeInfoNode.SetPropertyNoCheck(varName, Marshal.GetObjectForNativeVariant(varDesc.desc.lpvarValue));
                            }
                        }
                        finally
                        {
                            typeInfo.ReleaseVarDesc(pVarDesc);
                        }
                    }
                }
            }

            return(rootNode);
        }
Пример #28
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Processes one type info. We get the necessary information from the type library
		/// and also from the registry.
		/// </summary>
		/// <param name="parent">The parent element.</param>
		/// <param name="tlbGuid">The guid of the type library.</param>
		/// <param name="typeInfo">The type info.</param>
		/// ------------------------------------------------------------------------------------
		private void ProcessTypeInfo(XmlNode parent, Guid tlbGuid, ITypeInfo typeInfo)
		{
			try
			{
				IntPtr pTypeAttr;
				typeInfo.GetTypeAttr(out pTypeAttr);
				var typeAttr = (TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(TYPEATTR));
				typeInfo.ReleaseTypeAttr(pTypeAttr);
				if (typeAttr.typekind == TYPEKIND.TKIND_COCLASS)
				{
					var clsId = typeAttr.guid.ToString("B");
					string keyString = string.Format(@"CLSID\{0}", clsId);
					RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(keyString);
					if (typeKey == null)
						return;

					RegistryKey inprocServer = typeKey.OpenSubKey("InprocServer32");
					if (inprocServer == null)
						return;

					// Try to get the file element for the server
					var bldr = new StringBuilder(255);
					RegHelper.GetLongPathName((string)inprocServer.GetValue(null), bldr, 255);
					string serverFullPath = bldr.ToString();
					string server = Path.GetFileName(serverFullPath);
					if (!File.Exists(serverFullPath) &&
						!File.Exists(Path.Combine(_baseDirectory, server)))
					{
						if (!_nonExistingServers.Contains(server))
						{
							_log.LogMessage(MessageImportance.Low, "{0} is referenced in the TLB but is not in current directory", server);
							_nonExistingServers.Add(server);
						}
						return;
					}

					XmlElement file = GetOrCreateFileNode(parent, server);
					//// Check to see that the DLL we're processing is really the DLL that can
					//// create this class. Otherwise we better not claim that we know how to do it!
					//if (keyString == null || keyString == string.Empty ||
					//    server.ToLower() != Path.GetFileName(m_FileName))
					//{
					//    return;
					//}

					if (!_coClasses.ContainsKey(clsId))
					{
						var description = (string)typeKey.GetValue(string.Empty);
						var threadingModel = (string)inprocServer.GetValue("ThreadingModel");
						var progId = GetDefaultValueForKey(typeKey, "ProgID");
						AddOrReplaceCoClass(file, clsId, threadingModel, description, tlbGuid.ToString("B"), progId);
						_log.LogMessage(MessageImportance.Low, string.Format(@"Coclass: clsid=""{0}"", threadingModel=""{1}"", tlbid=""{2}"", progid=""{3}""",
							clsId, threadingModel, tlbGuid, progId));
					}
				}
			}
			catch(Exception e)
			{
				_log.LogMessage(MessageImportance.High, "Failed to process the type info for {0}", tlbGuid);
				_log.LogMessage(MessageImportance.High, e.StackTrace);
			}
		}
Пример #29
0
        public static Dictionary <string, string> GetHelpStrings(Assembly assembly)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            var a = assembly.CustomAttributes.FirstOrDefault(x => x.AttributeType.Equals(typeof(ImportedFromTypeLibAttribute)));
            var b = assembly.CustomAttributes.FirstOrDefault(x => x.AttributeType.Equals(typeof(GuidAttribute)));
            var c = assembly.CustomAttributes.FirstOrDefault(x => x.AttributeType.Equals(typeof(TypeLibVersionAttribute)));

            if (a != null)
            {
                Guid guid      = Guid.Parse(String.Format("{0}", b.ConstructorArguments[0].Value));
                int  wVerMajor = (int)c.ConstructorArguments[0].Value;
                int  wVerMinor = (int)c.ConstructorArguments[1].Value;

                ITypeLib typeLib = null;
                typeLib = LoadRegTypeLib(ref guid, wVerMajor, wVerMinor, 0);

                string strLibName       = null;
                string strLibDocString  = null;
                int    dwLibHelpContext = 0;
                string strLibHelpFile   = null;

                typeLib.GetDocumentation(-1, out strLibName, out strLibDocString, out dwLibHelpContext, out strLibHelpFile);

                int count = typeLib.GetTypeInfoCount();

                // Loop through types.
                for (int i = 0; i < count; i++)
                {
                    ITypeInfo typeInfo = null;
                    typeLib.GetTypeInfo(i, out typeInfo);

                    IntPtr pTypeAttr = IntPtr.Zero;

                    typeInfo.GetTypeAttr(out pTypeAttr);
                    System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                    // Skip type if it is hidden.
                    if (typeAttr.wTypeFlags.HasFlag(System.Runtime.InteropServices.ComTypes.TYPEFLAGS.TYPEFLAG_FHIDDEN) == true)
                    {
                        continue;
                    }

                    string strTypeName       = null;
                    string strTypeDocString  = null;
                    int    dwTypeHelpContext = 0;
                    string strTypeHelpFile   = null;

                    typeInfo.GetDocumentation(-1, out strTypeName, out strTypeDocString, out dwTypeHelpContext, out strTypeHelpFile);

                    string typeKey = String.Format("{0}.{1}", strLibName, strTypeName);
                    dictionary.Add(typeKey, strTypeDocString);

                    for (int j = 0; j < typeAttr.cFuncs; j++)
                    {
                        IntPtr pFuncDesc = IntPtr.Zero;
                        typeInfo.GetFuncDesc(j, out pFuncDesc);

                        System.Runtime.InteropServices.ComTypes.FUNCDESC funcDesc = (System.Runtime.InteropServices.ComTypes.FUNCDESC)Marshal.PtrToStructure(pFuncDesc, typeof(System.Runtime.InteropServices.ComTypes.FUNCDESC));

                        string strMemberName       = null;
                        string strMemberDocString  = null;
                        int    dwMemberHelpContext = 0;
                        string strMemberHelpFile   = null;

                        typeInfo.GetDocumentation(funcDesc.memid, out strMemberName, out strMemberDocString, out dwMemberHelpContext, out strMemberHelpFile);

                        string memberKey = String.Format("{0}.{1}", typeKey, strMemberName);

                        if (!dictionary.ContainsKey(memberKey))
                        {
                            dictionary.Add(memberKey, strMemberDocString);
                        }

                        typeInfo.ReleaseFuncDesc(pFuncDesc);
                    }

                    typeInfo.ReleaseTypeAttr(pTypeAttr);
                }
            }

            return(dictionary);
        }
Пример #30
0
 public static TYPEFLAGS GetFlags(this ITypeInfo typeInfo)
 {
     return(typeInfo.GetTypeAttr().wTypeFlags);
 }
Пример #31
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Processes one type info. We get the necessary information from the type library
        /// and also from the registry.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="tlbGuid">The guid of the type library.</param>
        /// <param name="typeInfo">The type info.</param>
        /// ------------------------------------------------------------------------------------
        private void ProcessTypeInfo(XmlNode parent, Guid tlbGuid, ITypeInfo typeInfo)
        {
// ReSharper disable EmptyGeneralCatchClause
            try
            {
                IntPtr pTypeAttr;
                typeInfo.GetTypeAttr(out pTypeAttr);
                var typeAttr = (TYPEATTR)
                               System.Runtime.InteropServices.Marshal.PtrToStructure(pTypeAttr, typeof(TYPEATTR));
                typeInfo.ReleaseTypeAttr(pTypeAttr);
                if (typeAttr.typekind == TYPEKIND.TKIND_COCLASS)
                {
                    string      keyString = string.Format(@"CLSID\{{{0}}}", typeAttr.guid);
                    RegistryKey typeKey   = Registry.ClassesRoot.OpenSubKey(keyString);
                    if (typeKey == null)
                    {
                        return;
                    }

                    RegistryKey inprocServer = typeKey.OpenSubKey("InprocServer32");
                    if (inprocServer == null)
                    {
                        return;
                    }

                    // Try to get the file element for the server
                    var bldr = new StringBuilder(255);
                    GetLongPathName((string)inprocServer.GetValue(null), bldr, 255);
                    string serverFullPath = bldr.ToString();
                    string server         = Path.GetFileName(serverFullPath);
                    if (!File.Exists(serverFullPath) &&
                        !File.Exists(Path.Combine(m_BaseDirectory, server)))
                    {
                        if (!m_NonExistingServers.Contains(server))
                        {
                            Console.WriteLine("{0} is referenced in the TLB but is not in current directory", server);
                            m_NonExistingServers.Add(server);
                        }
                        return;
                    }

                    XmlElement file = GetOrCreateFileNode(parent, server);
                    //// Check to see that the DLL we're processing is really the DLL that can
                    //// create this class. Otherwise we better not claim that we know how to do it!
                    //if (keyString == null || keyString == string.Empty ||
                    //    server.ToLower() != Path.GetFileName(m_FileName))
                    //{
                    //    return;
                    //}

                    var threadingModel = (string)inprocServer.GetValue("ThreadingModel");
                    var progIdKey      = typeKey.OpenSubKey("ProgID");
                    if (progIdKey == null)
                    {
                        return;
                    }
                    var progId = (string)progIdKey.GetValue(null);

                    XmlElement elem;
                    if (!m_CoClasses.ContainsKey(progId))
                    {
                        // <comClass clsid="{2f0fccc2-c160-11d3-8da2-005004defec4}" threadingModel="Apartment"
                        //		tlbid="{2f0fccc0-c160-11d3-8da2-005004defec4}" progid="FieldWorks.FwXmlData" />
                        elem = m_Doc.CreateElement("comClass", UrnAsmv1);
                        elem.SetAttribute("clsid", string.Format("{{{0}}}", typeAttr.guid));
                        elem.SetAttribute("threadingModel", threadingModel);
                        elem.SetAttribute("tlbid", string.Format("{{{0}}}", tlbGuid));
                        elem.SetAttribute("progid", progId);
                        m_CoClasses.Add(progId, elem);
                        XmlNode oldChild = file.SelectSingleNode(string.Format("comClass[clsid='{{{0}}}']",
                                                                               typeAttr.guid));
                        if (oldChild != null)
                        {
                            file.ReplaceChild(elem, oldChild);
                        }
                        else
                        {
                            file.AppendChild(elem);
                        }
                    }

                    Debug.WriteLine(string.Format(@"Coclass: clsid=""{0}"", threadingModel=""{1}"", tlbid=""{2}"", progid=""{3}""",
                                                  typeAttr.guid, threadingModel, tlbGuid, progId));
                }
            }
            catch
            {
                // just ignore any errors
            }
// ReSharper restore EmptyGeneralCatchClause
        }
Пример #32
0
 public static TYPEKIND GetKind(this ITypeInfo typeInfo)
 {
     return(typeInfo.GetTypeAttr().typekind);
 }
Пример #33
0
 private static IntPtr GetTypeAttr_Proxy(ITypeInfo typeInfo)
 {
     IntPtr typeAttr;
     typeInfo.GetTypeAttr(out typeAttr);
     return typeAttr;
 }
Пример #34
0
        public override void asCommandLine(Dictionary <string, string> args)
        {
            _typeLibrary = (string)args["TypeLibrary"];
            //_typeLibrary = @"C:\Peach3\ComTest\Release\ComTest.dll";

            if (!File.Exists(_typeLibrary))
            {
                throw new PeachException("Error, the TypeLibrary was not found.");
            }

            ITypeLib typeLib = null;
            int      ret     = LoadTypeLib(_typeLibrary, out typeLib);

            if (ret != 0)
            {
                throw new PeachException("Error loading TypeLibrary.  LoadTypeLib returned " + ret);
            }

            if (typeLib == null)
            {
                throw new PeachException("Error, LoadTypeLib returned a null ITypeLib interface.");
            }

            string name;
            string doc;
            int    helpid;
            string helpfile;

            string [] arrClassification = new string [] { "Enum", "Struct", "Module", "Interface",
                                                          "Dispinterface", "Coclass", "Typedef", "Union" };


            typeLib.GetDocumentation(-1, out name, out doc, out helpid, out helpfile);
            Console.WriteLine(name);

            ITypeInfo typeInfo = null;

            for (int cnt = 0; cnt < typeLib.GetTypeInfoCount(); cnt++)
            {
                // http://www.codeguru.com/cpp/com-tech/activex/misc/article.php/c2569

                Console.WriteLine(" ------------- ");

                typeInfo = null;
                typeLib.GetTypeInfo(cnt, out typeInfo);
                if (typeInfo == null)
                {
                    Console.WriteLine("typeInfo was null, continue!");
                    continue;
                }

                typeLib.GetDocumentation(cnt, out name, out doc, out helpid, out helpfile);
                Console.WriteLine("  " + name);

                System.Runtime.InteropServices.ComTypes.TYPEKIND typeKind;
                typeLib.GetTypeInfoType(cnt, out typeKind);

                Console.WriteLine("  " + arrClassification[(int)typeKind]);

                IntPtr ppTypeAttributes;
                typeInfo.GetTypeAttr(out ppTypeAttributes);
                var typeAttributes = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(ppTypeAttributes, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                for (int cntFuncs = 0; cntFuncs < typeAttributes.cFuncs; cntFuncs++)
                {
                    IntPtr ppFuncDesc;
                    typeInfo.GetFuncDesc(cntFuncs, out ppFuncDesc);
                    var funcDesc = (System.Runtime.InteropServices.ComTypes.FUNCDESC)Marshal.PtrToStructure(ppFuncDesc, typeof(System.Runtime.InteropServices.ComTypes.FUNCDESC));

                    int memberID = funcDesc.memid;
                    //var elemDesc = funcDesc.elemdescFunc;

                    typeInfo.GetDocumentation(memberID, out name, out doc, out helpid, out helpfile);
                    Console.WriteLine("    " + name);

                    //funcDesc.

                    typeInfo.ReleaseFuncDesc(ppFuncDesc);
                }

                for (int cntVars = 0; cntVars < typeAttributes.cVars; cntVars++)
                {
                    IntPtr ppVarDesc;
                    typeInfo.GetVarDesc(cntVars, out ppVarDesc);
                    var varDesc = (System.Runtime.InteropServices.ComTypes.VARDESC)Marshal.PtrToStructure(ppVarDesc, typeof(System.Runtime.InteropServices.ComTypes.VARDESC));

                    int memberID = varDesc.memid;

                    typeInfo.GetDocumentation(memberID, out name, out doc, out helpid, out helpfile);
                    Console.WriteLine("    " + name);

                    typeInfo.ReleaseVarDesc(ppVarDesc);
                }

                typeInfo.ReleaseTypeAttr(ppTypeAttributes);
            }
        }
Пример #35
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Processes one type info. We get the necessary information from the type library
        /// and also from the registry.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="tlbGuid">The guid of the type library.</param>
        /// <param name="typeInfo">The type info.</param>
        /// ------------------------------------------------------------------------------------
        private void ProcessTypeInfo(XmlNode parent, Guid tlbGuid, ITypeInfo typeInfo)
        {
            try
            {
                IntPtr pTypeAttr;
                typeInfo.GetTypeAttr(out pTypeAttr);
                var typeAttr = (TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(TYPEATTR));
                typeInfo.ReleaseTypeAttr(pTypeAttr);
                if (typeAttr.typekind == TYPEKIND.TKIND_COCLASS)
                {
                    var         clsId     = typeAttr.guid.ToString("B");
                    string      keyString = string.Format(@"CLSID\{0}", clsId);
                    RegistryKey typeKey   = Registry.ClassesRoot.OpenSubKey(keyString);
                    if (typeKey == null)
                    {
                        return;
                    }

                    RegistryKey inprocServer = typeKey.OpenSubKey("InprocServer32");
                    if (inprocServer == null)
                    {
                        return;
                    }

                    // Try to get the file element for the server
                    var bldr = new StringBuilder(255);
                    RegHelper.GetLongPathName((string)inprocServer.GetValue(null), bldr, 255);
                    string serverFullPath = bldr.ToString();
                    string server         = Path.GetFileName(serverFullPath);
                    if (!File.Exists(serverFullPath) &&
                        !File.Exists(Path.Combine(_baseDirectory, server)))
                    {
                        if (!_nonExistingServers.Contains(server))
                        {
                            _log.LogMessage(MessageImportance.Low, "{0} is referenced in the TLB but is not in current directory", server);
                            _nonExistingServers.Add(server);
                        }
                        return;
                    }

                    XmlElement file = GetOrCreateFileNode(parent, server);
                    //// Check to see that the DLL we're processing is really the DLL that can
                    //// create this class. Otherwise we better not claim that we know how to do it!
                    //if (keyString == null || keyString == string.Empty ||
                    //    server.ToLower() != Path.GetFileName(m_FileName))
                    //{
                    //    return;
                    //}

                    if (!_coClasses.ContainsKey(clsId))
                    {
                        var description    = (string)typeKey.GetValue(string.Empty);
                        var threadingModel = (string)inprocServer.GetValue("ThreadingModel");
                        var progId         = GetDefaultValueForKey(typeKey, "ProgID");
                        AddOrReplaceCoClass(file, clsId, threadingModel, description, tlbGuid.ToString("B"), progId);
                        _log.LogMessage(MessageImportance.Low, string.Format(@"Coclass: clsid=""{0}"", threadingModel=""{1}"", tlbid=""{2}"", progid=""{3}""",
                                                                             clsId, threadingModel, tlbGuid, progId));
                    }
                }
            }
            catch (Exception e)
            {
                _log.LogMessage(MessageImportance.High, "Failed to process the type info for {0}", tlbGuid);
                _log.LogMessage(MessageImportance.High, e.StackTrace);
            }
        }
Пример #36
0
 internal static System.Runtime.InteropServices.ComTypes.TYPEATTR GetTypeAttr(ITypeInfo typeinfo)
 {
     IntPtr ptr;
     typeinfo.GetTypeAttr(out ptr);
     System.Runtime.InteropServices.ComTypes.TYPEATTR typeattr = (System.Runtime.InteropServices.ComTypes.TYPEATTR) Marshal.PtrToStructure(ptr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));
     typeinfo.ReleaseTypeAttr(ptr);
     return typeattr;
 }
Пример #37
0
 public TypeAttr(ITypeInfo typeinfo)
 {
     m_typeinfo = typeinfo;
     typeinfo.GetTypeAttr(out m_ipTypeAttr);
     m_typeattr = (TYPEATTR)Marshal.PtrToStructure(m_ipTypeAttr, typeof(TYPEATTR));
 }
Пример #38
0
 public static Guid GetGuid(this ITypeInfo typeInfo)
 {
     return(typeInfo.GetTypeAttr().guid);
 }