Beispiel #1
0
        internal bool ImportComComponent(string path, OutputMessageCollection outputMessages, string outputDisplayName)
        {
            var importer = new ComImporter(path, outputMessages, outputDisplayName);

            if (importer.Success)
            {
                var typeLibs = new List <TypeLib>();

                // Add TypeLib objects from importer...
                if (_typeLibs != null)
                {
                    typeLibs.AddRange(_typeLibs);
                }

                if (importer.TypeLib != null)
                {
                    typeLibs.Add(importer.TypeLib);
                }
                _typeLibs = typeLibs.ToArray();

                // Add ComClass objects from importer...
                var comClasses = new List <ComClass>();
                if (_comClasses != null)
                {
                    comClasses.AddRange(_comClasses);
                }

                if (importer.ComClasses != null)
                {
                    comClasses.AddRange(importer.ComClasses);
                }
                _comClasses = comClasses.ToArray();
            }
            return(importer.Success);
        }
Beispiel #2
0
        public ComImporter(string path, OutputMessageCollection outputMessages, string outputDisplayName)
        {
            _outputMessages    = outputMessages;
            _outputDisplayName = outputDisplayName;

            if (NativeMethods.SfcIsFileProtected(IntPtr.Zero, path) != 0)
            {
                outputMessages.AddWarningMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.ProtectedFile"));
            }

            object obj = null;

            try { NativeMethods.LoadTypeLibEx(path, NativeMethods.RegKind.RegKind_None, out obj); }
            catch (COMException) { }

#pragma warning disable 618
            UCOMITypeLib tlib = (UCOMITypeLib)obj;
            if (tlib != null)
            {
                IntPtr typeLibAttrPtr = IntPtr.Zero;
                tlib.GetLibAttr(out typeLibAttrPtr);
                var typeLibAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttrPtr, typeof(TYPELIBATTR));
                tlib.ReleaseTLibAttr(typeLibAttrPtr);
                Guid tlbid = typeLibAttr.guid;

                tlib.GetDocumentation(-1, out _, out string docString, out _, out string helpFile);
                string helpdir = Util.FilterNonprintableChars(helpFile); //Path.GetDirectoryName(helpFile);

                TypeLib = new TypeLib(tlbid, new Version(typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum), helpdir, typeLibAttr.lcid, Convert.ToInt32(typeLibAttr.wLibFlags, CultureInfo.InvariantCulture));

                var comClassList = new List <ComClass>();
                int count        = tlib.GetTypeInfoCount();
                for (int i = 0; i < count; ++i)
                {
                    tlib.GetTypeInfoType(i, out TYPEKIND tkind);
                    if (tkind == TYPEKIND.TKIND_COCLASS)
                    {
                        tlib.GetTypeInfo(i, out UCOMITypeInfo tinfo);

                        IntPtr tinfoAttrPtr = IntPtr.Zero;
                        tinfo.GetTypeAttr(out tinfoAttrPtr);
                        TYPEATTR tinfoAttr = (TYPEATTR)Marshal.PtrToStructure(tinfoAttrPtr, typeof(TYPEATTR));
                        tinfo.ReleaseTypeAttr(tinfoAttrPtr);
                        Guid clsid = tinfoAttr.guid;

                        tlib.GetDocumentation(i, out _, out docString, out _, out helpFile);
                        string description = Util.FilterNonprintableChars(docString);

                        ClassInfo info = GetRegisteredClassInfo(clsid);
                        if (info == null)
                        {
                            continue;
                        }

                        comClassList.Add(new ComClass(tlbid, clsid, info.Progid, info.ThreadingModel, description));
                    }
                }
                if (comClassList.Count > 0)
                {
                    ComClasses = comClassList.ToArray();
                    Success    = true;
                }
                else
                {
                    outputMessages.AddErrorMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.NoRegisteredClasses"));
                    Success = false;
                }
            }
            else
            {
                outputMessages.AddErrorMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.TypeLibraryLoadFailure"));
                Success = false;
            }
#pragma warning restore 618
        }