Skip to content

Commit f01e350

Browse files
jevansaksAArnott
andauthored
Update documentation for CsWin32RunAsBuildTask mode (#1497)
* Doc updates * Update docfx/docs/getting-started.md Co-authored-by: Andrew Arnott <[email protected]> --------- Co-authored-by: Andrew Arnott <[email protected]>
1 parent 1dd744a commit f01e350

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

docfx/docs/getting-started.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,36 @@ To keep CsWin32 generating the referred types you need, add them explicitly to `
121121

122122
### Support for trimming, AOT, and/or disabling the runtime marshaler
123123

124-
Newer .NET runtime versions may fail for CsWin32 generated code when the application project builds with one or both of these properties set:
124+
**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.
125+
126+
If you set one of these flags:
125127

126128
```xml
127129
<PublishAot>true</PublishAot>
128130
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
129131
<PublishTrimmed>true</PublishTrimmed>
130132
```
131133

132-
CsWin32 supports these environments by avoiding code that relies on the runtime marshaler when the `allowMarshaling` setting is disabled in the `NativeMethods.json` file.
134+
Then you have two options:
135+
136+
#### Enable CsWin32RunAsBuildTask
137+
138+
CsWin32 now supports AOT by generating code which relies on `GeneratedComInterface` and `LibraryImport`, which are source generators.
139+
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:
140+
141+
```xml
142+
<CsWin32RunAsBuildTask>true</CsWin32RunAsBuildTask>
143+
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
144+
```
145+
146+
It is also strongly recommended to set DisableRuntimeMarshalling=true as shown here to ensure that the COM source generators handle
147+
all the marshalling correctly. See https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation and
148+
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation for more information on these .NET source
149+
generators.
150+
151+
#### Disable runtime marshalling
152+
153+
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.
133154
For example:
134155

135156
```json

0 commit comments

Comments
 (0)