{"id":13594936,"url":"https://github.com/microsoft/CsWin32","last_synced_at":"2025-04-09T10:32:23.841Z","repository":{"id":37455566,"uuid":"317639384","full_name":"microsoft/CsWin32","owner":"microsoft","description":"A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.","archived":false,"fork":false,"pushed_at":"2024-10-28T16:31:46.000Z","size":5847,"stargazers_count":2094,"open_issues_count":105,"forks_count":89,"subscribers_count":43,"default_branch":"main","last_synced_at":"2024-10-29T15:37:49.863Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/microsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":"SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-01T18:59:20.000Z","updated_at":"2024-10-29T03:29:15.000Z","dependencies_parsed_at":"2023-02-13T03:05:25.006Z","dependency_job_id":"e953b6dd-0341-4041-bc72-c8ffd068f147","html_url":"https://github.com/microsoft/CsWin32","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FCsWin32","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FCsWin32/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FCsWin32/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FCsWin32/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microsoft","download_url":"https://codeload.github.com/microsoft/CsWin32/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248020593,"owners_count":21034459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T16:01:41.129Z","updated_at":"2025-04-09T10:32:18.818Z","avatar_url":"https://github.com/microsoft.png","language":"C#","funding_links":[],"categories":["Content","C\\#","C#","C# #"],"sub_categories":["173. [Microsoft.Windows.CsWin32](https://ignatandrei.github.io/RSCG_Examples/v2/docs/Microsoft.Windows.CsWin32) , in the [WinAPI](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#winapi) category"],"readme":"# C#/Win32 P/Invoke Source Generator\n\n***A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.***\n\n[![NuGet (prerelease)](https://img.shields.io/nuget/vpre/Microsoft.Windows.CsWin32)](https://www.nuget.org/packages/Microsoft.Windows.CsWin32)\n[![NuGet (daily)](https://img.shields.io/badge/nuget-daily-red)](https://dev.azure.com/azure-public/winsdk/_packaging?_a=package\u0026feed=CI%40Local\u0026package=Microsoft.Windows.CsWin32\u0026protocolType=NuGet)\n\n[![Build Status](https://dev.azure.com/azure-public/winsdk/_apis/build/status/microsoft.CsWin32?branchName=main)](https://dev.azure.com/azure-public/winsdk/_build/latest?definitionId=47\u0026branchName=main)\n\n## Features\n\n* Rapidly add P/Invoke methods and supporting types to your C# project.\n* No bulky assemblies to ship alongside your application.\n* `SafeHandle`-types automatically generated.\n* Generates xml documentation based on and links back to docs.microsoft.com\n\n![Animation demonstrating p/invoke code generation](doc/demo.gif)\n\n## Prerequisites\n\nThe .NET 6 SDK or Visual Studio 2019 Update 11 (16.11).\n\nWPF projects have [additional requirements](https://github.com/microsoft/CsWin32/issues/7).\n\nIn addition, some generated code may require use of the C# 9+ language version (`\u003cLangVersion\u003e9\u003c/LangVersion\u003e`) in your project file. See [issue #4](https://github.com/microsoft/CsWin32/issues/4) for more on this.\nNewer is generally better. Use the latest C# language version for the best results, regardless of your TargetFramework.\n\n## Usage\n\nInstall the `Microsoft.Windows.CsWin32` package:\n\n```ps1\ndotnet add package Microsoft.Windows.CsWin32 --prerelease\n```\n\nYou should also install the `System.Memory` and `System.Runtime.CompilerServices.Unsafe` packages when targeting .NET Framework 4.5+ or .NET Standard 2.0,\nas these add APIs that significantly improve much of the code generated by CsWin32:\n\n```ps1\ndotnet add package System.Memory\ndotnet add package System.Runtime.CompilerServices.Unsafe\n```\n\nProjects targeting .NET Core 2.1+ or .NET 5+ do *not* need to add these package references,\nalthough it is harmless to do so.\n\nNote that while the `System.Memory` package depends on the `System.Runtime.CompilerServices.Unsafe` package,\nreferencing the latter directly is still important to get the latest version of the APIs it provides.\n\nYour project must allow unsafe code to support the generated code that will likely use pointers.\nThis does *not* automatically make all your code *unsafe*.\nUse of the `unsafe` keyword is required anywhere you use pointers.\nThe source generator NuGet package sets the default value of the `AllowUnsafeBlocks` property for your project to `true`,\nbut if you explicitly set it to `false` in your project file, generated code may produce compiler errors.\n\nCreate a `NativeMethods.txt` file in your project directory that lists the APIs to generate code for.\nEach line may consist of *one* of the following:\n\n* 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.\n* 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.\n* 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).\n* Module name followed by `.*` to generate all methods exported from that module (e.g. `Kernel32.*`).\n* 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.\n* A prefix shared by many constants, followed by `*`, to generate all constants that share that prefix (e.g. `ALG_SID_MD*`).\n* A comment (i.e. any line starting with `//`) or white space line, which will be ignored.\n\nWhen generating any type or member, all supporting types will also be generated.\n\nGenerated code is added directly in the compiler.\nAn 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.\n\nAssuming 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:\n\n```cs\nusing Windows.Win32;\n\nPInvoke.CreateFile(/*args*/);\n```\n\nConstants are defined on the same class as the p/invoke methods (by default, the `Windows.Win32.PInvoke` class).\n\nOther supporting types are defined within or under the `Windows.Win32` namespace.\nDiscovery 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.\n\nA project may include many NativeMethods.txt files (each one necessarily in its own directory).\nCsWin32 will read them all to generate APIs, provided these files are included as `AdditionalFiles` in the project.\nA `NativeMethods.txt` file directly in the project directory is added automatically to `AdditionalFiles`.\nFiles in other directories must be added to the project file manually.\n\nWhether API requests are all in a single NativeMethods.txt file or split across many makes no difference to the generated result.\nWe recommend using just one NativeMethods.txt file and keeping it sorted for easy bookkeeping.\nMultiple 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.\n\nSome APIs require targeting a specific architecture and are not available when your C# project compiles as \"Any CPU\".\nLearn more about [how this manifests and what your options are](doc/ArchSpecificAPIs.md).\n\n### Customizing generated code\n\nSeveral aspects of the generated code can be customized, including:\n\n* The name of the class(es) that declare p/invoke methods\n* Whether to emit interop types as `public` or `internal`\n* Whether to emit ANSI functions as well where Wide character functions also exist\n* Set `PreserveSig` for COM interfaces or individual members\n* Force generation of blittable structs, COM structs instead of interfaces (for super high performance with 0 GC pressure), etc.\n\nTo configure these settings, create a `NativeMethods.json` file in your project directory.\nSpecifying 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.\n\n```json\n{\n  \"$schema\": \"https://aka.ms/CsWin32.schema.json\",\n  \"emitSingleFile\": false\n}\n```\n\nMost generated types include the `partial` modifier so you can add your own members to that type within your code.\n\nWhen you need to *replace* a generated type, simply copy and paste it from generated code into your own source files\nand remove the `partial` modifier.\nBe sure to keep the name and namespace exactly the same.\nCsWin32 will notice that your project already declares the type and skip generating it, but generate everything else.\nNote that if that type is the only thing that references some other generated type, CsWin32 will stop generating that type too.\nTo keep CsWin32 generating the referred types you need, add them explicitly to `NativeMethods.txt`.\n\n### Support for trimming, AOT, and/or disabling the runtime marshaler\n\nNewer .NET runtime versions may fail for CsWin32 generated code when the application project builds with one or both of these properties set:\n\n```xml\n\u003cPublishAot\u003etrue\u003c/PublishAot\u003e\n\u003cDisableRuntimeMarshalling\u003etrue\u003c/DisableRuntimeMarshalling\u003e\n\u003cPublishTrimmed\u003etrue\u003c/PublishTrimmed\u003e\n```\n\nCsWin32 supports these environments by avoiding code that relies on the runtime marshaler when the `allowMarshaling` setting is disabled in the `NativeMethods.json` file.\nFor example:\n\n```json\n{\n  \"$schema\": \"https://aka.ms/CsWin32.schema.json\",\n  \"allowMarshaling\": false\n}\n```\n\n### Newer metadata\n\nTo update the metadata used as the source for code generation, you may install a newer `Microsoft.Windows.SDK.Win32Metadata` package:\n\n```ps1\ndotnet add package Microsoft.Windows.SDK.Win32Metadata --prerelease\n```\n\nCsWin32 also consumes the WDK from a similarly named package: `Microsoft.Windows.WDK.Win32Metadata`.\n\n## Consuming daily builds\n\nCan't wait for the next release to try out a bug fix? Follow these steps to consume directly from our daily build.\n\nJust add this package feed to your nuget.config file:\n\n   ```xml\n   \u003cadd key=\"winsdk\" value=\"https://pkgs.dev.azure.com/azure-public/winsdk/_packaging/CI/nuget/v3/index.json\" /\u003e\n   ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoft%2FCsWin32","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicrosoft%2FCsWin32","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoft%2FCsWin32/lists"}