Exemple #1
0
        public void ReferenceFiles()
        {
            var t = new ResolveComReference.TlbImp();
            Assert.Null(t.ReferenceFiles); // "ReferenceFiles should be null by default"

            t.ReferenceFiles = new string[] { "File1.dll", "File2.dll" };
            CommandLine.ValidateHasParameter(t, "/reference:File1.dll", false /* no response file */);
            CommandLine.ValidateHasParameter(t, "/reference:File2.dll", false /* no response file */);
        }
Exemple #2
0
        public void Machine()
        {
            var t = new ResolveComReference.TlbImp();
            Assert.Null(t.Machine); // "Machine should be null by default"

            t.Machine = "Agnostic";
            Assert.Equal("Agnostic", t.Machine); // "New TypeLibName value should be set"
            CommandLine.ValidateHasParameter(t, "/machine:Agnostic", false /* no response file */);
        }
Exemple #3
0
        public void TypeLibNameWithSpaces()
        {
            var t = new ResolveComReference.TlbImp();
            string testParameterValue = @"c:\Program Files\Interop.Foo.dll";

            Assert.Null(t.TypeLibName); // "TypeLibName should be null by default"

            t.TypeLibName = testParameterValue;
            Assert.Equal(testParameterValue, t.TypeLibName); // "New TypeLibName value should be set"
            CommandLine.ValidateHasParameter(t, testParameterValue, false /* no response file */);
        }
Exemple #4
0
        public void TypeLibName()
        {
            var t = new ResolveComReference.TlbImp();
            string testParameterValue = "Interop.Foo.dll";

            Assert.IsNull(t.TypeLibName, "TypeLibName should be null by default");

            t.TypeLibName = testParameterValue;
            Assert.AreEqual(testParameterValue, t.TypeLibName, "New TypeLibName value should be set");
            CommandLine.ValidateHasParameter(t, testParameterValue, false /* no response file */);
        }
Exemple #5
0
        public void AssemblyNamespace()
        {
            var t = new ResolveComReference.TlbImp();
            string testParameterValue = "Microsoft.Build.Foo";

            Assert.Null(t.AssemblyNamespace); // "AssemblyNamespace should be null by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/namespace:", false /* no response file */);

            t.AssemblyNamespace = testParameterValue;
            Assert.Equal(testParameterValue, t.AssemblyNamespace); // "New AssemblyNamespace value should be set"
            CommandLine.ValidateHasParameter(t, @"/namespace:" + testParameterValue, false /* no response file */);
        }
        public void KeyContainer()
        {
            var t = new ResolveComReference.TlbImp();
            t.TypeLibName = "FakeTlb.tlb";
            string badParameterValue = "badKeyContainer";
            string goodParameterValue = "myKeyContainer";

            try
            {
                t.ToolPath = Path.GetTempPath();

                Assert.Null(t.KeyContainer); // "KeyContainer should be null by default"
                CommandLine.ValidateNoParameterStartsWith(t, @"/keycontainer:", false /* no response file */);

                t.KeyContainer = badParameterValue;
                Assert.Equal(badParameterValue, t.KeyContainer); // "New KeyContainer value should be set"
                CommandLine.ValidateHasParameter(t, @"/keycontainer:" + badParameterValue, false /* no response file */);
                Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.StrongNameUtils.NoKeyPairInContainer", t.KeyContainer);
                //ensure the key does not exist in the CSP
                StrongNameHelpers.StrongNameKeyDelete(goodParameterValue);

                IntPtr publicKeyBlob = IntPtr.Zero;
                int publicKeyBlobSize = 0;

                //add key to CSP
                if (StrongNameHelpers.StrongNameKeyGen(goodParameterValue, 1 /* leave key registered */, out publicKeyBlob, out publicKeyBlobSize) && publicKeyBlob != IntPtr.Zero)
                {
                    StrongNameHelpers.StrongNameFreeBuffer(publicKeyBlob);

                    t.KeyContainer = goodParameterValue;
                    Assert.Equal(goodParameterValue, t.KeyContainer); // "New KeyContainer value should be set"
                    CommandLine.ValidateHasParameter(t, @"/keycontainer:" + goodParameterValue, false /* no response file */);
                    Utilities.ExecuteTaskAndVerifyLogDoesNotContainErrorFromResource(t, "AxTlbBaseTask.StrongNameUtils.NoKeyPairInContainer", t.KeyContainer);
                }
                else
                {
                    Assert.True(false, "Key container could not be created.");
                }
            }
            finally
            {
                //remove key from CSP
                StrongNameHelpers.StrongNameKeyDelete(goodParameterValue);

                // get rid of the generated temp file
                if (goodParameterValue != null)
                {
                    File.Delete(goodParameterValue);
                }
            }
        }
Exemple #7
0
        /*
         * Method:  GenerateWrapper
         * 
         * Generates a wrapper for this reference.
         */
        internal bool GenerateWrapper(out ComReferenceWrapperInfo wrapperInfo)
        {
            wrapperInfo = null;

            string rootNamespace = ReferenceInfo.typeLibName;
            string wrapperPath = GetWrapperPath();
            bool generateWrapperSucceeded = true;

            if (ExecuteAsTool)
            {
                // delegate generation of the assembly to an instance of the TlbImp ToolTask. MUST
                // HAVE SET SDKTOOLSPATH TO THE TARGET SDK TO WORK
                var tlbImp = new ResolveComReference.TlbImp();

                tlbImp.BuildEngine = BuildEngine;
                tlbImp.EnvironmentVariables = EnvironmentVariables;
                tlbImp.DelaySign = DelaySign;
                tlbImp.KeyContainer = KeyContainer;
                tlbImp.KeyFile = KeyFile;
                tlbImp.OutputAssembly = wrapperPath;
                tlbImp.ToolPath = ToolPath;
                tlbImp.TypeLibName = ReferenceInfo.fullTypeLibPath;
                tlbImp.AssemblyNamespace = rootNamespace;
                tlbImp.AssemblyVersion = null;
                tlbImp.PreventClassMembers = _noClassMembers;
                tlbImp.SafeArrayAsSystemArray = true;
                tlbImp.Silent = Silent;
                tlbImp.Transform = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals;
                if (_referenceFiles != null)
                {
                    // Issue is that there may be reference dependencies that need to be passed in. It is possible
                    // that the set of references will also contain the file that is meant to be written here (when reference resolution
                    // found the file in the output folder). We need to filter out this case.
                    var fullPathToOutput = Path.GetFullPath(wrapperPath); // Current directory is the directory of the project file.
                    tlbImp.ReferenceFiles = _referenceFiles.Where(rf => String.Compare(fullPathToOutput, rf, StringComparison.OrdinalIgnoreCase) != 0).ToArray();
                }

                switch (_targetProcessorArchitecture)
                {
                    case UtilitiesProcessorArchitecture.MSIL:
                        tlbImp.Machine = "Agnostic";
                        break;
                    case UtilitiesProcessorArchitecture.AMD64:
                        tlbImp.Machine = "X64";
                        break;
                    case UtilitiesProcessorArchitecture.IA64:
                        tlbImp.Machine = "Itanium";
                        break;
                    case UtilitiesProcessorArchitecture.X86:
                        tlbImp.Machine = "X86";
                        break;
                    case UtilitiesProcessorArchitecture.ARM:
                        tlbImp.Machine = "ARM";
                        break;
                    case null:
                        break;
                    default:
                        // Transmit the flag directly from the .targets files and rely on tlbimp.exe to produce a good error message.
                        tlbImp.Machine = _targetProcessorArchitecture;
                        break;
                }

                generateWrapperSucceeded = tlbImp.Execute();

                // store the wrapper info...
                wrapperInfo = new ComReferenceWrapperInfo();
                wrapperInfo.path = (HasTemporaryWrapper) ? null : wrapperPath;
                // Changed to ReflectionOnlyLoadFrom, related to bug:
                //  RCR: Bad COM-interop assemblies being generated when using 64-bit MSBuild to build a project that is targeting 32-bit platform
                // The original call to UnsafeLoadFrom loads the assembly in preparation for execution. If the assembly is x86 and this is x64 msbuild.exe then we
                // have problems (UnsafeLoadFrom will fail). We only use this assembly for reference resolution so we don't need to be ready to execute the code.
                //
                // Its actually not clear to me that we even need to load the assembly at all. Reference resoluton is only used in the !ExecuteAsTool which is not
                // where we are right now.
                //
                // If we really do need to load it then:
                //
                //  wrapperInfo.assembly = Assembly.ReflectionOnlyLoadFrom(wrapperPath);
            }
            else
            {
                // use framework classes in-proc to generate the assembly
                TypeLibConverter converter = new TypeLibConverter();

                AssemblyBuilder assemblyBuilder = null;
                StrongNameKeyPair keyPair;
                byte[] publicKey;

                GetAndValidateStrongNameKey(out keyPair, out publicKey);

                try
                {
                    TypeLibImporterFlags flags = TypeLibImporterFlags.SafeArrayAsSystemArray | TypeLibImporterFlags.TransformDispRetVals;

                    if (_noClassMembers)
                    {
                        flags |= TypeLibImporterFlags.PreventClassMembers;
                    }

                    switch (_targetProcessorArchitecture)
                    {
                        case UtilitiesProcessorArchitecture.MSIL:
                            flags |= TypeLibImporterFlags.ImportAsAgnostic;
                            break;
                        case UtilitiesProcessorArchitecture.AMD64:
                            flags |= TypeLibImporterFlags.ImportAsX64;
                            break;
                        case UtilitiesProcessorArchitecture.IA64:
                            flags |= TypeLibImporterFlags.ImportAsItanium;
                            break;
                        case UtilitiesProcessorArchitecture.X86:
                            flags |= TypeLibImporterFlags.ImportAsX86;
                            break;
                        case UtilitiesProcessorArchitecture.ARM:
                            flags |= TypeLibImporterFlags.ImportAsArm;
                            break;

                        default:
                            // Let the type importer decide.
                            break;
                    }

                    // Start the conversion process. We'll get callbacks on ITypeLibImporterNotifySink to resolve dependent refs.
                    assemblyBuilder = converter.ConvertTypeLibToAssembly(ReferenceInfo.typeLibPointer, wrapperPath,
                        flags, this, publicKey, keyPair, rootNamespace, null);
                }
                catch (COMException ex)
                {
                    if (!Silent)
                    {
                        Log.LogWarningWithCodeFromResources("ResolveComReference.ErrorCreatingWrapperAssembly", ItemName, ex.Message);
                    }

                    throw new ComReferenceResolutionException(ex);
                }

                // if we're done, and this is not a temporary wrapper, write it out to disk
                if (!HasTemporaryWrapper)
                {
                    WriteWrapperToDisk(assemblyBuilder, wrapperPath);
                }

                // store the wrapper info...
                wrapperInfo = new ComReferenceWrapperInfo();
                wrapperInfo.path = (HasTemporaryWrapper) ? null : wrapperPath;
                wrapperInfo.assembly = assemblyBuilder;
            }

            // ...and we're done!
            return generateWrapperSucceeded;
        }
Exemple #8
0
        public void Transform()
        {
            var t = new ResolveComReference.TlbImp();

            var dispRet = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals;
            var serialize = ResolveComReference.TlbImpTransformFlags.SerializableValueClasses;
            var both = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals | ResolveComReference.TlbImpTransformFlags.SerializableValueClasses;

            t.TypeLibName = "SomeRandomControl.tlb";
            t.ToolPath = Path.GetTempPath();

            Assert.Equal(ResolveComReference.TlbImpTransformFlags.None, t.Transform); // "Transform should be TlbImpTransformFlags.None by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/transform:", false /* no response file */);

            t.Transform = dispRet;
            Assert.Equal(dispRet, t.Transform); // "New Transform value should be set"
            CommandLine.ValidateHasParameter(t, @"/transform:DispRet", false /* no response file */);

            t.Transform = serialize;
            Assert.Equal(serialize, t.Transform); // "New Transform value should be set"
            CommandLine.ValidateHasParameter(t, @"/transform:SerializableValueClasses", false /* no response file */);

            t.Transform = both;
            Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "TlbImp.InvalidTransformParameter", t.Transform);
        }
Exemple #9
0
        public void Silent()
        {
            var t = new ResolveComReference.TlbImp();

            Assert.False(t.Silent); // "Silent should be false by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/silent", false /* no response file */);

            t.Silent = true;
            Assert.True(t.Silent); // "Silent should be true"
            CommandLine.ValidateHasParameter(t, @"/silent", false /* no response file */);
        }
Exemple #10
0
        public void SafeArrayAsSystemArray()
        {
            var t = new ResolveComReference.TlbImp();

            Assert.False(t.SafeArrayAsSystemArray); // "SafeArrayAsSystemArray should be false by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/sysarray", false /* no response file */);

            t.SafeArrayAsSystemArray = true;
            Assert.True(t.SafeArrayAsSystemArray); // "SafeArrayAsSystemArray should be true"
            CommandLine.ValidateHasParameter(t, @"/sysarray", false /* no response file */);
        }
Exemple #11
0
        public void PreventClassMembers()
        {
            var t = new ResolveComReference.TlbImp();

            Assert.False(t.PreventClassMembers); // "PreventClassMembers should be false by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/noclassmembers", false /* no response file */);

            t.PreventClassMembers = true;
            Assert.True(t.PreventClassMembers); // "PreventClassMembers should be true"
            CommandLine.ValidateHasParameter(t, @"/noclassmembers", false /* no response file */);
        }
 internal bool GenerateWrapper(out ComReferenceWrapperInfo wrapperInfo)
 {
     wrapperInfo = null;
     string typeLibName = this.ReferenceInfo.typeLibName;
     string wrapperPath = base.GetWrapperPath();
     StrongNameKeyPair keyPair = null;
     byte[] publicKey = null;
     StrongNameUtils.GetStrongNameKey(base.Log, base.KeyFile, base.KeyContainer, out keyPair, out publicKey);
     if (base.DelaySign)
     {
         keyPair = null;
         if (publicKey == null)
         {
             base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "StrongNameUtils.NoPublicKeySpecified", new object[0]);
             throw new StrongNameException();
         }
     }
     else
     {
         publicKey = null;
         if (keyPair == null)
         {
             if ((base.KeyContainer != null) && (base.KeyContainer.Length > 0))
             {
                 base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInContainer", new object[] { base.KeyContainer });
                 throw new StrongNameException();
             }
             if ((base.KeyFile != null) && (base.KeyFile.Length > 0))
             {
                 base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInFile", new object[] { base.KeyFile });
                 throw new StrongNameException();
             }
         }
     }
     bool flag = true;
     if (!base.ExecuteAsTool)
     {
         TypeLibConverter converter = new TypeLibConverter();
         AssemblyBuilder assemblyBuilder = null;
         try
         {
             TypeLibImporterFlags flags = TypeLibImporterFlags.TransformDispRetVals | TypeLibImporterFlags.SafeArrayAsSystemArray;
             if (this.noClassMembers)
             {
                 flags |= TypeLibImporterFlags.PreventClassMembers;
             }
             string str4 = this.targetProcessorArchitecture;
             if (str4 != null)
             {
                 if (!(str4 == "MSIL"))
                 {
                     if (str4 == "AMD64")
                     {
                         goto Label_0323;
                     }
                     if (str4 == "IA64")
                     {
                         goto Label_032F;
                     }
                     if (str4 == "x86")
                     {
                         goto Label_033B;
                     }
                 }
                 else
                 {
                     flags |= TypeLibImporterFlags.ImportAsAgnostic;
                 }
             }
             goto Label_0345;
         Label_0323:
             flags |= TypeLibImporterFlags.ImportAsX64;
             goto Label_0345;
         Label_032F:
             flags |= TypeLibImporterFlags.ImportAsItanium;
             goto Label_0345;
         Label_033B:
             flags |= TypeLibImporterFlags.ImportAsX86;
         Label_0345:
             assemblyBuilder = converter.ConvertTypeLibToAssembly(this.ReferenceInfo.typeLibPointer, wrapperPath, flags, this, publicKey, keyPair, typeLibName, null);
         }
         catch (COMException exception)
         {
             base.Log.LogWarningWithCodeFromResources("ResolveComReference.ErrorCreatingWrapperAssembly", new object[] { this.ItemName, exception.Message });
             throw new ComReferenceResolutionException(exception);
         }
         if (!this.HasTemporaryWrapper)
         {
             this.WriteWrapperToDisk(assemblyBuilder, wrapperPath);
         }
         wrapperInfo = new ComReferenceWrapperInfo();
         wrapperInfo.path = this.HasTemporaryWrapper ? null : wrapperPath;
         wrapperInfo.assembly = assemblyBuilder;
         return flag;
     }
     ResolveComReference.TlbImp imp = new ResolveComReference.TlbImp {
         BuildEngine = base.BuildEngine,
         EnvironmentVariables = base.EnvironmentVariables,
         DelaySign = base.DelaySign,
         KeyContainer = base.KeyContainer,
         KeyFile = base.KeyFile,
         OutputAssembly = wrapperPath,
         ToolPath = base.ToolPath,
         TypeLibName = this.ReferenceInfo.typeLibPath,
         AssemblyNamespace = typeLibName,
         AssemblyVersion = null,
         PreventClassMembers = this.noClassMembers,
         SafeArrayAsSystemArray = true,
         Transform = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals
     };
     if (this.referenceFiles != null)
     {
         string fullPathToOutput = Path.GetFullPath(wrapperPath);
         imp.ReferenceFiles = (from rf in this.referenceFiles
             where string.Compare(fullPathToOutput, rf, StringComparison.OrdinalIgnoreCase) != 0
             select rf).ToArray<string>();
     }
     string targetProcessorArchitecture = this.targetProcessorArchitecture;
     if (targetProcessorArchitecture != null)
     {
         if (!(targetProcessorArchitecture == "MSIL"))
         {
             if (targetProcessorArchitecture == "AMD64")
             {
                 imp.Machine = "X64";
             }
             else if (targetProcessorArchitecture == "IA64")
             {
                 imp.Machine = "Itanium";
             }
             else if (targetProcessorArchitecture == "x86")
             {
                 imp.Machine = "X86";
             }
             else
             {
                 imp.Machine = this.targetProcessorArchitecture;
             }
         }
         else
         {
             imp.Machine = "Agnostic";
         }
     }
     flag = imp.Execute();
     wrapperInfo = new ComReferenceWrapperInfo();
     wrapperInfo.path = this.HasTemporaryWrapper ? null : wrapperPath;
     return flag;
 }
Exemple #13
0
        public void Verbose()
        {
            var t = new ResolveComReference.TlbImp();

            Assert.False(t.Verbose); // "Verbose should be false by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/verbose", false /* no response file */);

            t.Verbose = true;
            Assert.True(t.Verbose); // "Verbose should be true"
            CommandLine.ValidateHasParameter(t, @"/verbose", false /* no response file */);
        }
        internal bool GenerateWrapper(out ComReferenceWrapperInfo wrapperInfo)
        {
            wrapperInfo = null;
            string            typeLibName = this.ReferenceInfo.typeLibName;
            string            wrapperPath = base.GetWrapperPath();
            StrongNameKeyPair keyPair     = null;

            byte[] publicKey = null;
            StrongNameUtils.GetStrongNameKey(base.Log, base.KeyFile, base.KeyContainer, out keyPair, out publicKey);
            if (base.DelaySign)
            {
                keyPair = null;
                if (publicKey == null)
                {
                    base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "StrongNameUtils.NoPublicKeySpecified", new object[0]);
                    throw new StrongNameException();
                }
            }
            else
            {
                publicKey = null;
                if (keyPair == null)
                {
                    if ((base.KeyContainer != null) && (base.KeyContainer.Length > 0))
                    {
                        base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInContainer", new object[] { base.KeyContainer });
                        throw new StrongNameException();
                    }
                    if ((base.KeyFile != null) && (base.KeyFile.Length > 0))
                    {
                        base.Log.LogErrorWithCodeFromResources(null, this.ReferenceInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.StrongNameUtils.NoKeyPairInFile", new object[] { base.KeyFile });
                        throw new StrongNameException();
                    }
                }
            }
            bool flag = true;

            if (!base.ExecuteAsTool)
            {
                TypeLibConverter converter       = new TypeLibConverter();
                AssemblyBuilder  assemblyBuilder = null;
                try
                {
                    TypeLibImporterFlags flags = TypeLibImporterFlags.TransformDispRetVals | TypeLibImporterFlags.SafeArrayAsSystemArray;
                    if (this.noClassMembers)
                    {
                        flags |= TypeLibImporterFlags.PreventClassMembers;
                    }
                    string str4 = this.targetProcessorArchitecture;
                    if (str4 != null)
                    {
                        if (!(str4 == "MSIL"))
                        {
                            if (str4 == "AMD64")
                            {
                                goto Label_0323;
                            }
                            if (str4 == "IA64")
                            {
                                goto Label_032F;
                            }
                            if (str4 == "x86")
                            {
                                goto Label_033B;
                            }
                        }
                        else
                        {
                            flags |= TypeLibImporterFlags.ImportAsAgnostic;
                        }
                    }
                    goto Label_0345;
Label_0323:
                    flags |= TypeLibImporterFlags.ImportAsX64;
                    goto Label_0345;
Label_032F:
                    flags |= TypeLibImporterFlags.ImportAsItanium;
                    goto Label_0345;
Label_033B:
                    flags |= TypeLibImporterFlags.ImportAsX86;
Label_0345:
                    assemblyBuilder = converter.ConvertTypeLibToAssembly(this.ReferenceInfo.typeLibPointer, wrapperPath, flags, this, publicKey, keyPair, typeLibName, null);
                }
                catch (COMException exception)
                {
                    base.Log.LogWarningWithCodeFromResources("ResolveComReference.ErrorCreatingWrapperAssembly", new object[] { this.ItemName, exception.Message });
                    throw new ComReferenceResolutionException(exception);
                }
                if (!this.HasTemporaryWrapper)
                {
                    this.WriteWrapperToDisk(assemblyBuilder, wrapperPath);
                }
                wrapperInfo          = new ComReferenceWrapperInfo();
                wrapperInfo.path     = this.HasTemporaryWrapper ? null : wrapperPath;
                wrapperInfo.assembly = assemblyBuilder;
                return(flag);
            }
            ResolveComReference.TlbImp imp = new ResolveComReference.TlbImp {
                BuildEngine          = base.BuildEngine,
                EnvironmentVariables = base.EnvironmentVariables,
                DelaySign            = base.DelaySign,
                KeyContainer         = base.KeyContainer,
                KeyFile                = base.KeyFile,
                OutputAssembly         = wrapperPath,
                ToolPath               = base.ToolPath,
                TypeLibName            = this.ReferenceInfo.typeLibPath,
                AssemblyNamespace      = typeLibName,
                AssemblyVersion        = null,
                PreventClassMembers    = this.noClassMembers,
                SafeArrayAsSystemArray = true,
                Transform              = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals
            };
            if (this.referenceFiles != null)
            {
                string fullPathToOutput = Path.GetFullPath(wrapperPath);
                imp.ReferenceFiles = (from rf in this.referenceFiles
                                      where string.Compare(fullPathToOutput, rf, StringComparison.OrdinalIgnoreCase) != 0
                                      select rf).ToArray <string>();
            }
            string targetProcessorArchitecture = this.targetProcessorArchitecture;

            if (targetProcessorArchitecture != null)
            {
                if (!(targetProcessorArchitecture == "MSIL"))
                {
                    if (targetProcessorArchitecture == "AMD64")
                    {
                        imp.Machine = "X64";
                    }
                    else if (targetProcessorArchitecture == "IA64")
                    {
                        imp.Machine = "Itanium";
                    }
                    else if (targetProcessorArchitecture == "x86")
                    {
                        imp.Machine = "X86";
                    }
                    else
                    {
                        imp.Machine = this.targetProcessorArchitecture;
                    }
                }
                else
                {
                    imp.Machine = "Agnostic";
                }
            }
            flag             = imp.Execute();
            wrapperInfo      = new ComReferenceWrapperInfo();
            wrapperInfo.path = this.HasTemporaryWrapper ? null : wrapperPath;
            return(flag);
        }
        public void TaskFailsWhenImproperlySigned()
        {
            var t = new ResolveComReference.TlbImp();
            t.TypeLibName = "Blah.tlb";
            string tempKeyContainer = null;
            string tempKeyFile = null;

            try
            {
                tempKeyContainer = FileUtilities.GetTemporaryFile();
                tempKeyFile = FileUtilities.GetTemporaryFile();
                t.ToolPath = Path.GetTempPath();

                // DelaySign is passed without a KeyFile or a KeyContainer
                t.DelaySign = true;
                Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.CannotSpecifyDelaySignWithoutEitherKeyFileOrKeyContainer");

                // KeyContainer and KeyFile are both passed in
                t.KeyContainer = tempKeyContainer;
                t.KeyFile = tempKeyFile;
                Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.CannotSpecifyBothKeyFileAndKeyContainer");

                // All the inputs are correct, but the KeyContainer passed in is bad            
                t.DelaySign = false;
                t.KeyContainer = tempKeyContainer;
                t.KeyFile = null;
                Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.StrongNameUtils.NoKeyPairInContainer", t.KeyContainer);

                // All the inputs are correct, but the KeyFile passed in is bad            
                t.KeyContainer = null;
                t.KeyFile = tempKeyFile;
                Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.StrongNameUtils.NoKeyPairInFile", t.KeyFile);
            }
            finally
            {
                if (tempKeyContainer != null)
                {
                    File.Delete(tempKeyContainer);
                }
                if (tempKeyFile != null)
                {
                    File.Delete(tempKeyContainer);
                }
            }
        }
        public void SdkToolsPath()
        {
            var t = new ResolveComReference.TlbImp();
            t.TypeLibName = "FakeLibrary.tlb";
            string badParameterValue = @"C:\Program Files\Microsoft Visual Studio 10.0\My Fake SDK Path";
            string goodParameterValue = Path.GetTempPath();
            bool taskPassed;

            Assert.Null(t.SdkToolsPath); // "SdkToolsPath should be null by default"
            Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath);

            t.SdkToolsPath = badParameterValue;
            Assert.Equal(badParameterValue, t.SdkToolsPath); // "New SdkToolsPath value should be set"
            Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "AxTlbBaseTask.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath);

            MockEngine e = new MockEngine();
            t.BuildEngine = e;
            t.SdkToolsPath = goodParameterValue;

            Assert.Equal(goodParameterValue, t.SdkToolsPath); // "New SdkToolsPath value should be set"
            taskPassed = t.Execute();
            Assert.False(taskPassed); // "Task should still fail -- there are other things wrong with it."

            // but that particular error shouldn't be there anymore.
            string sdkToolsPathMessage = t.Log.FormatResourceString("AxTlbBaseTask.SdkOrToolPathNotSpecifiedOrInvalid", t.SdkToolsPath, t.ToolPath);
            string messageWithNoCode;
            string sdkToolsPathCode = t.Log.ExtractMessageCode(sdkToolsPathMessage, out messageWithNoCode);
            e.AssertLogDoesntContain(sdkToolsPathCode);
        }
        public void KeyFileWithSpaces()
        {
            AxTlbBaseTask t = new ResolveComReference.TlbImp();
            string testParameterValue = @"C:\Program Files\myKeyFile.key";

            Assert.Null(t.KeyFile); // "KeyFile should be null by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/keyfile:", false /* no response file */);

            t.KeyFile = testParameterValue;
            Assert.Equal(testParameterValue, t.KeyFile); // "New KeyFile value should be set"
            CommandLine.ValidateHasParameter(t, @"/keyfile:" + testParameterValue, false /* no response file */);
        }
Exemple #18
0
        public void TaskFailsWithNoInputs()
        {
            var t = new ResolveComReference.TlbImp();

            Utilities.ExecuteTaskAndVerifyLogContainsErrorFromResource(t, "TlbImp.NoInputFileSpecified");
        }
Exemple #19
0
        /*
         * Method:  GenerateWrapper
         *
         * Generates a wrapper for this reference.
         */
        internal bool GenerateWrapper(out ComReferenceWrapperInfo wrapperInfo)
        {
            wrapperInfo = null;

            string rootNamespace            = ReferenceInfo.typeLibName;
            string wrapperPath              = GetWrapperPath();
            bool   generateWrapperSucceeded = true;

            if (ExecuteAsTool)
            {
                // delegate generation of the assembly to an instance of the TlbImp ToolTask. MUST
                // HAVE SET SDKTOOLSPATH TO THE TARGET SDK TO WORK
                var tlbImp = new ResolveComReference.TlbImp();

                tlbImp.BuildEngine          = BuildEngine;
                tlbImp.EnvironmentVariables = EnvironmentVariables;
                tlbImp.DelaySign            = DelaySign;
                tlbImp.KeyContainer         = KeyContainer;
                tlbImp.KeyFile                = KeyFile;
                tlbImp.OutputAssembly         = wrapperPath;
                tlbImp.ToolPath               = ToolPath;
                tlbImp.TypeLibName            = ReferenceInfo.fullTypeLibPath;
                tlbImp.AssemblyNamespace      = rootNamespace;
                tlbImp.AssemblyVersion        = null;
                tlbImp.PreventClassMembers    = _noClassMembers;
                tlbImp.SafeArrayAsSystemArray = true;
                tlbImp.Silent    = Silent;
                tlbImp.Transform = ResolveComReference.TlbImpTransformFlags.TransformDispRetVals;
                if (_referenceFiles != null)
                {
                    // Issue is that there may be reference dependencies that need to be passed in. It is possible
                    // that the set of references will also contain the file that is meant to be written here (when reference resolution
                    // found the file in the output folder). We need to filter out this case.
                    var fullPathToOutput = Path.GetFullPath(wrapperPath); // Current directory is the directory of the project file.
                    tlbImp.ReferenceFiles = _referenceFiles.Where(rf => String.Compare(fullPathToOutput, rf, StringComparison.OrdinalIgnoreCase) != 0).ToArray();
                }

                switch (_targetProcessorArchitecture)
                {
                case UtilitiesProcessorArchitecture.MSIL:
                    tlbImp.Machine = "Agnostic";
                    break;

                case UtilitiesProcessorArchitecture.AMD64:
                    tlbImp.Machine = "X64";
                    break;

                case UtilitiesProcessorArchitecture.IA64:
                    tlbImp.Machine = "Itanium";
                    break;

                case UtilitiesProcessorArchitecture.X86:
                    tlbImp.Machine = "X86";
                    break;

                case UtilitiesProcessorArchitecture.ARM:
                    tlbImp.Machine = "ARM";
                    break;

                case null:
                    break;

                default:
                    // Transmit the flag directly from the .targets files and rely on tlbimp.exe to produce a good error message.
                    tlbImp.Machine = _targetProcessorArchitecture;
                    break;
                }

                generateWrapperSucceeded = tlbImp.Execute();

                // store the wrapper info...
                wrapperInfo      = new ComReferenceWrapperInfo();
                wrapperInfo.path = (HasTemporaryWrapper) ? null : wrapperPath;
                // Changed to ReflectionOnlyLoadFrom, related to bug:
                //  RCR: Bad COM-interop assemblies being generated when using 64-bit MSBuild to build a project that is targeting 32-bit platform
                // The original call to UnsafeLoadFrom loads the assembly in preparation for execution. If the assembly is x86 and this is x64 msbuild.exe then we
                // have problems (UnsafeLoadFrom will fail). We only use this assembly for reference resolution so we don't need to be ready to execute the code.
                //
                // Its actually not clear to me that we even need to load the assembly at all. Reference resoluton is only used in the !ExecuteAsTool which is not
                // where we are right now.
                //
                // If we really do need to load it then:
                //
                //  wrapperInfo.assembly = Assembly.ReflectionOnlyLoadFrom(wrapperPath);
            }
            else
            {
                // use framework classes in-proc to generate the assembly
                TypeLibConverter converter = new TypeLibConverter();

                AssemblyBuilder   assemblyBuilder = null;
                StrongNameKeyPair keyPair;
                byte[]            publicKey;

                GetAndValidateStrongNameKey(out keyPair, out publicKey);

                try
                {
                    TypeLibImporterFlags flags = TypeLibImporterFlags.SafeArrayAsSystemArray | TypeLibImporterFlags.TransformDispRetVals;

                    if (_noClassMembers)
                    {
                        flags |= TypeLibImporterFlags.PreventClassMembers;
                    }

                    switch (_targetProcessorArchitecture)
                    {
                    case UtilitiesProcessorArchitecture.MSIL:
                        flags |= TypeLibImporterFlags.ImportAsAgnostic;
                        break;

                    case UtilitiesProcessorArchitecture.AMD64:
                        flags |= TypeLibImporterFlags.ImportAsX64;
                        break;

                    case UtilitiesProcessorArchitecture.IA64:
                        flags |= TypeLibImporterFlags.ImportAsItanium;
                        break;

                    case UtilitiesProcessorArchitecture.X86:
                        flags |= TypeLibImporterFlags.ImportAsX86;
                        break;

                    case UtilitiesProcessorArchitecture.ARM:
                        flags |= TypeLibImporterFlags.ImportAsArm;
                        break;

                    default:
                        // Let the type importer decide.
                        break;
                    }

                    // Start the conversion process. We'll get callbacks on ITypeLibImporterNotifySink to resolve dependent refs.
                    assemblyBuilder = converter.ConvertTypeLibToAssembly(ReferenceInfo.typeLibPointer, wrapperPath,
                                                                         flags, this, publicKey, keyPair, rootNamespace, null);
                }
                catch (COMException ex)
                {
                    if (!Silent)
                    {
                        Log.LogWarningWithCodeFromResources("ResolveComReference.ErrorCreatingWrapperAssembly", ItemName, ex.Message);
                    }

                    throw new ComReferenceResolutionException(ex);
                }

                // if we're done, and this is not a temporary wrapper, write it out to disk
                if (!HasTemporaryWrapper)
                {
                    WriteWrapperToDisk(assemblyBuilder, wrapperPath);
                }

                // store the wrapper info...
                wrapperInfo          = new ComReferenceWrapperInfo();
                wrapperInfo.path     = (HasTemporaryWrapper) ? null : wrapperPath;
                wrapperInfo.assembly = assemblyBuilder;
            }

            // ...and we're done!
            return(generateWrapperSucceeded);
        }
Exemple #20
0
        public void NoLogo()
        {
            var t = new ResolveComReference.TlbImp();

            Assert.False(t.NoLogo); // "NoLogo should be false by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/nologo", false /* no response file */);

            t.NoLogo = true;
            Assert.True(t.NoLogo); // "NoLogo should be true"
            CommandLine.ValidateHasParameter(t, @"/nologo", false /* no response file */);
        }
Exemple #21
0
        public void OutputAssemblyWithSpaces()
        {
            var t = new ResolveComReference.TlbImp();
            string testParameterValue = @"c:\Program Files\AxInterop.Foo.dll";

            Assert.Null(t.OutputAssembly); // "OutputAssembly should be null by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/out:", false /* no response file */);

            t.OutputAssembly = testParameterValue;
            Assert.Equal(testParameterValue, t.OutputAssembly); // "New OutputAssembly value should be set"
            CommandLine.ValidateHasParameter(t, @"/out:" + testParameterValue, false /* no response file */);
        }
Exemple #22
0
        public void AssemblyVersion()
        {
            var t = new ResolveComReference.TlbImp();
            Version testParameterValue = new Version(2, 12);

            Assert.Null(t.AssemblyVersion); // "AssemblyVersion should be null by default"
            CommandLine.ValidateNoParameterStartsWith(t, @"/asmversion:", false /* no response file */);

            t.AssemblyVersion = testParameterValue;
            Assert.Equal(testParameterValue, t.AssemblyVersion); // "New AssemblyNamespace value should be set"
            CommandLine.ValidateHasParameter(t, @"/asmversion:" + testParameterValue.ToString(), false /* no response file */);
        }
Exemple #23
0
        public void OutputAssembly()
        {
            var t = new ResolveComReference.TlbImp();
            string testParameterValue = "AxInterop.Foo.dll";

            Assert.IsNull(t.OutputAssembly, "OutputAssembly should be null by default");
            CommandLine.ValidateNoParameterStartsWith(t, @"/out:", false /* no response file */);

            t.OutputAssembly = testParameterValue;
            Assert.AreEqual(testParameterValue, t.OutputAssembly, "New OutputAssembly value should be set");
            CommandLine.ValidateHasParameter(t, @"/out:" + testParameterValue, false /* no response file */);
        }