Esempio n. 1
0
        /// <summary>
        /// Attempts to load the specified type lib and return it's parsed metadata.
        /// </summary>
        /// <param name="typeLibPath"></param>
        /// <returns></returns>
        bool TryLoadTypeLibFromPathInternal(string path, out TypeLibInfo info)
        {
            info = null;

            if (string.IsNullOrWhiteSpace(path))
            {
                return(false);
            }

            Log.LogMessage("TryLoadTypeLibFromPath: {0}", path);

            ITypeLib typeLib    = null;
            var      typeLibPtr = IntPtr.Zero;

            try
            {
                typeLib = LoadTypeLib(path);
                if (typeLib != null)
                {
                    typeLib.GetLibAttr(out typeLibPtr);
                    if (typeLibPtr != IntPtr.Zero)
                    {
                        // marshal pointer into struct
                        var ta = (System.Runtime.InteropServices.ComTypes.TYPELIBATTR)Marshal.PtrToStructure(typeLibPtr, typeof(System.Runtime.InteropServices.ComTypes.TYPELIBATTR));

                        // obtain information from typelib
                        typeLib.GetDocumentation(-1, out var name, out var docString, out var helpContext, out var helpFile);

                        // generate information
                        info = new TypeLibInfo()
                        {
                            Name            = name,
                            Description     = docString,
                            Guid            = ta.guid,
                            MajorVersion    = ta.wMajorVerNum,
                            MinorVersion    = ta.wMinorVerNum,
                            Lcid            = ta.lcid,
                            TypeLibPath     = NormalizePath(path),
                            TypeLibFilePath = GetTypeLibFilePath(path),
                        };

                        // success
                        return(true);
                    }
                }
            }
            catch (COMException e)
            {
                Log.LogMessage(MessageImportance.Low, "LoadTypeLib error: {0}; {1}", path, e.Message);
            }
            finally
            {
                if (typeLib != null && typeLibPtr != IntPtr.Zero)
                {
                    typeLib.ReleaseTLibAttr(typeLibPtr);
                }
            }

            return(false);
        }
Esempio n. 2
0
        public Assembly Convert(ITypeLib tl)
        {
            tl.GetLibAttr(out IntPtr ipta);
            var ta = *(_TYPELIBATTR *)ipta;

            tl.ReleaseTLibAttr(ipta);
            var hash = Fnv1((byte *)&ta, sizeof(_TYPELIBATTR) - 2).ToString("x");

            tl.GetDocumentation(-1, out var tlName, out var tlDescription, out var _, out var _);
            var fileName = $"{tlName} {ta.wMajorVerNum}.{ta.wMinorVerNum} #{hash}.dll";
            var netPath  = saveDir + fileName;

            if (!s_converted.TryGetValue(fileName, out var asm) || !File.Exists(netPath))
            {
                Print($"Converted: {tlName} ({tlDescription}) to \"{fileName}\".");

                var converter = new TypeLibConverter();
                asm = converter.ConvertTypeLibToAssembly(tl, netPath,
                                                         TypeLibImporterFlags.ReflectionOnlyLoading | TypeLibImporterFlags.TransformDispRetVals | TypeLibImporterFlags.UnsafeInterfaces,
                                                         this, null, null, tlName, null);
                asm.Save(fileName);
                s_converted[fileName] = asm;
            }
            return(asm);
        }
Esempio n. 3
0
        internal static AssemblyName GetAssemblyNameFromTypelib(object typeLib, string asmFileName, byte[] publicKey, StrongNameKeyPair keyPair, Version asmVersion, AssemblyNameFlags asmNameFlags)
        {
            string   text     = null;
            string   text2    = null;
            int      num      = 0;
            string   text3    = null;
            ITypeLib typeLib2 = (ITypeLib)typeLib;

            typeLib2.GetDocumentation(-1, out text, out text2, out num, out text3);
            if (asmFileName == null)
            {
                asmFileName = text;
            }
            else
            {
                string fileName  = Path.GetFileName(asmFileName);
                string extension = Path.GetExtension(asmFileName);
                if (!".dll".Equals(extension, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileExtension"));
                }
                asmFileName = fileName.Substring(0, fileName.Length - ".dll".Length);
            }
            if (asmVersion == null)
            {
                int major;
                int minor;
                Marshal.GetTypeLibVersion(typeLib2, out major, out minor);
                asmVersion = new Version(major, minor, 0, 0);
            }
            AssemblyName assemblyName = new AssemblyName();

            assemblyName.Init(asmFileName, publicKey, null, asmVersion, null, AssemblyHashAlgorithm.None, AssemblyVersionCompatibility.SameMachine, null, asmNameFlags, keyPair);
            return(assemblyName);
        }
Esempio n. 4
0
        public TypeLibViewModel(ITypeLib typeLib)
        {
            _typeLib = typeLib;
            TYPELIBATTR attr = COMUtil.GetTypeLibAttr(typeLib);
            CLSID = attr.guid.ToString();
            MajorVersion = attr.wMajorVerNum;
            MinorVersion = attr.wMinorVerNum;
            LCID = attr.lcid;
            //Name = Marshal.GetTypeLibName(typeLib);
            string name, docString, helpFile;
            int helpContext;
            typeLib.GetDocumentation(-1,
                                     out name,
                                     out docString,
                                     out helpContext,
                                     out helpFile);

            Path = COMUtil.GetTypeLibPath(typeLib);
            Name = name;
            Description = docString;
            // Remove the null char
            HelpFilePath = helpFile == null ? string.Empty : helpFile.Substring(0, helpFile.Length - 1);

            string asmName, asmCodeBase;
            var converter = new TypeLibConverter();
            converter.GetPrimaryInteropAssembly(
                attr.guid, MajorVersion, MinorVersion, LCID, out asmName, out asmCodeBase);

            PIAName = asmName;
            PIACodeBase = asmCodeBase;
        }
Esempio n. 5
0
 public ComDocumentation(ITypeLib typeLib, int index)
 {
     typeLib.GetDocumentation(index, out string name, out string docString, out int helpContext, out string helpFile);
     Name        = name;
     DocString   = docString;
     HelpContext = helpContext;
     HelpFile    = helpFile?.Trim('\0');
 }
Esempio n. 6
0
        public static string GetMemberName(this ITypeLib typeLib, int index)
        {
            string name;
            string docString;
            int    helpContext;
            string helpFile;

            typeLib.GetDocumentation(index, out name, out docString, out helpContext, out helpFile);
            return(name);
        }
Esempio n. 7
0
        public ComTypeLibrary(ITypeLib typeLib)
        {
            _typeLib = typeLib;
            _typeLib.GetDocumentation(-1, out _name, out _description, out _helpContext, out _helpFile);
            _typeLibAttr = _typeLib.GetLibAttr();

            _fileName = NativeMethods.QueryPathOfRegTypeLib(_typeLibAttr.guid, _typeLibAttr.wMajorVerNum, _typeLibAttr.wMinorVerNum, _typeLibAttr.lcid);
            _fileName = _fileName.Trim(new [] { '\0' });

            _fileName = Path.GetFullPath(_fileName);
        }
Esempio n. 8
0
        private static void SetVersionInformation(AssemblyBuilder asmBldr, object typeLib, AssemblyName asmName)
        {
            string   arg      = null;
            string   text     = null;
            int      num      = 0;
            string   text2    = null;
            ITypeLib typeLib2 = (ITypeLib)typeLib;

            typeLib2.GetDocumentation(-1, out arg, out text, out num, out text2);
            string product = string.Format(CultureInfo.InvariantCulture, Environment.GetResourceString("TypeLibConverter_ImportedTypeLibProductName"), arg);

            asmBldr.DefineVersionInfoResource(product, asmName.Version.ToString(), null, null, null);
            TypeLibConverter.SetTypeLibVersionAttribute(asmBldr, typeLib);
        }
Esempio n. 9
0
        internal static AssemblyName GetAssemblyNameFromTypelib(object typeLib, string asmFileName, byte[] publicKey, StrongNameKeyPair keyPair, Version asmVersion, AssemblyNameFlags asmNameFlags)
        {
            string   strName       = (string)null;
            string   strDocString  = (string)null;
            int      dwHelpContext = 0;
            string   strHelpFile   = (string)null;
            ITypeLib typeLibrary   = (ITypeLib)typeLib;

            typeLibrary.GetDocumentation(-1, out strName, out strDocString, out dwHelpContext, out strHelpFile);
            if (asmFileName == null)
            {
                asmFileName = strName;
            }
            else
            {
                string fileName = Path.GetFileName(asmFileName);
                if (!".dll".Equals(Path.GetExtension(asmFileName), StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileExtension"));
                }
                asmFileName = fileName.Substring(0, fileName.Length - ".dll".Length);
            }
            if (asmVersion == (Version)null)
            {
                int major;
                int minor;
                Marshal.GetTypeLibVersion(typeLibrary, out major, out minor);
                asmVersion = new Version(major, minor, 0, 0);
            }
            AssemblyName assemblyName = new AssemblyName();
            string       name         = asmFileName;

            byte[] publicKey1 = publicKey;
            // ISSUE: variable of the null type
            __Null  local1  = null;
            Version version = asmVersion;
            // ISSUE: variable of the null type
            __Null local2 = null;
            int    num1   = 0;
            int    num2   = 1;
            // ISSUE: variable of the null type
            __Null            local3   = null;
            int               num3     = (int)asmNameFlags;
            StrongNameKeyPair keyPair1 = keyPair;

            assemblyName.Init(name, publicKey1, (byte[])local1, version, (CultureInfo)local2, (AssemblyHashAlgorithm)num1, (AssemblyVersionCompatibility)num2, (string)local3, (AssemblyNameFlags)num3, keyPair1);
            return(assemblyName);
        }
Esempio n. 10
0
        public TypeViewModel(ITypeLib typeLib, int index)
        {
            Members = new List<MemberViewModel>();
            ITypeInfo typeInfo;
            typeLib.GetTypeInfo(index, out typeInfo);

            string strName, strDocString, strHelpFile;
            int dwHelpContext;
            typeLib.GetDocumentation(index, out strName, out strDocString, out dwHelpContext, out strHelpFile);

            Name = strName;

            // This sometimes throws a COMException
            var attr = COMUtil.GetTypeAttr(typeInfo);

            Kind = attr.typekind;

            // Get the functions
            for (int i = 0; i < attr.cFuncs; i++)
            {
                // TODO: Figure out why this throws an AccessViolationException for the groove type library
                IntPtr ppFuncDesc;
                typeInfo.GetFuncDesc(i, out ppFuncDesc);

                FUNCDESC funcdesc = (FUNCDESC)Marshal.PtrToStructure(ppFuncDesc, typeof(FUNCDESC));
                typeInfo.ReleaseFuncDesc(ppFuncDesc);

                typeInfo.GetDocumentation(funcdesc.memid, out strName, out strDocString, out dwHelpContext, out strHelpFile);
                var member = new MemberViewModel(strName);
                Members.Add(member);
            }

            // Get the vars
            for (int i = 0; i < attr.cVars; i++)
            {
                IntPtr ppVarDesc;
                typeInfo.GetVarDesc(i, out ppVarDesc);

                VARDESC vardesc = (VARDESC)Marshal.PtrToStructure(ppVarDesc, typeof(VARDESC));
                typeInfo.ReleaseVarDesc(ppVarDesc);

                typeInfo.GetDocumentation(vardesc.memid, out strName, out strDocString, out dwHelpContext, out strHelpFile);
                var member = new MemberViewModel(strName);
                Members.Add(member);
            }
        }
Esempio n. 11
0
        [System.Security.SecurityCritical]  // auto-generated
        private static void SetVersionInformation(AssemblyBuilder asmBldr, Object typeLib, AssemblyName asmName)
        {
            // Extract the name of the typelib.
            String   strTypeLibName = null;
            String   strDocString   = null;
            int      dwHelpContext  = 0;
            String   strHelpFile    = null;
            ITypeLib pTLB           = (ITypeLib)typeLib;

            pTLB.GetDocumentation(-1, out strTypeLibName, out strDocString, out dwHelpContext, out strHelpFile);

            // Generate the product name string from the named of the typelib.
            String strProductName = String.Format(CultureInfo.InvariantCulture, Environment.GetResourceString("TypeLibConverter_ImportedTypeLibProductName"), strTypeLibName);

            // Set the OS version information.
            asmBldr.DefineVersionInfoResource(strProductName, asmName.Version.ToString(), null, null, null);

            // Set the TypeLibVersion attribute
            SetTypeLibVersionAttribute(asmBldr, typeLib);
        }
Esempio n. 12
0
        private void LoadDocumentation(ITypeLib typeLib, ITypeInfo info, int index)
        {
            string name;
            string docString;
            int    helpContext;
            string helpFile;

            if (info == null)
            {
                typeLib.GetDocumentation(index, out name, out docString, out helpContext, out helpFile);
            }
            else
            {
                info.GetDocumentation(index, out name, out docString, out helpContext, out helpFile);
            }

            //See http://chat.stackexchange.com/transcript/message/30119269#30119269
            Name        = name;
            DocString   = docString;
            HelpContext = helpContext;
            HelpFile    = helpFile;
        }
Esempio n. 13
0
        private Assembly GenerateAssemblyFromNativeTypeLibInternal(Guid iid, Guid typeLibraryID, ITypeLib typeLibrary)
        {
            Assembly asm = null;

            try
            {
                lock (this)
                {
                    TypelibraryAssembly.TryGetValue(typeLibraryID, out asm);
                    if (asm == null)
                    {
                        string assemblyName = "";
                        string notused1     = "";
                        string notused2     = "";
                        int    notused3;
                        string namespaceName;
                        typeLibrary.GetDocumentation(-1, out namespaceName, out notused1, out notused3, out notused2);
                        if (String.IsNullOrEmpty(namespaceName))
                        {
                            throw Fx.AssertAndThrowFatal("Assembly cannot be null");
                        }
                        assemblyName = String.Concat(namespaceName, GetRandomName(), ".dll");
                        asm          = TypelibraryConverter.ConvertTypeLibToAssembly(typeLibrary, assemblyName, TypeLibImporterFlags.SerializableValueClasses, new ConversionEventHandler(iid, typeLibraryID), null, null, namespaceName, null);
                        TypelibraryAssembly[typeLibraryID] = asm;
                    }
                }
            }
            catch (ReflectionTypeLoadException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.FailedToConvertTypelibraryToAssembly)));
            }

            if (asm == null)
            {
                throw Fx.AssertAndThrowFatal("Assembly cannot be null");
            }
            return(asm);
        }
Esempio n. 14
0
        private Assembly GenerateAssemblyFromNativeTypeLibInternal(Guid iid, Guid typeLibraryID, ITypeLib typeLibrary)
        {
            Assembly assembly = null;

            try
            {
                lock (this)
                {
                    this.TypelibraryAssembly.TryGetValue(typeLibraryID, out assembly);
                    if (assembly == null)
                    {
                        int    num;
                        string str4;
                        string asmFileName  = "";
                        string strDocString = "";
                        string strHelpFile  = "";
                        typeLibrary.GetDocumentation(-1, out str4, out strDocString, out num, out strHelpFile);
                        if (string.IsNullOrEmpty(str4))
                        {
                            throw Fx.AssertAndThrowFatal("Assembly cannot be null");
                        }
                        asmFileName = str4 + this.GetRandomName() + ".dll";
                        assembly    = this.TypelibraryConverter.ConvertTypeLibToAssembly(typeLibrary, asmFileName, TypeLibImporterFlags.SerializableValueClasses, new ConversionEventHandler(iid, typeLibraryID), null, null, str4, null);
                        this.TypelibraryAssembly[typeLibraryID] = assembly;
                    }
                }
            }
            catch (ReflectionTypeLoadException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("FailedToConvertTypelibraryToAssembly")));
            }
            if (assembly == null)
            {
                throw Fx.AssertAndThrowFatal("Assembly cannot be null");
            }
            return(assembly);
        }
Esempio n. 15
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static AssemblyName GetAssemblyNameFromTypelib(Object typeLib, String asmFileName, byte[] publicKey, StrongNameKeyPair keyPair, Version asmVersion, AssemblyNameFlags asmNameFlags)
        {
            // Extract the name of the typelib.
            String   strTypeLibName = null;
            String   strDocString   = null;
            int      dwHelpContext  = 0;
            String   strHelpFile    = null;
            ITypeLib pTLB           = (ITypeLib)typeLib;

            pTLB.GetDocumentation(-1, out strTypeLibName, out strDocString, out dwHelpContext, out strHelpFile);

            // Retrieve the name to use for the assembly.
            if (asmFileName == null)
            {
                asmFileName = strTypeLibName;
            }
            else
            {
                Contract.Assert((asmFileName != null) && (asmFileName.Length > 0), "The assembly file name cannot be an empty string!");

                String strFileNameNoPath = Path.GetFileName(asmFileName);
                String strExtension      = Path.GetExtension(asmFileName);

                // Validate that the extension is valid.
                bool bExtensionValid = ".dll".Equals(strExtension, StringComparison.OrdinalIgnoreCase);

                // If the extension is not valid then tell the user and quit.
                if (!bExtensionValid)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileExtension"));
                }

                // The assembly cannot contain the path nor the extension.
                asmFileName = strFileNameNoPath.Substring(0, strFileNameNoPath.Length - ".dll".Length);
            }

            // If the version information was not specified, then retrieve it from the typelib.
            if (asmVersion == null)
            {
                int major;
                int minor;
                Marshal.GetTypeLibVersion(pTLB, out major, out minor);
                asmVersion = new Version(major, minor, 0, 0);
            }

            // Create the assembly name for the imported typelib's assembly.
            AssemblyName AsmName = new AssemblyName();

            AsmName.Init(
                asmFileName,
                publicKey,
                null,
                asmVersion,
                null,
                AssemblyHashAlgorithm.None,
                AssemblyVersionCompatibility.SameMachine,
                null,
                asmNameFlags,
                keyPair);

            return(AsmName);
        }
Esempio n. 16
0
 public static string GetMemberName(this ITypeLib typeLib, int index)
 {
     typeLib.GetDocumentation(index, out var name, out _, out _, out _);
     return(name);
 }
Esempio n. 17
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);
        }
Esempio n. 18
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);
            }
        }
Esempio n. 19
0
 public static string GetHelpDocumentation(this ITypeLib ti, out int context)
 {
     ti.GetDocumentation(MEMBERID_NONE, out _, out var res, out context, out _);
     return(res);
 }
Esempio n. 20
0
 public static string GetName(this ITypeLib ti)
 {
     ti.GetDocumentation(MEMBERID_NONE, out var res, out _, out var _, out _);
     return(res);
 }
        private Assembly GenerateAssemblyFromNativeTypeLibInternal(Guid iid, Guid typeLibraryID, ITypeLib typeLibrary)
        {
            Assembly asm = null;

            try
            {
                lock (this)
                {
                    TypelibraryAssembly.TryGetValue(typeLibraryID, out asm);
                    if (asm == null)
                    {
                        string assemblyName = "";
                        string notused1 = "";
                        string notused2 = "";
                        int notused3;
                        string namespaceName;
                        typeLibrary.GetDocumentation(-1, out namespaceName, out notused1, out notused3, out notused2);
                        if (String.IsNullOrEmpty(namespaceName))
                        {
                            throw Fx.AssertAndThrowFatal("Assembly cannot be null");
                        }
                        assemblyName = String.Concat(namespaceName, GetRandomName(), ".dll");
                        asm = TypelibraryConverter.ConvertTypeLibToAssembly(typeLibrary, assemblyName, TypeLibImporterFlags.SerializableValueClasses, new ConversionEventHandler(iid, typeLibraryID), null, null, namespaceName, null);
                        TypelibraryAssembly[typeLibraryID] = asm;
                    }
                }

            }
            catch (ReflectionTypeLoadException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.FailedToConvertTypelibraryToAssembly)));

            }

            if (asm == null)
            {
                throw Fx.AssertAndThrowFatal("Assembly cannot be null");
            }
            return asm;
        }
Esempio n. 22
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;
        }