public static int Run() { int RetCode = SuccessReturnCode; try { // Call SearchPath to find the full path of the typelib to load. StringBuilder sb = new StringBuilder(MAX_PATH + 1); if (SearchPath(null, s_Options.m_strTypeLibName, null, sb.Capacity + 1, sb, null) == 0) { // We failed to find the typelib. This might be because a resource ID is specified // so lets let LoadTypeLibEx have a crack at it but remember that we failed to find it. s_Options.m_bSearchPathSucceeded = false; } else { // We found the typelib. s_Options.m_bSearchPathSucceeded = true; s_Options.m_strTypeLibName = sb.ToString(); } // Retrieve the full path name of the typelib and output file. s_Options.m_strTypeLibName = (new FileInfo(s_Options.m_strTypeLibName)).FullName; if (s_Options.m_strAssemblyName != null) { s_Options.m_strAssemblyName = (new FileInfo(s_Options.m_strAssemblyName)).FullName; if (Directory.Exists(s_Options.m_strAssemblyName)) { throw new ApplicationException(Resource.FormatString("Err_OutputCannotBeDirectory")); } } // Determine the output directory for the generated assembly. if (s_Options.m_strAssemblyName != null) { // An output file has been provided so use its directory as the output directory. s_Options.m_strOutputDir = Path.GetDirectoryName(s_Options.m_strAssemblyName); } else { // No output file has been provided so use the current directory as the output directory. s_Options.m_strOutputDir = Environment.CurrentDirectory; } if (!Directory.Exists(s_Options.m_strOutputDir)) { Directory.CreateDirectory(s_Options.m_strOutputDir); } // If the output directory is different from the current directory then change to that directory. if (String.Compare(s_Options.m_strOutputDir, Environment.CurrentDirectory, true, CultureInfo.InvariantCulture) != 0) { Environment.CurrentDirectory = s_Options.m_strOutputDir; } // Create an AppDomain to load the implementation part of the app into. AppDomainSetup options = new AppDomainSetup(); options.ApplicationBase = s_Options.m_strOutputDir; AppDomain domain = AppDomain.CreateDomain("TlbImp", null, options); if (domain == null) { throw new ApplicationException(Resource.FormatString("Err_CannotCreateAppDomain")); } // Create the remote component that will perform the rest of the conversion. String assemblyName = typeof(TlbImpCode.RemoteTlbImp).Assembly.GetName().FullName; ObjectHandle h = domain.CreateInstance(assemblyName, "TlbImpCode.RemoteTlbImp"); if (h == null) { throw new ApplicationException(Resource.FormatString("Err_CannotCreateRemoteTlbImp")); } // Have the remote component perform the rest of the conversion. RemoteTlbImp code = (RemoteTlbImp)h.Unwrap(); if (code != null) { RetCode = code.Run(s_Options); } // Do not unload the domain, this causes problems with IJW MC++ assemblies that // call managed code from DllMain's ProcessDetach notification. } catch (Exception e) { WriteErrorMsg(null, e); RetCode = ErrorReturnCode; } return(RetCode); }
public static int Run() { int RetCode = SuccessReturnCode; string TypeLibName = s_Options.m_strTypeLibName; s_Options.m_strTypeLibName = GetFullPath(s_Options.m_strTypeLibName, true); if (s_Options.m_strTypeLibName == null) { // We failed to find the typelib. This might be because a resource ID is specified // so let's have LoadTypeLibEx try to load it but remember that we failed to find it. s_Options.m_bSearchPathSucceeded = false; s_Options.m_strTypeLibName = TypeLibName; } else { // We found the typelib. s_Options.m_bSearchPathSucceeded = true; } // Retrieve the full path name of the output file. if ("".Equals(Path.GetExtension(s_Options.m_strAssemblyName))) { s_Options.m_strAssemblyName = s_Options.m_strAssemblyName + ".dll"; } if (s_Options.m_strAssemblyName != null) { try { s_Options.m_strAssemblyName = (new FileInfo(s_Options.m_strAssemblyName)).FullName; } catch (System.IO.PathTooLongException) { throw new TlbImpGeneralException(Resource.FormatString("Err_OutputFileNameTooLong", s_Options.m_strAssemblyName), ErrorCode.Err_OutputFileNameTooLong); } if (Directory.Exists(s_Options.m_strAssemblyName)) { throw new TlbImpGeneralException(Resource.FormatString("Err_OutputCannotBeDirectory"), ErrorCode.Err_OutputCannotBeDirectory); } } // Determine the output directory for the generated assembly. if (s_Options.m_strAssemblyName != null) { // An output file has been provided so use its directory as the output directory. s_Options.m_strOutputDir = Path.GetDirectoryName(s_Options.m_strAssemblyName); } else { // No output file has been provided so use the current directory as the output directory. s_Options.m_strOutputDir = Environment.CurrentDirectory; } if (!Directory.Exists(s_Options.m_strOutputDir)) { try { Directory.CreateDirectory(s_Options.m_strOutputDir); } catch (System.IO.IOException) { throw new TlbImpGeneralException(Resource.FormatString("Err_InvalidOutputDirectory"), ErrorCode.Err_InvalidOutputDirectory); } } // If the output directory is different from the current directory then change to that directory. if (String.Compare(s_Options.m_strOutputDir, Environment.CurrentDirectory, true, CultureInfo.InvariantCulture) != 0) { Environment.CurrentDirectory = s_Options.m_strOutputDir; } // TlbImp uses ReflectionOnly loading. s_Options.m_flags |= TypeLibImporterFlags.ReflectionOnlyLoading; // Create an AppDomain to load the implementation part of the app into. AppDomainSetup options = new AppDomainSetup(); options.ApplicationBase = s_Options.m_strOutputDir; AppDomain domain = AppDomain.CreateDomain("TlbImp", null, options); if (domain == null) { throw new TlbImpGeneralException(Resource.FormatString("Err_CannotCreateAppDomain"), ErrorCode.Err_CannotCreateAppDomain); } // Create the remote component that will perform the rest of the conversion. ObjectHandle h = domain.CreateInstanceFrom(typeof(TlbImpCode.RemoteTlbImp).Assembly.CodeBase, "TlbImpCode.RemoteTlbImp"); if (h == null) { throw new TlbImpGeneralException(Resource.FormatString("Err_CannotCreateRemoteTlbImp"), ErrorCode.Err_CannotCreateRemoteTlbImp); } // Have the remote component perform the rest of the conversion. RemoteTlbImp code = (RemoteTlbImp)h.Unwrap(); // We just can't pass the TlbImpOptions class, because remoting won't be able // to resolve the object when it re-emerges in the other AppDomain, because the // other AppDomain doesn't know anything about the executing assembly (where // TlbImpOptions is defined) if (code != null) { RetCode = code.Run(s_Options.m_strTypeLibName, s_Options.m_strAssemblyName, s_Options.m_strAssemblyNamespace, s_Options.m_strOutputDir, s_Options.m_aPublicKey, s_Options.m_sKeyPair, s_Options.m_strAssemblyRefList, s_Options.m_strTypeLibRefList, s_Options.m_AssemblyVersion, s_Options.m_flags, s_Options.m_bNoLogo, s_Options.m_bSilentMode, s_Options.m_silenceList, s_Options.m_bVerboseMode, s_Options.m_bStrictRef, s_Options.m_bStrictRefNoPia, s_Options.m_bSearchPathSucceeded, s_Options.m_strProduct, s_Options.m_strProductVersion, s_Options.m_strCompany, s_Options.m_strCopyright, s_Options.m_strTrademark, s_Options.m_isVersion2, s_Options.m_isPreserveSig, s_Options.m_isRemoveEnumPrefix); } // Unload the app domain now that we've finished the import. AppDomain.Unload(domain); return(RetCode); }