Closed Junjun-zhao closed 1 year ago
Tagging subscribers to this area: @dotnet/area-system-componentmodel See info in area-owners.md if you want to be subscribed.
Author: | Junjun-zhao |
---|---|
Assignees: | ericstj |
Labels: | `area-System.ComponentModel` |
Milestone: | - |
@Junjun-zhao , if the app is targeting net5.0, how is it running on only net7.0? are you sure app is targeting net 7.0 and machine only has net 7.0 but not net5.0?
@Olina-Zhang , can you try if you guys can repro this?
@dreddy-work Thanks for helping look into this issue. I'm from AppCompat team and doing third-party apps runtime combability validation with .NET 7 now. This issue was found when run the app against .NET 7.0 runtime.
You can Set the DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX environment variable to 2 to let the .NET 5.0 app run against .NET 7.0
Reference, following is code snippet that throwing.
IntPtr modHandle = Kernel32.GetModuleHandleW(null);
// Older versions of Windows AV rather than returning E_OUTOFMEMORY.
// Catch this and then we re-throw an out of memory error.
try
{
// CreateWindowEx throws if WindowText is greater than the max
// length of a 16 bit int (32767).
// If it exceeds the max, we should take the substring....
if (cp.Caption is not null && cp.Caption.Length > short.MaxValue)
{
cp.Caption = cp.Caption.Substring(0, short.MaxValue);
}
createResult = User32.CreateWindowExW(
(User32.WS_EX)cp.ExStyle,
windowClass._windowClassName,
cp.Caption,
(User32.WS)cp.Style,
cp.X,
cp.Y,
cp.Width,
cp.Height,
cp.Parent,
IntPtr.Zero,
modHandle,
cp.Param);
lastWin32Error = Marshal.GetLastWin32Error();
.
.
if (createResult == IntPtr.Zero)
{
throw new Win32Exception(lastWin32Error, SR.ErrorCreatingHandle);
}
``
@Junjun-zhao, What is the scenario here? What is set DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=2
doing here? can you retarget the app to .NET7.0 and run it? can you double click and run it? or its same issue with command line as well as double click? Can you please attach sample here or grant permission to us to access share in the linked Azdo item?
Provided information to Devendar via Teams - DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX setting is to make app use the latest major runtime installed on the machine instead of using the .NET version that the app was built with. More details available in Roll Forward section of https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-2-1 Have also shared permissions to source and app binaries.
@dreddy-work The same stack info and ComponentModel.Win32Exception exception occurred when I was writing a program before. I'm not sure if it's similar to this current post. I might be able to provide some useful information.
When I upgrade my program from .net 4.8 to .net 5.0 :
.net 4.8 code:
namespace WinFormsApp1
{
internal static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
.net 5.0 code:
namespace WinFormsApp1
{
internal static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.Run(new Form1());
}
}
}
You can compare the difference between these two pieces of code. These are all from templates that come with Visual Studio.
If this statement is missing:
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Since Visual Studio just upgrades the .net version number, it doesn't automatically add this statement for you.
Then in method Form.ShowDialog(), there is a certain probability that ComponentModel.Win32Exception will be prompted. It's not 100% wrong. It doesn't go wrong every time. But at indeterminate times, sometimes it goes wrong and sometimes it doesn't.
Currently I am using .net 6.0 With Win10 to run these codes. After the above adjustment, there is basically no Exception.
@dreddy-work
can you retarget the app to .NET7.0 and run it?
When retargeting the app to .NET7.0 and run it from Visual Studio, We get the same exception, see screenshot from Visual Studio:
can you double click and run it? or its same issue with command line as well as double click?
If we double click to run the exe directly, the app doesn't launch and we can see the same error message from Event Viewer.
In addition, we have tested with dotnet-sdk-7.0.100-alpha.1.22057.7, issue still exists.
@Junjun-zhao
You can see my previous content in this post.
Add this statement to your code:
Application.SetHighDpiMode(HighDpiMode.SystemAware);
It might solve the problem. This could be a bug due to DPI.
Full Sample Code:
namespace WinFormsApp1
{
internal static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.Run(new Form1());
}
}
}
@roland5572 Thanks for your comments for this issue. We have tried after adding Application.SetHighDpiMode(HighDpiMode.SystemAware);
, this issue still repro with same error.
@Olina-Zhang , can you try if you guys can repro this?
As issue's steps, it can repro on a machine with just .net 7.0 installed and that environment variables set. And it also repro when retargeting app from .net 5.0 to .net 7.0 in VS. Here is the .net 5.0 testing app with MetroFramework nuget package: WinFormsApp9.zip
More Info: If skip to install MetroFramework nuget package, it cannot repro.
@roland5572 - if you could collect a crash dump. or call stack for exception related to the lack of Application.SetHighDpiMode(HighDpiMode.SystemAware);
, could you please open a dedicated issue?
@Tanya-Solyanik Missing this statement can make the program unstable, but it won't fire 100% of the time. I'll try this bug again in the next few days. I'm not sure if the current .NET version has fixed this. If it still happens, I will create a new post.
And also verified this issue using that testing app with MetroFramework nuget package on a just .Net 6.0 SDK: 6.0.200-preview.22055.15, it cannot repro.
@Olina-Zhang when you run the app skipping the MetroFramework what do you get? Does the matching game correctly load and execute? This Framework was last touched in 2013. Though I don't know what changed in 7.0 that would have impacted this at all.
@merriemcgaw If skip to install MetroFramework nuget package, this issue cannot repro, our form is launched successfully after executed sample app exe.
At some point I'd love to see what is in that MetroFramework package and debug to see what has changed with .NET 7. @dreddy-work is this something we could get on your backlog (pretty low down of course š )?
@Tanya-Solyanik Do you have any update for this bug? We still repro this with build dotnet-sdk-7.0.100-preview.3.22179.4
@Olina-Zhang do you think your team would be able to try to repro this to see if it's still a problem?
@merriemcgaw It also reproduces the application with MetroFramework nuget package in the latest .Net 8 SDK build: 8.0.100-preview.2.23121.12.
Thank you for the seriously fast turnaround here! š„
Duplicate of https://github.com/dotnet/winforms/issues/8803
Verified and issue is fixed on dotnet-sdk-8.0.100-preview.3.23178.7.
Description
When testing WinForms Samples app against runtime dotnet-sdk-7.0.100-alpha.1.21568.2, it failed to launch with error System.ComponentModel.Win32Exception
Reproduction Steps
App Source, App checking at: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1458251 Repro Steps for the Problematic App: Copy Apps\Winforms Samples from server to local machine. Launch MatchingGame.exe from Winforms Samples\matching-game
Minimal Repro steps: 1.Create a .NET 5.0 WinForm project. 2.Install MetroFramework nuget package from nuget.org. 3.Update the Form1.cs to inherit from MetroForm:
4.Build the project to generate WinFormApp1.exe 5.Launch the exe on a machine that only dotnet-sdk-7.0.100-alpha.1.21568.2 installed
Expected behavior
App launch successful.
Actual behavior
Launch failed with error
Regression?
Verify Scenarios: 1). Windows10 RS5 x64+dotnet-sdk-7.0.100-alpha.1.21568.2:Fail 2). Windows10 RS5 x64+dotnet-sdk-6.0.200-preview.21603.2: Pass
Known Workarounds
No response
Configuration
Application Name: WinForms Samples OS: Windows 10 RS5 CPU: X64 .NET Build Number: dotnet-sdk-7.0.100-alpha.1.21568.2
Other information
1) Launch failed with MatchingGame.exe: If we launch the dll with dotnet command from command prompt window, we will get below error message:
And we found this issue is caused by MetroFramework nuget package used in the project. But this package only one version. NuGet Gallery | MetroFramework 1.2.0.3
2)Launch failed with error with minimal demo WinFormsApp1.exe:
When using Windbg to launch the minimal demo WinFormsApp1.exe, we will get these:
@dotnet-actwx-bot @dotnet/compat