/// <summary> /// This is an helper method to convert the VSCOMPONENTSELECTORDATA recieved by the /// implementer of IVsComponentUser into a ProjectElement that can be used to create /// an instance of this class. /// This should not be called for project reference or reference to managed assemblies. /// </summary> /// <returns>ProjectElement corresponding to the COM component passed in</returns> 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().ToLower()); } else { // MSBuild will have to generate an interop assembly element.SetMetadata(ProjectFileConstants.WrapperTool, WrapperToolAttributeValue.TlbImp.ToString().ToLower()); element.SetMetadata(ProjectFileConstants.Private, true.ToString()); } return(element); }
internal Assembly GetPrimaryInteropAssembly(UCOMITypeLib typeLib, TypeLibConverter tlbConverter) { Assembly pia = FindRCW(typeLib); if (pia != null) { return(pia); } IntPtr typeLibAttr = NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out typeLibAttr); if (typeLibAttr != NativeMethods.InvalidIntPtr) { // Marshal the returned int as a TLibAttr structure // TYPELIBATTR tlbAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttr, typeof(TYPELIBATTR)); string asmName = null; string asmCodeBase = null; try { tlbConverter.GetPrimaryInteropAssembly(tlbAttr.guid, tlbAttr.wMajorVerNum, tlbAttr.wMinorVerNum, tlbAttr.lcid, out asmName, out asmCodeBase); if (asmName != null && asmCodeBase == null) { // We found the PIA in the GAC... we need a codebase for this // so we can pass this to the compiler. // try { pia = Assembly.Load(asmName); asmCodeBase = GetLocalPath(pia.EscapedCodeBase); } catch (Exception e) { Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Could load assembly from GAC " + asmName + " Exception " + e.Message); } } else if (asmCodeBase != null) { asmCodeBase = GetLocalPath(asmCodeBase); pia = Assembly.LoadFrom(asmCodeBase); } if (pia != null) { AddRCW(typeLib, pia); AddReferencedAssembly(asmCodeBase); } } finally { typeLib.ReleaseTLibAttr(typeLibAttr); } } return(pia); }
/// <summary> /// Try to search the GAC for an assembly that seems to match the given type GUID /// </summary> private static Assembly SearchForInteropAssembly(Guid typeInfoGuid) { Assembly assembly = null; try { string typelibPath = @"Interface\{" + typeInfoGuid + @"}\TypeLib"; RegistryKey typelibKey = Registry.ClassesRoot.OpenSubKey(typelibPath); if (typelibKey == null) { return(null); } // Read HKCR\Interface\{<typeInfoGuid>}\TypeLib\(Default)" Guid typelibGuid = new Guid(typelibKey.GetValue(null, null) as string); // Read HKCR\Interface\{<typeInfoGuid>}\TypeLib\Version" string typelibVersion = typelibKey.GetValue("Version") as String; if (typelibVersion == null) { return(null); } string[] versionSplit = typelibVersion.Split('.'); int major = int.Parse(versionSplit[0]); int minor = int.Parse(versionSplit[1]); // Try using TypeLibConverter TypeLibConverter tlc = new TypeLibConverter(); string asmName = null, asmCodeBase = null; tlc.GetPrimaryInteropAssembly(typelibGuid, major, minor, 0, out asmName, out asmCodeBase); if (asmName != null) { assembly = Assembly.Load(asmName); } if (assembly != null) { return(assembly); } // Next, try looking up the GAC assembly = SearchForInteropAssemblyInGAC(typeInfoGuid, major, minor); } catch (Exception e) { if (IronPython.Hosting.PythonEngine.options.EngineDebug) { throw e; } } return(assembly); }
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); }
internal override bool FindExistingWrapper(out ComReferenceWrapperInfo wrapperInfo, DateTime componentTimestamp) { string str; string str2; wrapperInfo = null; TypeLibConverter converter = new TypeLibConverter(); if (!converter.GetPrimaryInteropAssembly(this.ReferenceInfo.attr.guid, this.ReferenceInfo.attr.wMajorVerNum, this.ReferenceInfo.attr.wMinorVerNum, this.ReferenceInfo.attr.lcid, out str, out str2)) { return(false); } try { if ((str2 != null) && (str2.Length > 0)) { Uri uri = new Uri(str2); Assembly assembly = Assembly.UnsafeLoadFrom(uri.LocalPath); wrapperInfo = new ComReferenceWrapperInfo(); wrapperInfo.path = uri.LocalPath; wrapperInfo.assembly = assembly; wrapperInfo.originalPiaName = new AssemblyNameExtension(AssemblyName.GetAssemblyName(uri.LocalPath)); } else { Assembly assembly2 = Assembly.Load(str); wrapperInfo = new ComReferenceWrapperInfo(); wrapperInfo.path = assembly2.Location; wrapperInfo.assembly = assembly2; wrapperInfo.originalPiaName = new AssemblyNameExtension(str, true); } } catch (FileNotFoundException) { } catch (BadImageFormatException) { base.Log.LogWarningWithCodeFromResources("ResolveComReference.BadAssemblyImage", new object[] { str }); } return(wrapperInfo != null); }
/* * 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); }
internal static Assembly generateAssemblyFromTypeLib(UCOMITypeLib typeLib, bool safeArrayAsArray, string asmFilePath, out bool foundPIAInstead) { foundPIAInstead = false; string typeLibName = Marshal.GetTypeLibName(typeLib); string asmDir, asmName, asmFullPath; if (asmFilePath == "") { // asmFilePath == "" means that user does not want the assembly saved in a file. A filename argument // is required by the conversion function, although no file is actually written by it. Therefore we // supply one in case it is truly necessary. asmName = "interop." + typeLibName + ".dll"; asmDir = ""; } else { asmDir = System.IO.Path.GetDirectoryName(asmFilePath); if (asmDir == null) { // asmFilePath was a root dir, like c:\. string asmRoot = System.IO.Path.GetPathRoot(asmFilePath); asmDir = asmRoot == null ? "" : asmRoot; } else { asmDir = asmDir + System.IO.Path.DirectorySeparatorChar; } // M code ensures that if asmFilePath is a dir and not a filename it ends in \, so asmName will be "" // if it is a dir. asmName = System.IO.Path.GetFileName(asmFilePath); if (asmName == "") { // asmFilePath was just a dir. asmName = "interop." + typeLibName + ".dll"; } } asmFullPath = asmDir + asmName; ImporterNotiferSink sink = new ImporterNotiferSink(safeArrayAsArray, asmDir); TypeLibConverter tlc = new TypeLibConverter(); // Check for an existing PIA and use it if it exists. IntPtr pTLibAttr; typeLib.GetLibAttr(out pTLibAttr); TYPELIBATTR typeLibAttr = (TYPELIBATTR)Marshal.PtrToStructure(pTLibAttr, typeof(TYPELIBATTR)); string piaName, piaCodeBase; bool piaExists = tlc.GetPrimaryInteropAssembly(typeLibAttr.guid, typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum, typeLibAttr.lcid, out piaName, out piaCodeBase); typeLib.ReleaseTLibAttr(pTLibAttr); if (piaExists) { Assembly pia = Assembly.LoadWithPartialName(piaName); if (pia != null) { foundPIAInstead = true; return(pia); } } TypeLoader.isBuildingDynamicAssembly = true; try { AssemblyBuilder asmBuilder = tlc.ConvertTypeLibToAssembly(typeLib, asmFullPath, safeArrayAsArray ? TypeLibImporterFlags.SafeArrayAsSystemArray : 0, sink, null, null, typeLibName, null); Type[] tt = asmBuilder.GetTypes(); if (asmFilePath != "") { asmBuilder.Save(asmName); } return(asmBuilder); } finally { TypeLoader.isBuildingDynamicAssembly = false; } }