

introduction
As technology continues to advance, Microsoft's. NET framework brings surprising new features with every iteration. A particularly eye-catching highlight in the. NET 9 release is AOT (Ahead-of-Time) support, which allows developers to optimize applications during the compilation stage 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 corporate and individual developers who still rely on these older platforms.
** Popularization of small knowledge: **
- Introduction to NET 9 AOT
The. NET 9 AOT compiler converts. NET applications into executable files that can be executed directly on the target machine through static compilation, eliminating the time and resources required for JIT (Just-In-Time) compilation at runtime. This has significant advantages for scenarios that require high performance requirements and need to support legacy systems.
- Background of supporting Windows 7 and Windows XP
Although Windows 7 and XP are no longer the mainstream operating systems, they are still widely used in certain areas, such as enterprise legacy systems, embedded devices, or resource-constrained environments. NET 9's AOT compilation extension is designed to meet the compatibility and performance requirements of these scenarios.
- how to achieve
- ** Compilation process optimization **: During AOT compilation, NET 9 optimizes the code in more detail, making the generated executable file smaller and the startup speed faster.
- ** Downward compatibility **: Through carefully designed compilation strategies, compatibility with Win7 and XP APIs is ensured, allowing the code to run seamlessly.
- ** Security considerations **: Although it supports old systems,. NET 9 still focuses on security and provides a certain degree of protection mechanisms to protect against potential risks.
- Example applications and advantages
- ** Performance improvements **: AOT-compiled programs are usually faster than JIT-executed programs, especially for CPU-intensive tasks.
- ** Easy deployment **: Users do not need to install the. NET runtime, simplifying the deployment process.
- ** Reduced maintenance costs **: For enterprises that rely on old systems, the trouble caused by frequent upgrades is avoided.
This article is only sharing a result of the practice of netizens and webmasters. If you have more discoveries, you are welcome to submit or PR this article.
Windows 7 support
The following picture is a screenshot of the Avalonia UI cross-platform project compiled by netizens running in Win 7 non-SP1 environments:

As shown in the figure above, the program execution interface is on the left side, and the operating system version is on the right side.


In order to facilitate readers to copy the code, the reference configuration is posted as follows:
<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>
<!--支持在Windows XP或更高版本的Windows操作系统上运行,XP下尝试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 instructions above:
<PublishAot>true</PublishAot>
This switch is used to support AOT compilation and release
<WindowsSupportedOSPlatformVersion>5.1</WindowsSupportedOSPlatformVersion>
Supports running on Windows XP or later versions of the Windows operating system
VC-LTL
VC-LTL is an open source runtime modified based on Microsoft VC. It effectively reduces the size of applications and gets rid of Microsoft runtime DLLs, such as msvcr120.dll, api-ms-win-crt-time-l1-1-0.dll and other dependencies.
With Win7 and above, AOT may work normally (you don't need to install the. NET runtime). However, it may also fail to run on the target system, so you can add this library and try to re-compile AOT. Refer to this warehouse for detailed principles: github.com/Chuyu-Team/VC-LTL
** Measured by the webmaster: Windows 7 may also need to add YY-Thunder package quote: **
<PackageReference Include="YY-Thunks" Version="1.1.4-Beta3" />
关于YY-Thunks:链接,说明:
As we all know, every update from Windows adds a large number of APIs, which makes compatibility with different versions of Windows requires a lot of effort. As a result, a large number of open source projects are no longer compatible with some earlier versions of Windows, such as Windows XP RTM.
Isn't there a quick and efficient solution to the problem of not being able to locate program input points?
YY-Thunder exists for the purpose of smoothing out the differences between different systems. Simply adding an obj during compilation can automatically solve these compatibility issues. Make it easier for you to be compatible with older versions of Windows!
After testing, Winform can run after the release of. NET 9x86 AOT. The screenshots of the effect are as follows:

Winform project configuration is as follows:

The copyable configurations are as follows:
<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>
入口再加一句代码ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance);:
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 testing can run the console program:

Netizens came to the conclusion:

XP requires a link to YY-Thunder, refer to the link: https://github.com/Chuyu-Team/Y-Thunder (as mentioned earlier, if Win7 fails, you can also add this package reference attempt)

You can pay attention to YY-Thunder, an ISSUE: github.com/Chuyu-Team/YY-Thunks/issues/66

The engineering configuration for the console to support 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>
Netizens 'comments:

Part that needs to be strengthened
After testing, errors will be reported when using the Prism framework:

There will also be errors when using HttpClient:

2024-08-02
通过阅读开源Avalonia主题库 Semi.Avalonia 的源码及作者
Rabbitism兔佬的PR已经解决Prism问题的,其他库问题使用方法应该类似,修改如下:Add Roots.xml to the main project, and the content is as follows:
<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 this XML configuration to the main project:
<ItemGroup> <TrimmerRootDescriptor Include="Roots.xml" /> </ItemGroup>HttpClient is a similar processing method. I won't go into it here, but you need to try more.
The different projects of each company are extremely different and complex. The actual release still requires constant testing. In order to support Windows 7 and Windows XP, you may have to make operations such as library replacement and some API use trade-offs. Readers are welcome to share their experiences during use.
conclusion
NET 9's AOT support has undoubtedly broadened the scope of applications of the. NET ecosystem and provided powerful tools for developers who need to run high-performance applications on older platforms. As technology evolves, we look forward to more. NET versions in the future that will further break the boundaries and make programming more flexible and efficient.
感谢网友GSD及M$達分享的这个好消息,大石头这篇文章《各版本操作系统对.NET 支持情况》推荐大家阅读:https://newlifex.com/tech/os_net
Refer to the AOT project: github.com/dotnet9/CodeWF.Toolbox
** Technical exchange **
Software development technology exchange Add QQ group: 771992300
或扫站长微信(codewf,备注加群)加入微信技术交流群:
