/* * 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); }
/* * 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; }
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); }
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); }
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; }