From 5fda5e167d92838b3eb25ca5e4ee050806d0aff6 Mon Sep 17 00:00:00 2001 From: Jevan Saks Date: Mon, 20 Oct 2025 17:24:54 -0700 Subject: [PATCH 1/2] Doc updates --- docfx/docs/getting-started.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/docfx/docs/getting-started.md b/docfx/docs/getting-started.md index 77fb69d6..ed5416fb 100644 --- a/docfx/docs/getting-started.md +++ b/docfx/docs/getting-started.md @@ -121,7 +121,9 @@ To keep CsWin32 generating the referred types you need, add them explicitly to ` ### Support for trimming, AOT, and/or disabling the runtime marshaler -Newer .NET runtime versions may fail for CsWin32 generated code when the application project builds with one or both of these properties set: +**NEW!** CsWin32 now supports AOT by generating code from an MSBuild task rather than a source generator. + +If you set one of these flags: ```xml true @@ -129,7 +131,26 @@ Newer .NET runtime versions may fail for CsWin32 generated code when the applica true ``` -CsWin32 supports these environments by avoiding code that relies on the runtime marshaler when the `allowMarshaling` setting is disabled in the `NativeMethods.json` file. +Then you have two options: + +#### Enable CsWin32RunAsBuildTask + +CsWin32 now supports AOT by generating code which relies on `GeneratedComInterface` and `LibraryImport`, which are source generators. +To make this chaining work, you have to request CsWin32 to run as a build task to generate the source code _before_ compile. Do this by adding this to your csproj: + +```xml +true +true +``` + +It is also strongly recommended to set DisableRuntimeMarshalling=true as shown here to ensure that the COM source generators handle +all the marshalling correctly. See https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation and +https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation for more information on these .NET source +generators. + +#### Disable runtime marshalling + +CsWin32 can also support these environments by avoiding code that relies on the runtime marshaler when the `allowMarshaling` setting is disabled in the `NativeMethods.json` file. For example: ```json From 224e4b78b712608b027deb589b65abce7905508d Mon Sep 17 00:00:00 2001 From: Jevan Saks Date: Tue, 21 Oct 2025 15:04:56 -0700 Subject: [PATCH 2/2] Update docfx/docs/getting-started.md Co-authored-by: Andrew Arnott --- docfx/docs/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docfx/docs/getting-started.md b/docfx/docs/getting-started.md index ed5416fb..c54668b1 100644 --- a/docfx/docs/getting-started.md +++ b/docfx/docs/getting-started.md @@ -121,7 +121,7 @@ To keep CsWin32 generating the referred types you need, add them explicitly to ` ### Support for trimming, AOT, and/or disabling the runtime marshaler -**NEW!** CsWin32 now supports AOT by generating code from an MSBuild task rather than a source generator. +**NEW!** CsWin32 now has improved support for NativeAOT by generating code from an MSBuild task rather than a source generator so that it can leverage other source generators that support `[LibraryImport]` and others. If you set one of these flags: