Commit 8db2cfe
committed
Migrate Windows interop to CsWin32 source-generated P/Invoke
Replace hand-written [DllImport] declarations and COM [ComImport]
interfaces with CsWin32 source-generated interop. This enables
Native AOT scenarios where built-in COM marshalling and
System.Runtime.InteropServices marshalling infrastructure do not
exist. The approach follows patterns established in WinForms, WPF,
and the dotnet/sdk repository.
CsWin32 is configured in Microsoft.Build.Framework with
allowMarshaling: false and useSafeHandles: false, producing raw
pointer signatures that are AOT-compatible. Other projects consume
generated types via InternalsVisibleTo.
Key changes:
Infrastructure:
- Add FEATURE_WINDOWSINTEROP compile-time gate in
Directory.BeforeCommon.targets, disabled for source-only builds
- Add CsWin32 (Microsoft.Windows.CsWin32) and PolySharp package
references to Framework.csproj
- Add NativeMethods.txt and NativeMethods.json for CsWin32
configuration
- Add .editorconfig suppression for CS3019 in Windows/ folders
- Add cswin32-interop skill document for agent guidance
New utility types:
- BufferScope<T>: ref struct for stack-allocated buffers with
ArrayPool fallback, replacing manual ArrayPool rent/return and
stackalloc patterns throughout interop code
- TypeInfo<T>: cached RuntimeHelpers.IsReferenceOrContainsReferences
polyfill for net472
- ComScope<T>: COM pointer lifetime management (using-disposable)
- ComClassFactory: AOT-compatible COM activation without
Activator.CreateInstance
- IID: generic IID lookup via IComIID interface
- VARIANT, BSTR, HRESULT, FILETIME extensions: CsWin32 partial
type augmentations for safe usage patterns
NativeMethods.cs cleanup:
- Delete ~130 hand-written constants, enums, structs, and
[DllImport] declarations replaced by CsWin32 typed equivalents
- Replace MemoryStatus class with MEMORYSTATUSEX struct via
TryGetMemoryStatus
- Replace GetCurrentDirectory/GetFullPath/GetShortPathName/
GetLongPathName with BufferScope-based implementations
- Replace StreamHandleType enum with bool useStandardError parameter
- Delete DirectoryExists/FileExists/FileOrDirectoryExists wrappers;
callers use PInvoke.GetFileAttributes directly
- Delete HResultSucceeded/HResultFailed; callers use HRESULT.Failed
- Add [UnsupportedOSPlatformGuard("windows")] to IsUnixLike
- Version all [SupportedOSPlatform] attributes to "windows6.1"
COM interop modernization:
- Replace [ComImport] WMI interfaces (IWbemLocator, IWbemServices,
IEnumWbemClassObject, IWbemClassObject) with struct-based COM
using delegate* unmanaged[Stdcall] vtables
- Add IDebugClient4-based command line retrieval as alternative to
WMI (CommandLineSource.DebugEngine)
- Move IFixedTypeInfo from NativeMethods.cs to its own file in Tasks
Platform guard pattern:
- Apply dual-guard pattern: #if FEATURE_WINDOWSINTEROP wraps runtime
IsWindows checks, eliminating dead code in source builds
- Use IsUnixLike (positive guard) instead of !IsWindows for CA1416
platform compatibility analysis
- Files excluded at project level via <Compile Remove> when
FeatureWindowsInterop is off (WindowsFileSystem, Windows/ folder,
WMI structs)
Behavioral changes:
- Unzip.cs: !IsWindows → IsUnixLike for correct CA1416 analysis
- ProcessExtensions: ArrayPool<T> replaced with BufferScope<T>
- AssemblyInformation/GlobalAssemblyCache: remove dead non-Windows
code paths in net472-only (#if !FEATURE_ASSEMBLYLOADCONTEXT)
blocks, which only run on Windows
Some related changes are also in dotnet#13426.1 parent 97e3065 commit 8db2cfe
51 files changed
Lines changed: 3197 additions & 1463 deletions
File tree
- .github/skills/cswin32-interop
- eng/dependabot
- src
- Build.UnitTests
- BackEnd
- Build
- BackEnd/Components
- BuildRequestEngine
- Communications
- Logging
- Framework.UnitTests
- Framework
- FileSystem
- Framework/Windows/Win32
- Telemetry
- Utilities
- Wmi
- Windows/Win32
- Foundation
- System
- Com
- Variant
- Tasks.UnitTests
- Tasks
- AssemblyDependency
- Utilities.UnitTests
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
66 | 75 | | |
67 | 76 | | |
68 | 77 | | |
| |||
Lines changed: 28 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| |||
960 | 963 | | |
961 | 964 | | |
962 | 965 | | |
963 | | - | |
| 966 | + | |
964 | 967 | | |
965 | 968 | | |
966 | 969 | | |
| |||
971 | 974 | | |
972 | 975 | | |
973 | 976 | | |
974 | | - | |
| 977 | + | |
975 | 978 | | |
976 | 979 | | |
977 | 980 | | |
| |||
982 | 985 | | |
983 | 986 | | |
984 | 987 | | |
985 | | - | |
| 988 | + | |
986 | 989 | | |
987 | 990 | | |
988 | 991 | | |
| |||
993 | 996 | | |
994 | 997 | | |
995 | 998 | | |
996 | | - | |
| 999 | + | |
997 | 1000 | | |
998 | 1001 | | |
999 | 1002 | | |
| |||
1004 | 1007 | | |
1005 | 1008 | | |
1006 | 1009 | | |
1007 | | - | |
| 1010 | + | |
1008 | 1011 | | |
1009 | 1012 | | |
1010 | 1013 | | |
1011 | | - | |
| 1014 | + | |
1012 | 1015 | | |
1013 | 1016 | | |
1014 | 1017 | | |
1015 | | - | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
1016 | 1030 | | |
1017 | 1031 | | |
1018 | 1032 | | |
| |||
1038 | 1052 | | |
1039 | 1053 | | |
1040 | 1054 | | |
1041 | | - | |
1042 | | - | |
1043 | | - | |
1044 | | - | |
1045 | | - | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
1046 | 1062 | | |
1047 | 1063 | | |
1048 | 1064 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
| |||
1959 | 1965 | | |
1960 | 1966 | | |
1961 | 1967 | | |
1962 | | - | |
| 1968 | + | |
1963 | 1969 | | |
1964 | 1970 | | |
1965 | 1971 | | |
1966 | | - | |
| 1972 | + | |
1967 | 1973 | | |
1968 | | - | |
| 1974 | + | |
1969 | 1975 | | |
1970 | | - | |
1971 | | - | |
1972 | 1976 | | |
1973 | | - | |
| 1977 | + | |
1974 | 1978 | | |
1975 | 1979 | | |
1976 | 1980 | | |
| |||
Lines changed: 15 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
885 | 888 | | |
886 | 889 | | |
887 | 890 | | |
| 891 | + | |
888 | 892 | | |
889 | 893 | | |
890 | 894 | | |
| |||
901 | 905 | | |
902 | 906 | | |
903 | 907 | | |
904 | | - | |
905 | | - | |
| 908 | + | |
906 | 909 | | |
907 | 910 | | |
908 | 911 | | |
909 | 912 | | |
910 | 913 | | |
911 | 914 | | |
912 | | - | |
| 915 | + | |
913 | 916 | | |
914 | 917 | | |
915 | | - | |
| 918 | + | |
916 | 919 | | |
917 | 920 | | |
918 | 921 | | |
| |||
936 | 939 | | |
937 | 940 | | |
938 | 941 | | |
939 | | - | |
940 | | - | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
941 | 949 | | |
942 | 950 | | |
943 | 951 | | |
| |||
949 | 957 | | |
950 | 958 | | |
951 | 959 | | |
| 960 | + | |
952 | 961 | | |
953 | 962 | | |
954 | 963 | | |
| |||
Lines changed: 20 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
| |||
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| 69 | + | |
65 | 70 | | |
66 | 71 | | |
67 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
68 | 77 | | |
69 | 78 | | |
70 | 79 | | |
| |||
156 | 165 | | |
157 | 166 | | |
158 | 167 | | |
159 | | - | |
| 168 | + | |
| 169 | + | |
160 | 170 | | |
161 | 171 | | |
162 | 172 | | |
| |||
227 | 237 | | |
228 | 238 | | |
229 | 239 | | |
230 | | - | |
| 240 | + | |
| 241 | + | |
231 | 242 | | |
232 | | - | |
| 243 | + | |
233 | 244 | | |
234 | 245 | | |
235 | | - | |
| 246 | + | |
236 | 247 | | |
237 | | - | |
| 248 | + | |
238 | 249 | | |
| 250 | + | |
239 | 251 | | |
240 | 252 | | |
| 253 | + | |
241 | 254 | | |
242 | | - | |
| 255 | + | |
243 | 256 | | |
244 | 257 | | |
245 | 258 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
7 | 13 | | |
8 | 14 | | |
9 | 15 | | |
| |||
24 | 30 | | |
25 | 31 | | |
26 | 32 | | |
| 33 | + | |
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
30 | 37 | | |
31 | | - | |
32 | | - | |
| 38 | + | |
| 39 | + | |
33 | 40 | | |
34 | | - | |
| 41 | + | |
35 | 42 | | |
36 | 43 | | |
37 | 44 | | |
| |||
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
| 50 | + | |
43 | 51 | | |
44 | 52 | | |
45 | 53 | | |
| |||
75 | 83 | | |
76 | 84 | | |
77 | 85 | | |
| 86 | + | |
78 | 87 | | |
79 | 88 | | |
80 | 89 | | |
81 | | - | |
| 90 | + | |
82 | 91 | | |
83 | | - | |
| 92 | + | |
84 | 93 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 94 | + | |
89 | 95 | | |
90 | 96 | | |
91 | 97 | | |
| 98 | + | |
92 | 99 | | |
93 | 100 | | |
94 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
0 commit comments