dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.36k stars 965 forks source link

.NET 9 Building fails with CLI/CLR C++ mixed code on Bitmap due to introduction of IPointer in #10783 in Visual Studio 2022 17.12 preview #11983

Closed mgexo closed 3 days ago

mgexo commented 2 weeks ago

.NET version

.NET 9 { "name": "Microsoft.NETCore.App", "version": "9.0.0-preview.7.24405.7" }, { "name": "Microsoft.WindowsDesktop.App", "version": "9.0.0-preview.7.24405.2" }

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

The error is not there in any previous .NET build before .NET9 e.g. it worked in .NET5, .NET6, .NET8 for us.

Issue description

.NET 9 Building fails with CLR C++ mixed code on Bitmap due to introduction of IPointer in #10783 in Visual Studio 2022 17.12 preview You get this strange error when you want to access any property of a System::Drawing::Bitmap ^ bmp in C++ CLI/CLR :

error C3229: 'TPointer *': indirections on a generic type parameter are not allowed

I assume this is caused by this change which doesn't even seem to document why it introduced such a native pointer class as base class from #10783 in https://github.com/dotnet/winforms/pull/10783/files

it changed public sealed unsafe class Bitmap : Image to public sealed unsafe class Bitmap : Image, IPointer

Note that this change will most likely also break running any .NET 8 or .NET6 software on .NET 9 , which may happen a lot when people like us distribute their software with Rollforward=LatestMajor

Steps to reproduce

Create mixed mode C++ CLR project for .NET9 with Visual Studio 2022 17.12 preview and a function that uses System::Drawing::Bitmap ^ bmp as argument and then try to access a property e.g. int flags = bmp->Flags; It will complain that you try to access a pointer error C3229: 'TPointer *': indirections on a generic type parameter are not allowed

Note that this may potentially also be caused by a bug in the Visual Studio 17.12 preview compiler or the .NET 9 SDK compiler but we are very worried that simply due to this change our software will not work on PCs with NET9

test.cpp(2168,5): error C3229: 'TPointer *': indirections on a generic type parameter are not allowed 1>(compiling source file '/test.cpp') 1> test.cpp(2168,5): 1> compiler using 'TPointer' to continue parsing 1> test.cpp(2168,5): 1> This diagnostic occurred while importing type 'Windows::Win32::Foundation::IPointer' from assembly 'System.Private.Windows.Core, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 1> test.cpp(2168,5): 1> This diagnostic occurred while importing type 'System::Drawing::IImage' from assembly 'System.Private.Windows.Core, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 1> test.cpp(2168,5): 1> This diagnostic occurred while importing type 'System::Drawing::Image' from assembly 'System.Drawing.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 1> test.cpp(2168,5): 1> This diagnostic occurred while importing type 'System::Drawing::Bitmap' from assembly 'System.Drawing.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

To reproduce the compile issue see example in https://github.com/dotnet/winforms/issues/11983#issuecomment-2308864145

mgexo commented 2 weeks ago

Hi @JeremyKuhne, are you aware of any related topics?

mgexo commented 2 weeks ago

Minimal repro sample for the compiler issue CLRTestProjectNET9A.zip


void TestClass1::TestMethodOnBitmap(System::Drawing::Bitmap ^ bmp) {
    int k = bmp->Flags; // this line with .NET9 preview7 and VS 2022 17.12 preview gives error C3229: 'TPointer *': indirections on a generic type parameter are not allowed 
    System::Diagnostics::Trace::WriteLine("k=" + k);
}
mgexo commented 2 weeks ago

The strange TPointer compiler error also sometimes affects System::Windows::Forms::ToolStripItemCollection ^ and not only Bitmap

elachlan commented 2 weeks ago

@Olina-Zhang can your team please test this?

Olina-Zhang commented 2 weeks ago

Can repro it with .NET 9, not .NET 8 .NET 9.0: image .NET 8.0: image

Zheng-Li01 commented 1 day ago

Verified the issue with .NET SDK 9.0.100-rc.2.24460.10 + private dlls built from Winforms main branch that the dlls were replaced under C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\9.0.0-rc.2.24458.2\ref\net9.0 path. the issue has been fixed that the project build successfully with no error as below screenshot. image

Zheng-Li01 commented 1 day ago

Verified the issue with .NET SDK 9.0.100-rc.2.24460.10 + private dlls built from Winforms release/9.0 branch, the issue has been fixed that have the same results as above.

mgexo commented 1 day ago

Looks good thanks. We will test it once the next .NET9 RC is released