msbuild
msbuild copied to clipboard
[vs17.14] Replace obsolete UCOMITypeInfo with recommended System.Runtime.InteropServices.ComTypes.ITypeInfo
Backport of #11940 to vs17.14
Fixes https://developercommunity.visualstudio.com/t/Unable-to-build-VBNET-app-in-VS2022--17/10867825
Work item (Internal use): AB#2495104
Summary
ClickOnce users are running into an intermittent build failure in Forms/WPF projects with COM library references where the ClickOnce manifest creation task (GenerateApplicationManifest) fails. The Isolated property on the COM reference needs to be set to true for the failure to manifest.
The System.Runtime.InteropServices.UCOMITypeInfo.ReleaseTypeAttr called in the ComImporter class throws a COMException when the task fails. The HResult in the exception is a random value instead of a standard HResult.
Below is the msbuild stack when the ClickOnce task fails:
System.Runtime.InteropServices.COMException (0xAEDCAE40): Exception from HRESULT: 0xAEDCAE40
at System.Runtime.InteropServices.UCOMITypeInfo.ReleaseTypeAttr(IntPtr pTypeAttr)
at Microsoft.Build.Tasks.Deployment.ManifestUtilities.c…ctor(String path, OutputMessageCollection outputMessages, String outputDisplayName)
at Microsoft.Build.Tasks.Deployment.ManifestUtilities.FileReference.ImportComComponent(String path, OutputMessageCollection outputMessages, String outputDisplayName)
at Microsoft.Build.Tasks.GenerateApplicationManifest.AddIsolatedComReferences(ApplicationManifest manifest)
at Microsoft.Build.Tasks.GenerateApplicationManifest.BuildApplicationManifest(ApplicationManifest manifest)
at Microsoft.Build.Tasks.GenerateManifestBase.BuildManifest()
at Microsoft.Build.Tasks.GenerateManifestBase.Execute()
The fix is to replace the UCOMITypeLib/UCOMITypeInfo classes that were obsoleted in .NET 2.0 with the recommended ITypeLib/ITypeInfo classes from System.Runtime.InteropServices.ComTypes.
The root cause appears to be that UCOMITypeInfo.ReleaseType does not have the PreserveSig attribute which may be causing the .NET Interop layer to assume a HResult return value from the method. The method returns void which is likely causing COM Interop to throw a failure based on what the return value register contains.
Customer Impact
Build failure in Forms/WPF projects with COM library reference when its Isolated property is set.
Regression?
No. There are reports of this failure on SO from several years ago. This is the first report on Developer Community feedback.
Testing
Intermittent failure is not reproducible after this change in the repro environment. CTI test pass completed and signed off with the fix.
Risk
Low