GetPrimaryInteropAssembly() private method

private GetPrimaryInteropAssembly ( System.Guid g, int major, int minor, int lcid, string &asmName, string &asmCodeBase ) : bool
g System.Guid
major int
minor int
lcid int
asmName string
asmCodeBase string
return bool
コード例 #1
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;
        }
コード例 #2
0
        private ProjectElement GetProjectElementBasedOnInputFromComponentSelectorData()
        {
            ProjectElement element = new ProjectElement(this.ProjectMgr, this.typeName, ProjectFileConstants.COMReference);

            // Set the basic information regarding this COM component
            element.SetMetadata(ProjectFileConstants.Guid, this.typeGuid.ToString("B"));
            element.SetMetadata(ProjectFileConstants.VersionMajor, this.majorVersionNumber);
            element.SetMetadata(ProjectFileConstants.VersionMinor, this.minorVersionNumber);
            element.SetMetadata(ProjectFileConstants.Lcid, this.lcid);
            element.SetMetadata(ProjectFileConstants.Isolated, false.ToString());

            // See if a PIA exist for this component
            TypeLibConverter typelib = new TypeLibConverter();
            string assemblyName;
            string assemblyCodeBase;
            if (typelib.GetPrimaryInteropAssembly(this.typeGuid, Int32.Parse(this.majorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.minorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.lcid, CultureInfo.InvariantCulture), out assemblyName, out assemblyCodeBase))
            {
                element.SetMetadata(ProjectFileConstants.WrapperTool, WrapperToolAttributeValue.Primary.ToString().ToLowerInvariant());
            }
            else
            {
                // MSBuild will have to generate an interop assembly
                element.SetMetadata(ProjectFileConstants.WrapperTool, WrapperToolAttributeValue.TlbImp.ToString().ToLowerInvariant());
                element.SetMetadata(ProjectFileConstants.Private, true.ToString());
            }
            return element;
        }
コード例 #3
0
ファイル: PiaReference.cs プロジェクト: cameron314/msbuild
        /*
         * Method:  Resolve
         * 
         * Gets the resolved assembly path for the typelib wrapper.
         */
        internal override bool FindExistingWrapper(out ComReferenceWrapperInfo wrapperInfo, DateTime componentTimestamp)
        {
            wrapperInfo = null;

            // Let NDP do the dirty work...
            TypeLibConverter converter = new TypeLibConverter();
            string asmName, asmCodeBase;

            if (!converter.GetPrimaryInteropAssembly(ReferenceInfo.attr.guid, ReferenceInfo.attr.wMajorVerNum, ReferenceInfo.attr.wMinorVerNum, ReferenceInfo.attr.lcid,
                out asmName, out asmCodeBase))
            {
                return false;
            }

            // let's try to load the assembly to determine its path and if it's there
            try
            {
                if (asmCodeBase != null && asmCodeBase.Length > 0)
                {
                    Uri uri = new Uri(asmCodeBase);

                    // make sure the PIA can be loaded
                    Assembly assembly = Assembly.UnsafeLoadFrom(uri.LocalPath);

                    // got here? then assembly must have been loaded successfully.
                    wrapperInfo = new ComReferenceWrapperInfo();
                    wrapperInfo.path = uri.LocalPath;
                    wrapperInfo.assembly = assembly;

                    // We need to remember the original assembly name of this PIA in case it gets redirected to a newer 
                    // version and other COM components use that name to reference the PIA. assembly.FullName wouldn't
                    // work here since we'd get the redirected assembly name.
                    wrapperInfo.originalPiaName = new AssemblyNameExtension(AssemblyName.GetAssemblyName(uri.LocalPath));
                }
                else
                {
                    Assembly assembly = Assembly.Load(asmName);

                    // got here? then assembly must have been loaded successfully.
                    wrapperInfo = new ComReferenceWrapperInfo();
                    wrapperInfo.path = assembly.Location;
                    wrapperInfo.assembly = assembly;

                    // We need to remember the original assembly name of this PIA in case it gets redirected to a newer 
                    // version and other COM components use that name to reference the PIA. 
                    wrapperInfo.originalPiaName = new AssemblyNameExtension(asmName, true);
                }
            }
            catch (FileNotFoundException)
            {
                // This means that assembly file cannot be found.
                // We don't need to do anything here; wrapperInfo is not set 
                // and we'll assume that the assembly doesn't exist.
            }
            catch (BadImageFormatException)
            {
                // Similar case as above, except we should additionally warn the user that the assembly file 
                // is not really a valid assembly file.
                if (!Silent)
                {
                    Log.LogWarningWithCodeFromResources("ResolveComReference.BadAssemblyImage", asmName);
                }
            }

            // have we found the wrapper?
            if (wrapperInfo != null)
            {
                return true;
            }

            return false;
        }
コード例 #4
0
        private static Type GetInterfaceForTypeInfo(ICallerContext context, IntPtr typeInfoPtr)
        {
            Debug.Assert(typeInfoPtr != IntPtr.Zero);

            ComTypes.TYPEATTR typeInfoAttr = GetITypeInfoAttr(typeInfoPtr);
            Guid typeInfoGuid = typeInfoAttr.guid;

            // Have we seen the GUID before in a previously-loaded assembly?

            Type interfaceType = null;
            if (ComTypeCache.TryGetValue(typeInfoGuid, out interfaceType))
                return interfaceType;

            // Try to find a registered Primary Interop Assembly (PIA)

            TypeLibConverter tlc = new TypeLibConverter();
            string asmName = null, asmCodeBase = null;
            if (tlc.GetPrimaryInteropAssembly(
                    typeInfoGuid,
                    typeInfoAttr.wMajorVerNum,
                    typeInfoAttr.wMinorVerNum,
                    0,
                    out asmName,
                    out asmCodeBase)) {
                try {
                    Assembly interopAssembly = Assembly.Load(asmName);
                    context.SystemState.TopPackage.LoadAssembly(context.SystemState, interopAssembly, true);
                    Debug.Assert(ComTypeCache.ContainsKey(typeInfoGuid));
                    if (!ComTypeCache.ContainsKey(typeInfoGuid))
                        throw new COMException("TypeLib " + asmName + " does not contain COM interface + " + typeInfoGuid);
                    return ComTypeCache[typeInfoGuid];
                } catch (FileNotFoundException) { }
            }

            #if COM_GAC_CRAWLER
            Assembly interopAssembly = SearchForInteropAssemblyInGAC(typeInfoGuid, typeInfoAttr.wMajorVerNum, typeInfoAttr.wMinorVerNum);
            if (interopAssembly != null) {
                context.SystemState.TopPackage.LoadAssembly(context.SystemState, interopAssembly, false);
                Debug.Assert(ComTypeCache.ContainsKey(typeInfoGuid));
                if (!ComTypeCache.ContainsKey(typeInfoGuid))
                    throw new COMException("TypeLib " + interopAssebly + " does not contain COM interface + " + typeInfoGuid);
                return ComTypeCache[typeInfoGuid];
            }
            #endif
            // Try creating an Interop assembly on the fly
            return ConvertTypeLibToAssembly(context, typeInfoPtr, typeInfoGuid);
        }
コード例 #5
0
 internal Assembly GetPrimaryInteropAssembly(System.Runtime.InteropServices.ComTypes.ITypeLib typeLib, TypeLibConverter tlbConverter)
 {
     Assembly assem = this.FindRCW(typeLib);
     if (assem == null)
     {
         IntPtr invalidIntPtr = System.Design.NativeMethods.InvalidIntPtr;
         typeLib.GetLibAttr(out invalidIntPtr);
         if (!(invalidIntPtr != System.Design.NativeMethods.InvalidIntPtr))
         {
             return assem;
         }
         System.Runtime.InteropServices.TYPELIBATTR typelibattr = (System.Runtime.InteropServices.TYPELIBATTR) Marshal.PtrToStructure(invalidIntPtr, typeof(System.Runtime.InteropServices.TYPELIBATTR));
         string asmName = null;
         string asmCodeBase = null;
         try
         {
             tlbConverter.GetPrimaryInteropAssembly(typelibattr.guid, typelibattr.wMajorVerNum, typelibattr.wMinorVerNum, typelibattr.lcid, out asmName, out asmCodeBase);
             if ((asmName != null) && (asmCodeBase == null))
             {
                 try
                 {
                     assem = Assembly.ReflectionOnlyLoad(asmName);
                     asmCodeBase = this.GetLocalPath(assem.EscapedCodeBase);
                 }
                 catch (Exception)
                 {
                 }
             }
             else if (asmCodeBase != null)
             {
                 asmCodeBase = this.GetLocalPath(asmCodeBase);
                 assem = Assembly.ReflectionOnlyLoadFrom(asmCodeBase);
             }
             if (assem != null)
             {
                 this.AddRCW(typeLib, assem);
                 this.AddReferencedAssembly(asmCodeBase);
             }
         }
         finally
         {
             typeLib.ReleaseTLibAttr(invalidIntPtr);
         }
     }
     return assem;
 }