.NET 9 AOT Breakthrough – Support for Legacy Win7 and XP Environments

.NET 9 AOT Breakthrough – Support for Legacy Win7 and XP Environments

Starting with .NET 9, AOT supports Win7 and XP, not just SP1 versions.

Last updated 10/23/2024 1:49 PM
沙漠尽头的狼
9 min read
Category
Avalonia UI Winform .NET
Topic
C# AOT
Tags
.NET C# Avalonia UI Winform AOT

Introduction

With continuous technological advancements, Microsoft's .NET framework brings exciting new features in each iteration. In .NET 9, a particularly notable highlight is AOT (Ahead-of-Time) support, which allows developers to optimize applications during the compilation phase to run on older Windows systems, including Windows 7 and even Windows XP. This not only improves performance but also opens up new possibilities for enterprises and individual developers who still rely on these legacy platforms.

Quick knowledge points:

  1. Introduction to .NET 9 AOT

The .NET 9 AOT compiler uses static compilation to convert .NET applications into executable files that can run directly on the target machine, eliminating the time and resources required for JIT (Just-In-Time) compilation at runtime. This brings significant advantages in scenarios demanding high performance and compatibility with older systems.

  1. Background of supporting Windows 7 and Windows XP

Although Windows 7 and XP are no longer mainstream operating systems, they are still widely used in certain areas such as enterprise legacy systems, embedded devices, or resource-constrained environments. The extension of .NET 9 AOT compilation aims to meet the compatibility and performance requirements of these scenarios.

  1. How it works
  • Compilation optimization: .NET 9 performs more detailed optimizations during AOT compilation, resulting in smaller executable files and faster startup times.
  • Downward compatibility: Through carefully designed compilation strategies, compatibility with Win7 and XP APIs is ensured, allowing code to run seamlessly.
  • Security considerations: While supporting older systems, .NET 9 still prioritizes security and provides a certain level of protection mechanisms against potential risks.
  1. Practical applications and advantages
  • Performance improvement: Programs compiled with AOT are generally faster than those executed via JIT, especially for CPU-intensive tasks.
  • Simplified deployment: No need for users to install the .NET runtime, simplifying the deployment process.
  • Reduced maintenance costs: For enterprises relying on older systems, it avoids the hassle of frequent runtime upgrades.

This article is only intended to share the practical results achieved by netizens and the site owner. If you have more findings, contributions or PRs are welcome.

Windows 7 Support

The image below shows a screenshot of an Avalonia UI cross-platform project compiled by a netizen running on Windows 7 non-SP1 environment:

As shown above, the left side is the program's running interface, and the right side shows the operating system version.

For readers' convenience in copying code, the reference configuration is provided below:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>WinExe</OutputType>
		<TargetFramework>net9.0-windows</TargetFramework>
		<Nullable>enable</Nullable>
		<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
		<ApplicationManifest>app.manifest</ApplicationManifest>
		<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
		<PublishAot>true</PublishAot>
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
		<InvariantGlobalization>true</InvariantGlobalization>
        <!--Supports running on Windows XP or higher; XP failed with Ava-->
		<WindowsSupportedOSPlatformVersion>5.1</WindowsSupportedOSPlatformVersion>
		<RuntimeIdentifier>win-x64</RuntimeIdentifier>
		<TargetPlatformMinVersion>5.1</TargetPlatformMinVersion>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="VC-LTL" Version="5.1.1-Beta3" />
	</ItemGroup>
	<ItemGroup>
		<PackageReference Include="Avalonia" Version="11.1.1" />
		<PackageReference Include="Avalonia.Desktop" Version="11.1.1" />
		<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.1" />
		<PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.1" />
		<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
		<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.1" />
		<PackageReference Include="Avalonia.ReactiveUI" Version="11.1.1" />
	</ItemGroup>
</Project>

Key configuration notes:

  1. <PublishAot>true</PublishAot>

This switch enables AOT compilation publishing.

  1. <WindowsSupportedOSPlatformVersion>5.1</WindowsSupportedOSPlatformVersion>

Supports running on Windows XP or higher versions of Windows.

  1. VC-LTL

VC-LTL is an open-source runtime based on a modified Microsoft VC, effectively reducing application size and eliminating dependencies on Microsoft runtime DLLs such as msvcr120.dll and api-ms-win-crt-time-l1-1-0.dll.

For Windows 7 and above, AOT may work directly (no need to install .NET runtime). However, if it fails on the target system, you can try adding this library and recompiling with AOT. For detailed principles, refer to this repository: https://github.com/Chuyu-Team/VC-LTL

Tested by the site owner: Windows 7 may also require adding the YY-Thunks package reference:

<PackageReference Include="YY-Thunks" Version="1.1.4-Beta3" />

About YY-Thunks: Link, description:

As we all know, each Windows update adds a large number of new APIs, making it costly to ensure compatibility across different Windows versions. Consequently, many open-source projects no longer support some earlier Windows versions, such as Windows XP RTM.

Isn't there a fast and efficient solution to the problem of "unable to locate program entry points"?

YY-Thunks (Duck Boat) exists to smooth out the differences between systems. By simply adding an obj during compilation, these compatibility issues can be automatically resolved, making it easier to support older versions of Windows!

After testing, Winforms can run after .NET 9 x86 AOT publishing. The screenshot is as follows:

Winforms project configuration:

Copyable configuration:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>WinExe</OutputType>
		<TargetFramework>net9.0-windows</TargetFramework>
		<Nullable>enable</Nullable>
		<UseWindowsForms>true</UseWindowsForms>
		<ImplicitUsings>enable</ImplicitUsings>
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
		<InvariantGlobalization>true</InvariantGlobalization>
		<WindowsSupportedOSPlatformVersion>5.1</WindowsSupportedOSPlatformVersion>
		<RuntimeIdentifier>win-x64</RuntimeIdentifier>
		<TargetPlatformMinVersion>5.1</TargetPlatformMinVersion>
		<PublishAot>true</PublishAot>
		<_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="VC-LTL" Version="5.1.1-Beta3" />
		<PackageReference Include="WinFormsComInterop" Version="0.5.0" />
	</ItemGroup>
</Project>

Also add one line ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance); at the entry point:

using System.Runtime.InteropServices;

namespace WinFormsAotDemo;

internal static class Program
{
    /// <summary>
    ///  The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // To customize application configuration such as set high DPI settings or default font,
        // see https://aka.ms/applicationconfiguration.

        ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance);

        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}

Windows XP Support

Currently tested console applications can run:

Netizen's conclusion:

XP requires linking YY-Thunks. Reference link: https://github.com/Chuyu-Team/YY-Thunks (mentioned earlier; if Win7 fails, you can also try adding this package reference).

You can follow this YY-Thunks ISSUE: https://github.com/Chuyu-Team/YY-Thunks/issues/66

The console project configuration supporting XP is as follows:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net9.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>
	<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
		<InvariantGlobalization>true</InvariantGlobalization>
		<WindowsSupportedOSPlatformVersion>5.1</WindowsSupportedOSPlatformVersion>
		<SupportWinXP>true</SupportWinXP>
		<PublishAot>true</PublishAot>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="VC-LTL" Version="5.1.1-Beta3" />
	</ItemGroup>
</Project>

Netizen's experience:

Areas for Improvement

Testing shows that using the Prism framework will cause errors:

Using HttpClient also causes errors:

2024-08-02

By reading the source code of the open-source Avalonia theme library Semi.Avalonia and the PR from author Rabbitism (Rabbit boss), the Prism issue has been resolved. Other libraries should follow a similar approach. The modifications are as follows:

Add a Roots.xml file to the main project with the following content:

<linker>
    <assembly fullname="CodeWF.Toolbox.Desktop" preserve="All"/>
    <assembly fullname="Ursa.PrismExtension" preserve="All" />
    <assembly fullname="Prism" preserve="All" />
    <assembly fullname="DryIoc" preserve="All" />
    <assembly fullname="Prism.Avalonia" preserve="All"/>
    <assembly fullname="Prism.DryIoc.Avalonia" preserve="All"/>
    <assembly fullname="CodeWF.Toolbox" preserve="All" />
</linker>

Add the XML configuration to the main project:

<ItemGroup>
    <TrimmerRootDescriptor Include="Roots.xml" />
</ItemGroup>

HttpClient issues can be handled similarly. It is not elaborated here; you'll need to experiment further.

Every company's projects are extremely different and complex. Actual publishing requires continuous testing. To support Windows 7 and Windows XP, you may need to replace libraries or make API usage trade-offs. Feel free to share your experiences during use.

Conclusion

The AOT support in .NET 9 undoubtedly broadens the application scope of the .NET ecosystem, providing powerful tools for developers who need to run high-performance applications on older platforms. As technology evolves, we look forward to future .NET versions further breaking boundaries, making programming more flexible and efficient.

Thanks to netizens GSD and M$達 for sharing this good news. We recommend reading Da Shi Tou's article "Support for .NET on Various Operating Systems": https://newlifex.com/tech/os_net

Reference AOT project: https://github.com/dotnet9/CodeWF.Toolbox

Technical Discussions

For software development technical discussions, add QQ group: 771992300

Or scan the site owner's WeChat (codewf, note add group) to join the WeChat technical discussion group:

Keep Exploring

Related Reading

More Articles
Same category / Same topic 8/29/2023

.NET 8.0 AOT DebugView

DebugView is an application that allows you to monitor debug output on your local system or on any computer on a network accessible via TCP/IP.

Continue Reading