Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SendKeys.SendWait will throw could not load type OsVersion exception #14145

@lindexi

Description

@lindexi

.NET version

.NET 10/.NET 9

Did it work in .NET Framework?

Not tested/verified

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

None.

Issue description

When use <PackageReference Include="System.Drawing.Common" Version="10.0.1" /> in .NET 9 TargetFramework, the System.Windows.Forms.SendKeys.SendWait("Hello, World!"); will throw the exception:

System.TypeInitializationException: The type initializer for 'System.Windows.Forms.SendKeys' threw an exception.
   ---> System.TypeInitializationException: The type initializer for 'System.Windows.Forms.ScaleHelper' threw an exception.
   ---> System.TypeLoadException: Could not load type 'System.Private.Windows.Core.OsVersion' from assembly 'System.Private.Windows.Core, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
     at System.Windows.Forms.ScaleHelper.<InitializeStatics>g__GetPerMonitorAware|8_1()
     at System.Windows.Forms.ScaleHelper.InitializeStatics()
     --- End of inner exception stack trace ---
     at System.Windows.Forms.ScaleHelper.get_InitialSystemDpi()
     at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)
     at System.Windows.Forms.SendKeys.SKWindow..ctor()
     at System.Windows.Forms.SendKeys..cctor()
     --- End of inner exception stack trace ---
     at System.Windows.Forms.SendKeys.SendWait(String keys)
     at sendkey_error.MainWindow.Button_Click(Object sender, RoutedEventArgs e)

This issue origin reported in dotnet/wpf#11313

And I think it was introduced by #12839

Cc @JeremyKuhne

Steps to reproduce

  1. Create a simple .NET project with the .csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="10.0.1" />
  </ItemGroup>
</Project>
  1. Call the SendKeys.SendWait in Program.cs
System.Windows.Forms.SendKeys.SendWait("Hello, World!");
  1. Build and run the project and you can find the exception.

You can find all my demo code in https://github.com/lindexi/lindexi_gd/tree/1534db9e5f807d2c958cc8f7509f9f45229651e5/WPFDemo/LaiwerelawkewaFeereajemle

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labeluntriagedThe team needs to look at this issue in the next triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions