public static void Run(GenMan32Options Options)
        {
            if (!File.Exists(Path.GetFullPath(Options.m_strAssemblyName)))
            {
                throw new ApplicationException("Input file is not found");
            }

            String strTlbName = null;

            if (Options.m_bGenerateTypeLib)
            {
                strTlbName = GenerateTypeLibUsingHelper(Options.m_strAssemblyName);
            }
            if (strTlbName != null) // strTlbName == null if the assembly has embedded typelib.
            {
                EmbedTypeLibToAssembly(strTlbName, Options.m_strAssemblyName);
            }

            String strOutputFileName = Options.m_strAssemblyName + ".Manifest";

            if (Options.m_bAddManifest)
            {                                               // Add the manifest file to the assembly as a resource
                if (Options.m_strInputManifestFile != null) // input file specified
                {
                    AddManifestToAssembly(Options.m_strInputManifestFile, Options.m_strAssemblyName);
                }
                else  // no input file specified
                {
                    strOutputFileName += ".tmp"; // change it to a tmp file
                    if (Options.m_strOutputFileName != null)
                    {
                        strOutputFileName = Options.m_strOutputFileName;
                        if (File.Exists(strOutputFileName))
                        {
                            throw new ApplicationException(String.Format("Manifest file {0} exists", strOutputFileName));
                        }
                    }

                    // generate the tmp manifest file, add it to assembly, and delete the tmp file if needed

                    // Here we need use a helper. Because GenerateWin32ManifestFile will
                    // load strAssemblyName, thus prevent any change to the assembly(AddManifestToAssembly).
                    // The helper will run in a separate domain. And when it returns, it releases the lock on the assembly.
                    GenerateWin32ManifestFileUsingHelper(strOutputFileName, Options.m_strAssemblyName, Options.m_bGenerateTypeLib, Options.m_strReferenceFiles);
                    try
                    {
                        AddManifestToAssembly(strOutputFileName, Options.m_strAssemblyName);
                    }
                    catch (Exception e)
                    {
                        if (Options.m_strOutputFileName == null)
                        {
                            File.Delete(strOutputFileName);
                        }
                        throw e;
                    }

                    if (Options.m_strOutputFileName == null)
                    {
                        File.Delete(strOutputFileName);
                    }
                }
            }
            else if (Options.m_bRemoveManifest) // remove manifest from an assembly
            {
                RemoveManifestFromAssembly(Options.m_strAssemblyName);
            }
            else if (Options.m_bReplaceManifest)
            {
                ReplaceManifestInAssembly(Options.m_strInputManifestFile, Options.m_strAssemblyName);
            }
            else // generate manifest file only
            {
                if (Options.m_strOutputFileName != null)
                {
                    strOutputFileName = Options.m_strOutputFileName;
                }
                else
                {
                    Options.m_strOutputFileName = strOutputFileName; // copy out output filename
                }
                if (File.Exists(strOutputFileName))
                {
                    throw new ApplicationException(String.Format("Manifest file {0} exists.", strOutputFileName));
                }

                GenerateWin32ManifestFile(strOutputFileName, Options.m_strAssemblyName, Options.m_bGenerateTypeLib, Options.m_strReferenceFiles);
            }
        }
示例#2
0
        private static bool ParseArguments(String[] args, ref GenMan32Options options)
        {
            CommandLine cmdLine;
            Option      opt;

            options = new GenMan32Options();

            try
            {
                cmdLine = new CommandLine(args, new String[] { "add", "*out", "remove", "replace", "*manifest", "silent", "?", "help", "typelib", "*reference" });
            }
            catch (ApplicationException e)
            {
                PrintLogo();
                Console.WriteLine(e.Message);
                return(false);
            }

            if ((cmdLine.NumArgs + cmdLine.NumOpts) < 1)
            {
                PrintUsage();
                return(false);
            }

            StringBuilder sb = null;

            // Get the name of the input assembly
            options.m_strAssemblyName = cmdLine.GetNextArg();

            while ((opt = cmdLine.GetNextOption()) != null)
            {
                // Dermine which option was specified
                if (opt.Name.Equals("add"))
                {
                    options.m_bAddManifest = true;
                }
                else if (opt.Name.Equals("out"))
                {
                    options.m_strOutputFileName = opt.Value;
                }
                else if (opt.Name.Equals("manifest"))
                {
                    options.m_strInputManifestFile = opt.Value;
                }
                else if (opt.Name.Equals("silent"))
                {
                    options.m_bSilentMode = true;
                }
                else if (opt.Name.Equals("remove"))
                {
                    options.m_bRemoveManifest = true;
                }
                else if (opt.Name.Equals("replace"))
                {
                    options.m_bReplaceManifest = true;
                }
                else if (opt.Name.Equals("typelib"))
                {
                    options.m_bGenerateTypeLib = true;
                }
                else if (opt.Name.Equals("reference"))
                {
                    if (sb == null)
                    {
                        sb = new StringBuilder(opt.Value);
                    }
                    else
                    {
                        // '?' is the separator
                        sb.Append("?");
                        sb.Append(opt.Value);
                    }
                    options.m_strReferenceFiles = sb.ToString();
                }
                else if (opt.Name.Equals("?") || opt.Name.Equals("help"))
                {
                    PrintUsage();
                    return(false);
                }
                else
                {
                    PrintLogo();
                    Console.WriteLine("Error: Invalid Option.");
                    return(false);
                }
            }// end of while

            // Make sure input assembly is specified
            if (options.m_strAssemblyName == null)
            {
                PrintLogo();
                Console.WriteLine("Error: No input file.");
                return(false);
            }

            // check options conflict
            if ((options.m_strInputManifestFile != null) && (options.m_strOutputFileName != null))
            {
                PrintLogo();
                Console.WriteLine("Error: /manifest and /out options cannot be used together.");
                return(false);
            }

            if ((options.m_bAddManifest && (options.m_bRemoveManifest || options.m_bReplaceManifest)) ||
                (options.m_bRemoveManifest && (options.m_bAddManifest || options.m_bReplaceManifest)) ||
                (options.m_bReplaceManifest && (options.m_bAddManifest || options.m_bRemoveManifest)))
            {
                PrintLogo();
                Console.WriteLine("Error: only one of /add, /remove /replace options can be specified.");
                return(false);
            }

            if (options.m_bRemoveManifest && options.m_strInputManifestFile != null)
            {
                PrintLogo();
                Console.WriteLine("Error: /remove does not accept /manifest option.");
                return(false);
            }

            if (options.m_bReplaceManifest && options.m_strInputManifestFile == null)
            {
                PrintLogo();
                Console.WriteLine("Error: /replace need to specify new manifest using /manifest option.");
                return(false);
            }

            if (options.m_strInputManifestFile != null) // input file specified
            {
                // verify the manifest file exists
                if (!File.Exists(options.m_strInputManifestFile))
                {
                    PrintLogo();
                    Console.WriteLine("Error: Manifest file {0} does not exist.", options.m_strInputManifestFile);
                    return(false);
                }
            }

            return(true);
        } // end of ParseArguments