|
2 | 2 |
|
3 | 3 | ***A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.***
|
4 | 4 |
|
5 |
| -[](https://www.nuget.org/packages/Microsoft.Windows.CsWin32) |
| 5 | +[](https://www.nuget.org/packages/Microsoft.Windows.CsWin32) |
6 | 6 | [](https://dev.azure.com/azure-public/winsdk/_packaging?_a=package&feed=CI%40Local&package=Microsoft.Windows.CsWin32&protocolType=NuGet)
|
7 | 7 |
|
8 | 8 | [](https://dev.azure.com/azure-public/winsdk/_build/latest?definitionId=47&branchName=main)
|
|
14 | 14 | * `SafeHandle`-types automatically generated.
|
15 | 15 | * Generates xml documentation based on and links back to docs.microsoft.com
|
16 | 16 |
|
17 |
| - |
18 |
| - |
19 |
| -## Prerequisites |
20 |
| - |
21 |
| -The .NET 6 SDK or Visual Studio 2019 Update 11 (16.11). |
22 |
| - |
23 |
| -WPF projects have [additional requirements](https://github.com/microsoft/CsWin32/issues/7). |
24 |
| - |
25 |
| -In addition, some generated code may require use of the C# 9+ language version (`<LangVersion>9</LangVersion>`) in your project file. See [issue #4](https://github.com/microsoft/CsWin32/issues/4) for more on this. |
26 |
| -Newer is generally better. Use the latest C# language version for the best results, regardless of your TargetFramework. |
| 17 | + |
27 | 18 |
|
28 | 19 | ## Usage
|
29 | 20 |
|
30 |
| -Install the `Microsoft.Windows.CsWin32` package: |
31 |
| - |
32 |
| -```ps1 |
33 |
| -dotnet add package Microsoft.Windows.CsWin32 --prerelease |
34 |
| -``` |
35 |
| - |
36 |
| -You should also install the `System.Memory` and `System.Runtime.CompilerServices.Unsafe` packages when targeting .NET Framework 4.5+ or .NET Standard 2.0, |
37 |
| -as these add APIs that significantly improve much of the code generated by CsWin32: |
38 |
| - |
39 |
| -```ps1 |
40 |
| -dotnet add package System.Memory |
41 |
| -dotnet add package System.Runtime.CompilerServices.Unsafe |
42 |
| -``` |
43 |
| - |
44 |
| -Projects targeting .NET Core 2.1+ or .NET 5+ do *not* need to add these package references, |
45 |
| -although it is harmless to do so. |
46 |
| - |
47 |
| -Note that while the `System.Memory` package depends on the `System.Runtime.CompilerServices.Unsafe` package, |
48 |
| -referencing the latter directly is still important to get the latest version of the APIs it provides. |
49 |
| - |
50 |
| -Your project must allow unsafe code to support the generated code that will likely use pointers. |
51 |
| -This does *not* automatically make all your code *unsafe*. |
52 |
| -Use of the `unsafe` keyword is required anywhere you use pointers. |
53 |
| -The source generator NuGet package sets the default value of the `AllowUnsafeBlocks` property for your project to `true`, |
54 |
| -but if you explicitly set it to `false` in your project file, generated code may produce compiler errors. |
55 |
| - |
56 |
| -Create a `NativeMethods.txt` file in your project directory that lists the APIs to generate code for. |
57 |
| -Each line may consist of *one* of the following: |
58 |
| - |
59 |
| -* Exported method name (e.g. `CreateFile`). This *may* include the `A` or `W` suffix, where applicable. This *may* be qualified with a namespace but is only recommended in cases of ambiguity, which CsWin32 will prompt where appropriate. |
60 |
| -* A macro name (e.g. `HRESULT_FROM_WIN32`). These are generated into the same class with extern methods. Macros must be hand-authored into CsWin32, so let us know if you want to see a macro added. |
61 |
| -* A namespace to generate all APIs from (e.g. `Windows.Win32.Storage.FileSystem` would search the metadata for all APIs within that namespace and generate them). |
62 |
| -* Module name followed by `.*` to generate all methods exported from that module (e.g. `Kernel32.*`). |
63 |
| -* The name of a struct, enum, constant or interface to generate. This *may* be qualified with a namespace but is only recommended in cases of ambiguity, which CsWin32 will prompt where appropriate. |
64 |
| -* A prefix shared by many constants, followed by `*`, to generate all constants that share that prefix (e.g. `ALG_SID_MD*`). |
65 |
| -* A comment (i.e. any line starting with `//`) or white space line, which will be ignored. |
66 |
| - |
67 |
| -When generating any type or member, all supporting types will also be generated. |
68 |
| - |
69 |
| -Generated code is added directly in the compiler. |
70 |
| -An IDE may make this generated code available to view through code navigation commands (e.g. Go to Definition) or a tree view of source files that include generated source files. |
71 |
| - |
72 |
| -Assuming default settings and a `NativeMethods.txt` file with content that includes `CreateFile`, the P/Invoke methods can be found on the `Windows.Win32.PInvoke` class, like this: |
73 |
| - |
74 |
| -```cs |
75 |
| -using Windows.Win32; |
76 |
| - |
77 |
| -PInvoke.CreateFile(/*args*/); |
78 |
| -``` |
79 |
| - |
80 |
| -Constants are defined on the same class as the p/invoke methods (by default, the `Windows.Win32.PInvoke` class). |
81 |
| - |
82 |
| -Other supporting types are defined within or under the `Windows.Win32` namespace. |
83 |
| -Discovery of the namespace for a given type can be done with the Go To All feature (Ctrl+T) in Visual Studio with the type name as the search query. |
84 |
| - |
85 |
| -A project may include many NativeMethods.txt files (each one necessarily in its own directory). |
86 |
| -CsWin32 will read them all to generate APIs, provided these files are included as `AdditionalFiles` in the project. |
87 |
| -A `NativeMethods.txt` file directly in the project directory is added automatically to `AdditionalFiles`. |
88 |
| -Files in other directories must be added to the project file manually. |
89 |
| - |
90 |
| -Whether API requests are all in a single NativeMethods.txt file or split across many makes no difference to the generated result. |
91 |
| -We recommend using just one NativeMethods.txt file and keeping it sorted for easy bookkeeping. |
92 |
| -Multiple files perhaps makes the most sense in a Shared Project scenario where several API requests will be common across many projects, so sharing a NativeMethods.txt file with those same projects that contain all the necessary APIs for the set of shared source files make maintenance easier. |
93 |
| - |
94 |
| -Some APIs require targeting a specific architecture and are not available when your C# project compiles as "Any CPU". |
95 |
| -Learn more about [how this manifests and what your options are](doc/ArchSpecificAPIs.md). |
96 |
| - |
97 |
| -### Customizing generated code |
98 |
| - |
99 |
| -Several aspects of the generated code can be customized, including: |
100 |
| - |
101 |
| -* The name of the class(es) that declare p/invoke methods |
102 |
| -* Whether to emit interop types as `public` or `internal` |
103 |
| -* Whether to emit ANSI functions as well where Wide character functions also exist |
104 |
| -* Set `PreserveSig` for COM interfaces or individual members |
105 |
| -* Force generation of blittable structs, COM structs instead of interfaces (for super high performance with 0 GC pressure), etc. |
106 |
| - |
107 |
| -To configure these settings, create a `NativeMethods.json` file in your project directory. |
108 |
| -Specifying the `$schema` property that points to [the schema](src/Microsoft.Windows.CsWin32/settings.schema.json) adds completions, descriptions and validation in many JSON editors, and in fact is where all the documentation for the available settings is found. |
109 |
| - |
110 |
| -```json |
111 |
| -{ |
112 |
| - "$schema": "https://aka.ms/CsWin32.schema.json", |
113 |
| - "emitSingleFile": false |
114 |
| -} |
115 |
| -``` |
116 |
| - |
117 |
| -Most generated types include the `partial` modifier so you can add your own members to that type within your code. |
118 |
| - |
119 |
| -When you need to *replace* a generated type, simply copy and paste it from generated code into your own source files |
120 |
| -and remove the `partial` modifier. |
121 |
| -Be sure to keep the name and namespace exactly the same. |
122 |
| -CsWin32 will notice that your project already declares the type and skip generating it, but generate everything else. |
123 |
| -Note that if that type is the only thing that references some other generated type, CsWin32 will stop generating that type too. |
124 |
| -To keep CsWin32 generating the referred types you need, add them explicitly to `NativeMethods.txt`. |
125 |
| - |
126 |
| -### Support for trimming, AOT, and/or disabling the runtime marshaler |
127 |
| - |
128 |
| -Newer .NET runtime versions may fail for CsWin32 generated code when the application project builds with one or both of these properties set: |
129 |
| - |
130 |
| -```xml |
131 |
| -<PublishAot>true</PublishAot> |
132 |
| -<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling> |
133 |
| -<PublishTrimmed>true</PublishTrimmed> |
134 |
| -``` |
135 |
| - |
136 |
| -CsWin32 supports these environments by avoiding code that relies on the runtime marshaler when the `allowMarshaling` setting is disabled in the `NativeMethods.json` file. |
137 |
| -For example: |
138 |
| - |
139 |
| -```json |
140 |
| -{ |
141 |
| - "$schema": "https://aka.ms/CsWin32.schema.json", |
142 |
| - "allowMarshaling": false |
143 |
| -} |
144 |
| -``` |
145 |
| - |
146 |
| -### Newer metadata |
147 |
| - |
148 |
| -To update the metadata used as the source for code generation, you may install a newer `Microsoft.Windows.SDK.Win32Metadata` package: |
149 |
| - |
150 |
| -```ps1 |
151 |
| -dotnet add package Microsoft.Windows.SDK.Win32Metadata --prerelease |
152 |
| -``` |
153 |
| - |
154 |
| -CsWin32 also consumes the WDK from a similarly named package: `Microsoft.Windows.WDK.Win32Metadata`. |
155 |
| - |
156 |
| -## Consuming daily builds |
157 |
| - |
158 |
| -Can't wait for the next release to try out a bug fix? Follow these steps to consume directly from our daily build. |
159 |
| - |
160 |
| -Just add this package feed to your nuget.config file: |
161 |
| - |
162 |
| - ```xml |
163 |
| - <add key="winsdk" value="https://pkgs.dev.azure.com/azure-public/winsdk/_packaging/CI/nuget/v3/index.json" /> |
164 |
| - ``` |
| 21 | +[Check out our product documentation](https://microsoft.github.io/CsWin32/). |
0 commit comments