Exemplo n.º 1
0
        private static bool ParseArguments(String [] aArgs, ref TlbImpOptions Options, ref int ReturnCode)
        {
            CommandLine cmdLine;
            Option      opt;
            bool        delaysign = false;

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

            // Parse the command line arguments using the command line argument parser.
            try
            {
                cmdLine = new CommandLine(aArgs, new String[] { "*out", "*publickey", "*keyfile", "*keycontainer", "delaysign", "*reference",
                                                                "unsafe", "nologo", "silent", "verbose", "strictref", "primary", "*namespace",
                                                                "*asmversion", "sysarray", "*transform", "?", "help" });
            }
            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.
            Options.m_strTypeLibName = cmdLine.GetNextArg();

            // Go through the list of options.
            while ((opt = cmdLine.GetNextOption()) != null)
            {
                // Determine which option was specified.
                if (opt.Name.Equals("out"))
                {
                    Options.m_strAssemblyName = opt.Value;
                }
                else if (opt.Name.Equals("namespace"))
                {
                    Options.m_strAssemblyNamespace = opt.Value;
                }
                else if (opt.Name.Equals("asmversion"))
                {
                    try
                    {
                        Options.m_AssemblyVersion = new Version(opt.Value);
                    }
                    catch (Exception e)
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_InvalidVersion"), e);
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                }
                else if (opt.Name.Equals("reference"))
                {
                    Assembly asm         = null;
                    String   AsmFileName = opt.Value;

                    // Call SearchPath to find the full path of the referenced assembly.
                    StringBuilder sb = new StringBuilder(MAX_PATH + 1);
                    if (SearchPath(null, AsmFileName, null, sb.Capacity + 1, sb, null) == 0)
                    {
                        WriteErrorMsg(Resource.FormatString("Err_RefAssemblyNotFound", AsmFileName));
                        return(false);
                    }
                    AsmFileName = sb.ToString();

                    try
                    {
                        // Load the assembly.
                        asm = Assembly.LoadFrom(AsmFileName);

                        // Retrieve the GUID and add the assembly to the hashtable of referenced assemblies.
                        Guid TypeLibId = GetTypeLibIdForAssembly(asm);

                        // Add the assembly to the list of referenced assemblies.
                        Options.m_AssemblyRefList.Add(TypeLibId, asm);
                    }
                    catch (BadImageFormatException)
                    {
                        WriteErrorMsg(Resource.FormatString("Err_RefAssemblyInvalid", AsmFileName));
                        return(false);
                    }
                    catch (FileNotFoundException)
                    {
                        WriteErrorMsg(Resource.FormatString("Err_RefAssemblyNotFound", AsmFileName));
                        return(false);
                    }
                    catch (ApplicationException e)
                    {
                        WriteErrorMsg(null, e);
                        return(false);
                    }
                }
                else if (opt.Name.Equals("delaysign"))
                {
                    delaysign = true;
                }
                else if (opt.Name.Equals("publickey"))
                {
                    if (Options.m_sKeyPair != null || Options.m_aPublicKey != null)
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_TooManyKeys"));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    // Read data from binary file into byte array.
                    byte[]     aData;
                    FileStream fs = null;
                    try
                    {
                        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);
                    }
                    catch (Exception e)
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_ErrorWhileOpenningFile", opt.Value), e);
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                    Options.m_aPublicKey = aData;
                }
                else if (opt.Name.Equals("keyfile"))
                {
                    if (Options.m_sKeyPair != null || Options.m_aPublicKey != null)
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_TooManyKeys"));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    // Read data from binary file into byte array.
                    byte[]     aData;
                    FileStream fs = null;
                    try
                    {
                        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);
                    }
                    catch (Exception e)
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_ErrorWhileOpenningFile", opt.Value), e);
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                    Options.m_sKeyPair = new StrongNameKeyPair(aData);
                }
                else if (opt.Name.Equals("keycontainer"))
                {
                    if (Options.m_sKeyPair != null)
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_TooManyKeys"));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                    Options.m_sKeyPair = new StrongNameKeyPair(opt.Value);
                }
                else if (opt.Name.Equals("unsafe"))
                {
                    Options.m_flags |= TypeLibImporterFlags.UnsafeInterfaces;
                }
                else if (opt.Name.Equals("primary"))
                {
                    Options.m_flags |= TypeLibImporterFlags.PrimaryInteropAssembly;
                }
                else if (opt.Name.Equals("sysarray"))
                {
                    Options.m_flags |= TypeLibImporterFlags.SafeArrayAsSystemArray;
                }
                else if (opt.Name.Equals("nologo"))
                {
                    Options.m_bNoLogo = true;
                }
                else if (opt.Name.Equals("silent"))
                {
                    Options.m_bSilentMode = true;
                }
                else if (opt.Name.Equals("verbose"))
                {
                    Options.m_bVerboseMode = true;
                }
                else if (opt.Name.Equals("strictref"))
                {
                    Options.m_bStrictRef = true;
                }
                else if (opt.Name.Equals("transform"))
                {
                    if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "dispret")
                    {
                        Options.m_flags |= TypeLibImporterFlags.TransformDispRetVals;
                    }
                    else
                    {
                        PrintLogo();
                        WriteErrorMsg(Resource.FormatString("Err_InvalidTransform", opt.Value));
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }
                }
                else if (opt.Name.Equals("?") || opt.Name.Equals("help"))
                {
                    PrintUsage();
                    ReturnCode = SuccessReturnCode;
                    return(false);
                }
                else
                {
                    PrintLogo();
                    WriteErrorMsg(Resource.FormatString("Err_InvalidOption"));
                    ReturnCode = ErrorReturnCode;
                    return(false);
                }
            }

            // Validate that the typelib name has been specified.
            if (Options.m_strTypeLibName == null)
            {
                PrintLogo();
                WriteErrorMsg(Resource.FormatString("Err_NoInputFile"));
                ReturnCode = ErrorReturnCode;
                return(false);
            }

            // Gather information needed for strong naming the assembly (if
            // the user desires this).
            if ((Options.m_sKeyPair != null) && (Options.m_aPublicKey == null))
            {
                try {
                    Options.m_aPublicKey = Options.m_sKeyPair.PublicKey;
                } catch (Exception) {
                    PrintLogo();
                    WriteErrorMsg(Resource.FormatString("Err_InvalidStrongName"));
                    ReturnCode = ErrorReturnCode;
                    return(false);
                }
            }
            if (delaysign && Options.m_sKeyPair != null)
            {
                Options.m_sKeyPair = null;
            }

            // To be able to generate a PIA, we must also be strong naming the assembly.
            if ((Options.m_flags & TypeLibImporterFlags.PrimaryInteropAssembly) != 0)
            {
                if (Options.m_aPublicKey == null && Options.m_sKeyPair == null)
                {
                    PrintLogo();
                    WriteErrorMsg(Resource.FormatString("Err_PIAMustBeStrongNamed"));
                    ReturnCode = ErrorReturnCode;
                    return(false);
                }
            }


            return(true);
        }
Exemplo n.º 2
0
        private static bool ParseArguments(String [] aArgs, ref TlbImpOptions Options, ref int ReturnCode)
        {
            CommandLine cmdLine;
            Option      opt;
            bool        delaysign = false;

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

            // Parse the command line arguments using the command line argument parser.
            cmdLine = new CommandLine(aArgs, new String[] { "*out", "*publickey", "*keyfile", "*keycontainer", "delaysign", "*reference",
                                                            "unsafe", "nologo", "silent", "verbose", "+strictref", "primary", "*namespace",
                                                            "*asmversion", "sysarray", "*transform", "?", "help", "*tlbreference",
                                                            "noclassmembers", "*machine", "*silence", "*product", "*productversion",
                                                            "*company", "*copyright", "*trademark", "v2", "preservesig", "removeenumprefix" });

            // 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.
            Options.m_strTypeLibName = cmdLine.GetNextArg();

            // Go through the list of options.
            while ((opt = cmdLine.GetNextOption()) != null)
            {
                // Determine which option was specified.
                if (opt.Name.Equals("out"))
                {
                    Options.m_strAssemblyName = opt.Value;
                }
                else if (opt.Name.Equals("namespace"))
                {
                    Options.m_strAssemblyNamespace = opt.Value;
                }
                else if (opt.Name.Equals("asmversion"))
                {
                    try
                    {
                        Options.m_AssemblyVersion = new Version(opt.Value);
                    }
                    catch (Exception)
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_InvalidVersion"), ErrorCode.Err_InvalidVersion, true);
                    }
                }
                else if (opt.Name.Equals("reference"))
                {
                    String FullPath = null;

                    FullPath = GetFullPath(opt.Value, false);

                    if (FullPath == null)
                    {
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }

                    if (Options.m_strAssemblyRefList == null)
                    {
                        Options.m_strAssemblyRefList = FullPath;
                    }
                    else
                    {
                        Options.m_strAssemblyRefList = Options.m_strAssemblyRefList + ";" + FullPath;
                    }
                }
                else if (opt.Name.Equals("tlbreference"))
                {
                    String FullPath = null;

                    FullPath = GetFullPath(opt.Value, false);
                    if (FullPath == null)
                    {
                        ReturnCode = ErrorReturnCode;
                        return(false);
                    }

                    if (Options.m_strTypeLibRefList == null)
                    {
                        Options.m_strTypeLibRefList = FullPath;
                    }
                    else
                    {
                        Options.m_strTypeLibRefList = Options.m_strTypeLibRefList + ";" + FullPath;
                    }
                }
                else if (opt.Name.Equals("delaysign"))
                {
                    delaysign = true;
                }
                else if (opt.Name.Equals("publickey"))
                {
                    if (Options.m_sKeyPair != null || Options.m_aPublicKey != null)
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_TooManyKeys"), ErrorCode.Err_TooManyKeys, true);
                    }
                    // Read data from binary file into byte array.
                    byte[]     aData;
                    FileStream fs = null;
                    try
                    {
                        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);
                    }
                    catch (Exception ex)
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_ErrorWhileOpenningFile", new object[] { opt.Value, ex.GetType().ToString(), ex.Message }), ErrorCode.Err_ErrorWhileOpenningFile, true);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                    Options.m_aPublicKey = aData;
                }
                else if (opt.Name.Equals("keyfile"))
                {
                    if (Options.m_sKeyPair != null || Options.m_aPublicKey != null)
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_TooManyKeys"), ErrorCode.Err_TooManyKeys, true);
                    }

                    // Read data from binary file into byte array.
                    byte[]     aData;
                    FileStream fs = null;
                    try
                    {
                        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);
                    }
                    catch (Exception ex)
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_ErrorWhileOpenningFile", new object[] { opt.Value, ex.GetType().ToString(), ex.Message }), ErrorCode.Err_ErrorWhileOpenningFile, true);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                    Options.m_sKeyPair = new StrongNameKeyPair(aData);
                }
                else if (opt.Name.Equals("keycontainer"))
                {
                    if ((Options.m_sKeyPair != null) || (Options.m_aPublicKey != null))
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_TooManyKeys"), ErrorCode.Err_TooManyKeys, true);
                    }
                    Options.m_sKeyPair = new StrongNameKeyPair(opt.Value);
                }
                else if (opt.Name.Equals("unsafe"))
                {
                    Options.m_flags |= TypeLibImporterFlags.UnsafeInterfaces;
                }
                else if (opt.Name.Equals("primary"))
                {
                    Options.m_flags |= TypeLibImporterFlags.PrimaryInteropAssembly;
                }
                else if (opt.Name.Equals("sysarray"))
                {
                    Options.m_flags |= TypeLibImporterFlags.SafeArrayAsSystemArray;
                }
                else if (opt.Name.Equals("nologo"))
                {
                    Options.m_bNoLogo = true;
                }
                else if (opt.Name.Equals("silent"))
                {
                    Output.SetSilent(true);
                    Options.m_bSilentMode = true;
                }
                else if (opt.Name.Equals("silence"))
                {
                    int warningNumber = int.Parse(opt.Value, System.Globalization.NumberStyles.HexNumber);
                    Output.Silence(warningNumber);
                    Options.m_silenceList.Add(warningNumber);
                }
                else if (opt.Name.Equals("verbose"))
                {
                    Options.m_bVerboseMode = true;
                }
                else if (opt.Name.Equals("noclassmembers"))
                {
                    Options.m_flags |= TypeLibImporterFlags.PreventClassMembers;
                }
                else if (opt.Name.Equals("strictref"))
                {
                    if (opt.Value != null)
                    {
                        if (String.Compare(opt.Value, "nopia", true) == 0)
                        {
                            Options.m_bStrictRefNoPia = true;
                        }
                        else
                        {
                            throw new TlbImpGeneralException(Resource.FormatString("Err_UnknownStrictRefOpt", opt.Value), ErrorCode.Err_UnknownStrictRefOpt, true);
                        }
                    }
                    else
                    {
                        Options.m_bStrictRef = true;
                    }
                }
                else if (opt.Name.Equals("transform"))
                {
                    if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "dispret")
                    {
                        Options.m_flags |= TypeLibImporterFlags.TransformDispRetVals;
                    }
                    else if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "serializablevalueclasses")
                    {
                        Options.m_flags |= TypeLibImporterFlags.SerializableValueClasses;
                    }
                    else
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_InvalidTransform", opt.Value), ErrorCode.Err_InvalidTransform, true);
                    }
                }
                else if (opt.Name.Equals("machine"))
                {
                    if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "itanium")
                    {
                        Options.m_flags |= TypeLibImporterFlags.ImportAsItanium;
                    }
                    else if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "x64")
                    {
                        Options.m_flags |= TypeLibImporterFlags.ImportAsX64;
                    }
                    else if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "x86")
                    {
                        Options.m_flags |= TypeLibImporterFlags.ImportAsX86;
                    }
                    else if (opt.Value.ToLower(CultureInfo.InvariantCulture) == "agnostic")
                    {
                        Options.m_flags |= TypeLibImporterFlags.ImportAsAgnostic;
                    }
                    else
                    {
                        throw new TlbImpGeneralException(Resource.FormatString("Err_InvalidMachine", opt.Value), ErrorCode.Err_InvalidMachine, true);
                    }
                }
                else if (opt.Name.Equals("product"))
                {
                    Options.m_strProduct = opt.Value;
                }
                else if (opt.Name.Equals("productversion"))
                {
                    Options.m_strProductVersion = opt.Value;
                }
                else if (opt.Name.Equals("company"))
                {
                    Options.m_strCompany = opt.Value;
                }
                else if (opt.Name.Equals("copyright"))
                {
                    Options.m_strCopyright = opt.Value;
                }
                else if (opt.Name.Equals("trademark"))
                {
                    Options.m_strTrademark = opt.Value;
                }
                else if (opt.Name.Equals("?") || opt.Name.Equals("help"))
                {
                    PrintUsage();
                    ReturnCode = SuccessReturnCode;
                    return(false);
                }
                else if (opt.Name.Equals("v2"))
                {
                    Options.m_isVersion2 = true;
                }
                else if (opt.Name.Equals("preservesig"))
                {
                    Options.m_isPreserveSig = true;
                }
                else if (opt.Name.Equals("removeenumprefix"))
                {
                    Options.m_isRemoveEnumPrefix = true;
                }
            }

            // Validate that the typelib name has been specified.
            if (Options.m_strTypeLibName == null)
            {
                throw new TlbImpGeneralException(Resource.FormatString("Err_NoInputFile"), ErrorCode.Err_NoInputFile, true);
            }

            // Gather information needed for strong naming the assembly (if
            // the user desires this).
            if ((Options.m_sKeyPair != null) && (Options.m_aPublicKey == null))
            {
                try
                {
                    Options.m_aPublicKey = Options.m_sKeyPair.PublicKey;
                }
                catch
                {
                    throw new TlbImpGeneralException(Resource.FormatString("Err_InvalidStrongName"), ErrorCode.Err_InvalidStrongName, true);
                }
            }

            if (delaysign && Options.m_sKeyPair != null)
            {
                Options.m_sKeyPair = null;
            }

            // To be able to generate a PIA, we must also be strong naming the assembly.
            if ((Options.m_flags & TypeLibImporterFlags.PrimaryInteropAssembly) != 0)
            {
                if (Options.m_aPublicKey == null && Options.m_sKeyPair == null)
                {
                    throw new TlbImpGeneralException(Resource.FormatString("Err_PIAMustBeStrongNamed"), ErrorCode.Err_PIAMustBeStrongNamed, true);
                }
            }

            return(true);
        }