private static extern void LoadTypeLibEx(string strTypeLibName, REGKIND regKind, out ITypeLib TypeLib);
public static extern void LoadTypeLibEx(String strTypeLibName, REGKIND regKind, out System.Runtime.InteropServices.ComTypes.ITypeLib TypeLib);
private static extern int LoadTypeLibEx(string strTypeLibName, REGKIND regKind, out ITypeLib TypeLib);
private static extern void LoadTypeLibEx(String strTypeLibName, REGKIND regKind, out UCOMITypeLib TypeLib);
public static extern int LoadTypeLibEx( [In, MarshalAs(UnmanagedType.LPWStr)] string fileName, REGKIND regKind, out ComTypes.ITypeLib typeLib);
public static extern ITypeLib LoadTypeLibEx(string szFile, REGKIND regkind);
private static extern int LoadTypeLibWithResolver([MarshalAs(UnmanagedType.LPWStr)] string file, REGKIND kind, ITypeLibResolver resolver, out TypeLibTypes.Interop.ITypeLib typeLib);
//************************************************************************** // Entry point called on the typelib importer in the proper app domain. //************************************************************************** public static int Run(TlbImpOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); s_Options = options; Output.SetSilent(options.m_bSilentMode); Output.Silence(options.m_silenceList); System.Runtime.InteropServices.ComTypes.ITypeLib TypeLib = null; String strPIAName = null; String strPIACodeBase = null; s_RK = REGKIND.REGKIND_NONE; if (Environment.OSVersion.Platform != PlatformID.Win32Windows) { if (IsImportingToItanium(options.m_flags) || IsImportingToX64(options.m_flags)) { s_RK |= REGKIND.REGKIND_LOAD_TLB_AS_64BIT; } else if (IsImportingToX86(options.m_flags)) { s_RK |= REGKIND.REGKIND_LOAD_TLB_AS_32BIT; } } //---------------------------------------------------------------------- // Load the typelib. try { LoadTypeLibEx(s_Options.m_strTypeLibName, s_RK, out TypeLib); s_RefTypeLibraries.Add(s_Options.m_strTypeLibName, TypeLib); } catch (COMException e) { if (!s_Options.m_bSearchPathSucceeded) { // We failed to search for the typelib and we failed to load it. // This means that the input typelib is not available. Output.WriteError(Resource.FormatString("Err_InputFileNotFound", s_Options.m_strTypeLibName), ErrorCode.Err_InputFileNotFound); } else { if (e.ErrorCode == unchecked((int)0x80029C4A)) { Output.WriteError(Resource.FormatString("Err_InputFileNotValidTypeLib", s_Options.m_strTypeLibName), ErrorCode.Err_InputFileNotValidTypeLib); } else { Output.WriteError(Resource.FormatString("Err_TypeLibLoad", e), ErrorCode.Err_TypeLibLoad); } } return ErrorReturnCode; } catch (Exception e) { Output.WriteError(Resource.FormatString("Err_TypeLibLoad", e), ErrorCode.Err_TypeLibLoad); return ErrorReturnCode; } //---------------------------------------------------------------------- // Check to see if there already exists a primary interop assembly for // this typelib. if (TlbImpCode.GetPrimaryInteropAssembly(TypeLib, out strPIAName, out strPIACodeBase)) { Output.WriteWarning(Resource.FormatString("Wrn_PIARegisteredForTlb", strPIAName, s_Options.m_strTypeLibName), WarningCode.Wrn_PIARegisteredForTlb); } //---------------------------------------------------------------------- // Retrieve the name of output assembly if it was not explicitly set. if (s_Options.m_strAssemblyName == null) { s_Options.m_strAssemblyName = Marshal.GetTypeLibName(TypeLib) + ".dll"; } //---------------------------------------------------------------------- // Do some verification on the output assembly. String strFileNameNoPath = Path.GetFileName(s_Options.m_strAssemblyName); String strExtension = Path.GetExtension(s_Options.m_strAssemblyName); // Validate that the extension is valid. bool bExtensionValid = ".dll".Equals(strExtension.ToLower(CultureInfo.InvariantCulture)); // If the extension is not valid then tell the user and quit. if (!bExtensionValid) { Output.WriteError(Resource.FormatString("Err_InvalidExtension"), ErrorCode.Err_InvalidExtension); return ErrorReturnCode; } // Make sure the output file will not overwrite the input file. String strInputFilePath = (new FileInfo(s_Options.m_strTypeLibName)).FullName.ToLower(CultureInfo.InvariantCulture); String strOutputFilePath; try { strOutputFilePath = (new FileInfo(s_Options.m_strAssemblyName)).FullName.ToLower(CultureInfo.InvariantCulture); } catch (System.IO.PathTooLongException) { Output.WriteError(Resource.FormatString("Err_OutputFileNameTooLong", s_Options.m_strAssemblyName), ErrorCode.Err_OutputFileNameTooLong); return ErrorReturnCode; } if (strInputFilePath.Equals(strOutputFilePath)) { Output.WriteError(Resource.FormatString("Err_OutputWouldOverwriteInput"), ErrorCode.Err_OutputWouldOverwriteInput); return ErrorReturnCode; } //------------------------------------------------------------------------- // Load all assemblies provided as explicit references on the command line. if (s_Options.m_strAssemblyRefList != null) { String[] asmPaths = s_Options.m_strAssemblyRefList.Split(';'); foreach (String asmPath in asmPaths) { if (!LoadAssemblyRef(asmPath)) return ErrorReturnCode; } } //------------------------------------------------------------------------- // And the same for type library references. if (s_Options.m_strTypeLibRefList != null) { String[] tlbPaths = s_Options.m_strTypeLibRefList.Split(';'); foreach (String tlbPath in tlbPaths) { if (!LoadTypeLibRef(tlbPath)) return ErrorReturnCode; } } //------------------------------------------------------------------------- // Before we attempt the import, verify the references first if (!VerifyTypeLibReferences(s_Options.m_strTypeLibName)) return ErrorReturnCode; //---------------------------------------------------------------------- // Attempt the import. try { try { // Import the typelib to an assembly. AssemblyBuilder AsmBldr = DoImport(TypeLib, s_Options.m_strAssemblyName, s_Options.m_strAssemblyNamespace, s_Options.m_AssemblyVersion, s_Options.m_aPublicKey, s_Options.m_sKeyPair, s_Options.m_strProduct, s_Options.m_strProductVersion, s_Options.m_strCompany, s_Options.m_strCopyright, s_Options.m_strTrademark, s_Options.m_flags, s_Options.m_isVersion2, s_Options.m_isPreserveSig, s_Options.m_ruleSetFileName); if (AsmBldr == null) return ErrorReturnCode; } catch (TlbImpResolveRefFailWrapperException ex) { // Throw out the inner exception instead throw ex.InnerException; } } catch (ReflectionTypeLoadException e) { Output.WriteError(Resource.FormatString("Err_TypeLoadExceptions"), ErrorCode.Err_TypeLoadExceptions); Exception[] exceptions = e.LoaderExceptions; for (int i = 0; i < exceptions.Length; i++) { try { Output.WriteInfo(Resource.FormatString("Msg_DisplayException", new object[] { i, exceptions[i].GetType().ToString(), exceptions[i].Message }), MessageCode.Msg_DisplayException); } catch (Exception ex) { Output.WriteInfo(Resource.FormatString("Msg_DisplayNestedException", new object [] { i, ex.GetType().ToString(), ex.Message }), MessageCode.Msg_DisplayNestedException); } } return ErrorReturnCode; } catch (TlbImpGeneralException tge) { Output.WriteTlbimpGeneralException(tge); return ErrorReturnCode; } catch (COMException ex) { if ((uint)ex.ErrorCode == HResults.TYPE_E_CANTLOADLIBRARY) { // Give a more specific message Output.WriteError(Resource.FormatString("Err_RefTlbCantLoad"), ErrorCode.Err_RefTlbCantLoad); } else { // TlbImp COM exception string msg = Resource.FormatString( "Err_UnexpectedException", ex.GetType().ToString(), ex.Message ); Output.WriteError(msg, ErrorCode.Err_UnexpectedException); } return ErrorReturnCode; } catch (TlbImpInvalidTypeConversionException ex) { // This usually means that a type conversion has failed outside normal conversion process... string name = null; try { name = ex.Type.GetDocumentation(); } catch(Exception) { } if (name != null) Output.WriteError(Resource.FormatString("Err_FatalErrorInConversion_Named", name), ErrorCode.Err_FatalErrorInConversion_Named); else Output.WriteError(Resource.FormatString("Err_FatalErrorInConversion_Unnamed"), ErrorCode.Err_FatalErrorInConversion_Unnamed); return ErrorReturnCode; } catch (SecurityException ex) { // Only treat SecurityException with PermissionType != null as permission issue if (ex.PermissionType == null) { string msg = Resource.FormatString( "Err_UnexpectedException", ex.GetType().ToString(), ex.Message ); Output.WriteError(msg, ErrorCode.Err_UnexpectedException); } else { Output.WriteError(Resource.GetString("Err_PermissionException"), ErrorCode.Err_PermissionException); } return ErrorReturnCode; } catch (Exception ex) { string msg = Resource.FormatString( "Err_UnexpectedException", ex.GetType().ToString(), ex.Message ); Output.WriteError(msg, ErrorCode.Err_UnexpectedException); return ErrorReturnCode; } Output.WriteInfo(Resource.FormatString("Msg_TypeLibImported", s_Options.m_strAssemblyName), MessageCode.Msg_TypeLibImported); return SuccessReturnCode; }
private static extern void LoadTypeLibEx(string typeLibName, REGKIND regKind, out ITypeLib typeLib);