internal void AddRCW(System.Runtime.InteropServices.ComTypes.ITypeLib typeLib, Assembly assem) { if (this.rcwCache == null) { this.rcwCache = new Hashtable(); } IntPtr invalidIntPtr = System.Design.NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out invalidIntPtr); try { if (invalidIntPtr != System.Design.NativeMethods.InvalidIntPtr) { System.Runtime.InteropServices.TYPELIBATTR typelibattr = (System.Runtime.InteropServices.TYPELIBATTR) Marshal.PtrToStructure(invalidIntPtr, typeof(System.Runtime.InteropServices.TYPELIBATTR)); this.rcwCache.Add(typelibattr.guid, assem); } } finally { typeLib.ReleaseTLibAttr(invalidIntPtr); } }
//************************************************************************** // Static importer function used by main and the callback. //************************************************************************** public static AssemblyBuilder DoImport(System.Runtime.InteropServices.ComTypes.ITypeLib TypeLib, String strAssemblyFileName, String strAssemblyNamespace, Version asmVersion, byte[] publicKey, StrongNameKeyPair keyPair, String strProduct, String strProductVersion, String strCompany, String strCopyright, String strTrademark, TypeLibImporterFlags flags, bool isVersion2, bool isPreserveSig, String ruleSetFileName) { if (TypeLib == null) throw new ArgumentNullException(nameof(TypeLib)); // Detemine the assembly file name. String asmFileName = Path.GetFileName(strAssemblyFileName); // Add this typelib to list of importing typelibs. Guid guid = Marshal.GetTypeLibGuid(TypeLib); s_ImportingLibraries.Add(guid.ToString(), guid); // If the type library is 64-bit, make sure the user specified a platform type. IntPtr pTLibAttr = IntPtr.Zero; TypeLib.GetLibAttr(out pTLibAttr); _TYPELIBATTR TLibAttr = (_TYPELIBATTR)Marshal.PtrToStructure(pTLibAttr, typeof(_TYPELIBATTR)); TypeLib.ReleaseTLibAttr(pTLibAttr); // Validate the machine options if (!ValidateMachineType(flags, TLibAttr.syskind)) return null; // Convert the typelib. ImporterCallback callback = new ImporterCallback(); tlbimp2.Process process = new Process(); AssemblyBuilder AsmBldr = process.DoProcess( TypeLib, strAssemblyFileName, flags, callback, publicKey, keyPair, strAssemblyNamespace, asmVersion, isVersion2, isPreserveSig, ruleSetFileName); if (AsmBldr == null) return null; // Remove this typelib from list of importing typelibs. s_ImportingLibraries.Remove(guid.ToString()); // Delete the output assembly. File.Delete(asmFileName); AsmBldr.DefineVersionInfoResource(strProduct, strProductVersion, strCompany, strCopyright, strTrademark); if (IsImportingToX64(flags)) { AsmBldr.Save(asmFileName, PortableExecutableKinds.ILOnly | PortableExecutableKinds.PE32Plus, ImageFileMachine.AMD64); } else if (IsImportingToItanium(flags)) { AsmBldr.Save(asmFileName, PortableExecutableKinds.ILOnly | PortableExecutableKinds.PE32Plus, ImageFileMachine.IA64); } else if (IsImportingToX86(flags)) { AsmBldr.Save(asmFileName, PortableExecutableKinds.ILOnly | PortableExecutableKinds.Required32Bit, ImageFileMachine.I386); } else { // Default is agnostic AsmBldr.Save(asmFileName); } return AsmBldr; }
private void AddTypeLibAttr(System.Runtime.InteropServices.ComTypes.ITypeLib typeLib) { if (this.tlbAttrs == null) { this.tlbAttrs = new ArrayList(); } IntPtr invalidIntPtr = System.Design.NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out invalidIntPtr); if (invalidIntPtr != System.Design.NativeMethods.InvalidIntPtr) { System.Runtime.InteropServices.TYPELIBATTR typelibattr = (System.Runtime.InteropServices.TYPELIBATTR) Marshal.PtrToStructure(invalidIntPtr, typeof(System.Runtime.InteropServices.TYPELIBATTR)); this.tlbAttrs.Add(typelibattr); typeLib.ReleaseTLibAttr(invalidIntPtr); } }
private static string GetFileOfTypeLib(System.Runtime.InteropServices.ComTypes.ITypeLib typeLib) { IntPtr invalidIntPtr = System.Design.NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out invalidIntPtr); if (invalidIntPtr != System.Design.NativeMethods.InvalidIntPtr) { System.Runtime.InteropServices.TYPELIBATTR tlibattr = (System.Runtime.InteropServices.TYPELIBATTR) Marshal.PtrToStructure(invalidIntPtr, typeof(System.Runtime.InteropServices.TYPELIBATTR)); try { return GetFileOfTypeLib(ref tlibattr); } finally { typeLib.ReleaseTLibAttr(invalidIntPtr); } } return null; }
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 Assembly FindRCW(System.Runtime.InteropServices.ComTypes.ITypeLib typeLib) { if (this.rcwCache != null) { IntPtr invalidIntPtr = System.Design.NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out invalidIntPtr); try { if (invalidIntPtr != System.Design.NativeMethods.InvalidIntPtr) { System.Runtime.InteropServices.TYPELIBATTR typelibattr = (System.Runtime.InteropServices.TYPELIBATTR) Marshal.PtrToStructure(invalidIntPtr, typeof(System.Runtime.InteropServices.TYPELIBATTR)); return (Assembly) this.rcwCache[typelibattr.guid]; } } finally { typeLib.ReleaseTLibAttr(invalidIntPtr); } } return null; }