コード例 #1
0
        /// <include file='doc\AxToWFC.uex' path='docs/doc[@for="AxImp.Run"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        private static void Run(AxImporter.Options options)
        {
            string[] assemsGenerated;

            AxImporter importer = new AxImporter(options);

            FileInfo file      = new FileInfo(typeLibName);
            string   axctlType = importer.GenerateFromFile(file);

            if (!options.silentMode && options.genSources && importer.GeneratedSources.Length > 0)
            {
                string[] srcGen = importer.GeneratedSources;
                foreach (string s in srcGen)
                {
                    Console.WriteLine(AxImpSR.GetString(AxImpSR.GeneratedSource, s));
                }
            }

            if (!options.silentMode)
            {
                assemsGenerated = importer.GeneratedAssemblies;
                foreach (string a in assemsGenerated)
                {
                    Console.WriteLine(AxImpSR.GetString(AxImpSR.GeneratedAssembly, a));
                }
            }
        }
コード例 #2
0
        /// <include file='doc\AxToWFC.uex' path='docs/doc[@for="AxImp.Main"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        unsafe public static void Main(string[] args)
        {
            Environment.ExitCode = errorCode;

            int err = SuccessReturnCode;

            if (!ParseArguments(args, ref options, ref err))
            {
                return;
            }

            if (!FillRcwReferences(ref err))
            {
                return;
            }

            if (rcwReferences.Count >= 1)
            {
                options.references = (AxImporter.IReferenceResolver)
                                     new AxImporterResolver(rcwReferences);
            }

            try {
                Run(options);
            }
            catch (Exception e) {
                Console.WriteLine(AxImpSR.GetString(AxImpSR.AxImpError, e.Message));
                return;
            }

            Environment.ExitCode = 0;
        }
コード例 #3
0
 private static void PrintLogo()
 {
     if (!options.noLogo)
     {
         Console.WriteLine(AxImpSR.GetString(AxImpSR.Logo, Environment.Version, ThisAssembly.Copyright));
     }
 }
コード例 #4
0
        public String Lookup(String strOpt, out bool bRequiresValue, out bool bCanHaveValue)
        {
            String strOptLower = strOpt.ToLower(CultureInfo.InvariantCulture);
            int    i;
            bool   bMatched = false;
            int    iMatch   = -1;

            // Compare option to stored list.
            for (i = 0; i < m_aOptions.Length; i++)
            {
                // Exact matches always cause immediate termination of
                // the search (else with options foo and foozle, the user
                // could never specify foo unambiguously).
                if (strOptLower.Equals(m_aOptions[i]))
                {
                    bRequiresValue = m_bRequiresValue[i];
                    bCanHaveValue  = m_bCanHaveValue[i];
                    return(m_aOptions[i]);
                }

                // Check for potential match (the input word is a prefix
                // of the current stored option).
                if (m_aOptions[i].StartsWith(strOptLower))
                {
                    // If we've already seen a prefix match then the
                    // input word is ambiguous.
                    if (bMatched)
                    {
                        throw new ApplicationException(AxImpSR.GetString(AxImpSR.Err_AmbigousOption, strOpt));
                    }

                    // Remember this partial match.
                    bMatched = true;
                    iMatch   = i;
                }
            }

            // If we get here with bMatched set, we saw one and only one
            // partial match, so we've got a winner.
            if (bMatched)
            {
                bRequiresValue = m_bRequiresValue[iMatch];
                bCanHaveValue  = m_bCanHaveValue[iMatch];
                return(m_aOptions[iMatch]);
            }

            // Else the word doesn't match at all.
            throw new ApplicationException(AxImpSR.GetString(AxImpSR.Err_UnknownOption, strOpt));
        }
コード例 #5
0
        private static bool FillRcwReferences(ref int returnCode)
        {
            // Stash correspondence between typelib name and RCW dll.  Typelib
            // name found through assembly level attribute on the RCW dll.

            foreach (string rcwPartialPath in rcwOptions)
            {
                string rcwPath = Path.Combine(Environment.CurrentDirectory, rcwPartialPath);

                if (!File.Exists(rcwPath))
                {
                    WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_RefAssemblyNotFound, rcwPath));
                    returnCode = ErrorReturnCode;
                    return(false);
                }

                Assembly asm;
                try
                {
                    asm = Assembly.LoadFrom(rcwPath);
                }
                catch (Exception e)
                {
                    WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_AssemblyLoadFailed, rcwPath, e.Message));
                    returnCode = ErrorReturnCode;
                    return(false);
                }

                object [] attrs = asm.GetCustomAttributes(typeof(ImportedFromTypeLibAttribute), true);
                if (attrs.Length != 1)
                {
                    WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_NotRcw, rcwPath));
                    returnCode = ErrorReturnCode;
                    return(false);
                }

                ImportedFromTypeLibAttribute attr = (ImportedFromTypeLibAttribute)(attrs[0]);
                rcwReferences[attr.Value] = rcwPath;
            }

            return(true);
        }
コード例 #6
0
        internal static void WriteErrorMsg(String strPrefix, Exception e)
        {
            String strErrorMsg = "";

            if (strPrefix != null)
            {
                strErrorMsg = strPrefix;
            }

            if (e.Message != null)
            {
                strErrorMsg += e.Message;
            }
            else
            {
                strErrorMsg += e.GetType().ToString();
            }

            Console.Error.WriteLine(AxImpSR.GetString(AxImpSR.AxImpError, strErrorMsg));
        }
コード例 #7
0
        public CommandLine(String[] aArgs, String[] aValidOpts)
        {
            int i, iArg, iOpt;

            // Keep a list of valid option names.
            m_sValidOptions = new Abbrevs(aValidOpts);

            // Temporary lists of raw arguments and options and their
            // associated values.
            String[] aArgList = new String[aArgs.Length];
            Option[] aOptList = new Option[aArgs.Length];

            // Reset counters of raw arguments and option/value pairs found
            // so far.
            iArg = 0;
            iOpt = 0;

            // Iterate through words of command line.
            for (i = 0; i < aArgs.Length; i++)
            {
                // Check for option or raw argument.
                if (aArgs[i].StartsWith("/") ||
                    aArgs[i].StartsWith("-"))
                {
                    String strOpt;
                    String strVal = null;
                    bool   bRequiresValue;
                    bool   bCanHaveValue;

                    // It's an option. Strip leading '/' or '-' and
                    // anything after a value separator (':' or
                    // '=').
                    int iColon = aArgs[i].IndexOfAny(new char[] { ':', '=' });
                    if (iColon == -1)
                    {
                        strOpt = aArgs[i].Substring(1);
                    }
                    else
                    {
                        strOpt = aArgs[i].Substring(1, iColon - 1);
                    }

                    // Look it up in the table of valid options (to
                    // check it exists, get the full option name and
                    // to see if an associated value is expected).
                    strOpt = m_sValidOptions.Lookup(strOpt, out bRequiresValue, out bCanHaveValue);

                    // Check that the user hasn't specified a value separator for an option
                    // that doesn't take a value.
                    if (!bCanHaveValue && (iColon != -1))
                    {
                        throw new ApplicationException(AxImpSR.GetString(AxImpSR.Err_NoValueRequired, strOpt));
                    }

                    // Check that the user has put a colon if the option requires a value.
                    if (bRequiresValue && (iColon == -1))
                    {
                        throw new ApplicationException(AxImpSR.GetString(AxImpSR.Err_ValueRequired, strOpt));
                    }

                    // Go look for a value if there is one.
                    if (bCanHaveValue && iColon != -1)
                    {
                        if (iColon == (aArgs[i].Length - 1))
                        {
                            // No value separator, or
                            // separator is at end of
                            // option; look for value in
                            // next command line arg.
                            if (i + 1 == aArgs.Length)
                            {
                                throw new ApplicationException(AxImpSR.GetString(AxImpSR.Err_ValueRequired, strOpt));
                            }
                            else
                            {
                                if ((aArgs[i + 1].StartsWith("/") || aArgs[i + 1].StartsWith("-")))
                                {
                                    throw new ApplicationException(AxImpSR.GetString(AxImpSR.Err_ValueRequired, strOpt));
                                }

                                strVal = aArgs[i + 1];
                                i++;
                            }
                        }
                        else
                        {
                            // Value is in same command line
                            // arg as the option, substring
                            // it out.
                            strVal = aArgs[i].Substring(iColon + 1);
                        }
                    }

                    // Build the option value pair.
                    aOptList[iOpt++] = new Option(strOpt, strVal);
                }
                else
                {
                    // Command line word is a raw argument.
                    aArgList[iArg++] = aArgs[i];
                }
            }

            // Allocate the non-temporary arg and option lists at exactly
            // the right size.
            m_aArgList = new String[iArg];
            m_aOptList = new Option[iOpt];

            // Copy in the values we've calculated.
            Array.Copy(aArgList, m_aArgList, iArg);
            Array.Copy(aOptList, m_aOptList, iOpt);

            // Reset enumeration cursors to start of lists.
            m_iArgCursor = 0;
            m_iOptCursor = 0;
        }
コード例 #8
0
        private static bool ParseArguments(String [] aArgs, ref AxImporter.Options Options, ref int ReturnCode)
        {
            CommandLine cmdLine;
            Option      opt;

            // Create the options object that will be returned.
            options = new AxImporter.Options();

            options.outputDirectory = Environment.CurrentDirectory;
            options.overwriteRCW    = true;

            // Parse the command line arguments using the command line argument parser.
            try {
                cmdLine = new CommandLine(aArgs, new String[] { "*out", "*publickey", "*keyfile", "*keycontainer",
                                                                "source", "delaysign", "nologo", "silent", "verbose", "?", "help", "*rcw" });
            }
            catch (ApplicationException e) {
                PrintLogo();
                WriteErrorMsg(null, e);
                ReturnCode = ErrorReturnCode;
                return(false);
            }

            // Make sure there is at least one argument.
            if ((cmdLine.NumArgs + cmdLine.NumOpts) < 1)
            {
                PrintUsage();
                ReturnCode = SuccessReturnCode;
                return(false);
            }

            // Get the name of the COM typelib.
            typeLibName = cmdLine.GetNextArg();

            // Go through the list of options.
            while ((opt = cmdLine.GetNextOption()) != null)
            {
                // Determine which option was specified.
                if (opt.Name.Equals("out"))
                {
                    FileInfo fi  = new FileInfo(opt.Value);
                    string   dir = fi.Directory.FullName;
                    if (dir != null && dir.Length > 0)
                    {
                        options.outputDirectory = dir;
                    }

                    options.outputName = fi.Name;
                }
                else if (opt.Name.Equals("publickey"))
                {
                    if (options.keyPair != null || options.publicKey != null)
                    {
                        PrintLogo();
                        WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_TooManyKeys));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    // Read data from binary file into byte array.
                    byte[] aData;
                    try {
                        FileStream fs      = new FileStream(opt.Value, FileMode.Open, FileAccess.Read, FileShare.Read);
                        int        iLength = (int)fs.Length;
                        aData = new byte[iLength];
                        fs.Read(aData, 0, iLength);
                        fs.Close();
                    }
                    catch (Exception e) {
                        PrintLogo();
                        WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_ErrorWhileOpeningFile, opt.Value), e);
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    options.publicKey = aData;
                }
                else if (opt.Name.Equals("keyfile"))
                {
                    if (options.keyPair != null || options.publicKey != null)
                    {
                        PrintLogo();
                        WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_TooManyKeys));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    // Read data from binary file into byte array.
                    byte[] aData;
                    try {
                        FileStream fs      = new FileStream(opt.Value, FileMode.Open, FileAccess.Read);
                        int        iLength = (int)fs.Length;
                        aData = new byte[iLength];
                        fs.Read(aData, 0, iLength);
                        fs.Close();
                    }
                    catch (Exception e) {
                        PrintLogo();
                        WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_ErrorWhileOpeningFile, opt.Value), e);
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    options.keyFile = opt.Value;
                    options.keyPair = new StrongNameKeyPair(aData);
                }
                else if (opt.Name.Equals("keycontainer"))
                {
                    if (options.keyPair != null)
                    {
                        PrintLogo();
                        WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_TooManyKeys));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    options.keyContainer = opt.Value;
                    options.keyPair      = new StrongNameKeyPair(opt.Value);
                }
                else if (opt.Name.Equals("source"))
                {
                    options.genSources = true;
                }
                else if (opt.Name.Equals("delaysign"))
                {
                    options.delaySign = true;
                }
                else if (opt.Name.Equals("nologo"))
                {
                    options.noLogo = true;
                }
                else if (opt.Name.Equals("silent"))
                {
                    options.silentMode = true;
                }
                else if (opt.Name.Equals("rcw"))
                {
                    // Store away for later processing
                    rcwOptions.Add(opt.Value);
                }
                else if (opt.Name.Equals("verbose"))
                {
                    options.verboseMode = true;
                }
                else if (opt.Name.Equals("?") || opt.Name.Equals("help"))
                {
                    PrintUsage();
                    ReturnCode = SuccessReturnCode;
                    return(false);
                }
                else
                {
                    PrintLogo();
                    WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_InvalidOption));
                    ReturnCode = ErrorReturnCode;
                    return(false);
                }
            }

            // Validate that the typelib name has been specified.
            if (typeLibName == null)
            {
                PrintLogo();
                WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_NoInputFile));
                ReturnCode = ErrorReturnCode;
                return(false);
            }

            // Gather information needed for strong naming the assembly (if
            // the user desires this).
            if ((options.keyPair != null) && (options.publicKey == null))
            {
                try {
                    options.publicKey = options.keyPair.PublicKey;
                }
                catch (Exception) {
                    PrintLogo();
                    WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_InvalidStrongName));
                    ReturnCode = ErrorReturnCode;
                    return(false);
                }
            }

            if (options.delaySign && options.keyPair == null && options.keyContainer == null)
            {
                PrintLogo();
                WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_DelaySignError));
                ReturnCode = ErrorReturnCode;
                return(false);
            }

            if (!File.Exists(typeLibName))
            {
                FileInfo file = new FileInfo(typeLibName);
                PrintLogo();
                WriteErrorMsg(AxImpSR.GetString(AxImpSR.Err_FileNotExists, file.FullName));
                ReturnCode = ErrorReturnCode;
                return(false);
            }

            return(true);
        }
コード例 #9
0
 internal static void WriteErrorMsg(String strErrorMsg)
 {
     Console.Error.WriteLine(AxImpSR.GetString(AxImpSR.AxImpError, strErrorMsg));
 }
コード例 #10
0
        private static void PrintUsage()
        {
            PrintLogo();

            Console.WriteLine(AxImpSR.GetString(AxImpSR.Usage, Environment.Version));
        }