{"id":46992828,"url":"https://github.com/cheatoid/nativeinvoke","last_synced_at":"2026-04-01T22:48:00.485Z","repository":{"id":343487788,"uuid":"1177320989","full_name":"Cheatoid/NativeInvoke","owner":"Cheatoid","description":"Modern, interface-based, generics-capable, source-generated P/Invoke generator for .NET","archived":false,"fork":false,"pushed_at":"2026-03-13T18:28:39.000Z","size":344,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-14T02:09:00.624Z","etag":null,"topics":["csharp","csharp-sourcegenerator","dotnet","interop","pinvoke","source-generator","sourcegenerator"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/NativeInvoke","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/Cheatoid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-09T23:12:37.000Z","updated_at":"2026-03-13T18:28:43.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/Cheatoid/NativeInvoke","commit_stats":null,"previous_names":["cheatoid/nativeinvoke"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Cheatoid/NativeInvoke","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cheatoid%2FNativeInvoke","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cheatoid%2FNativeInvoke/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cheatoid%2FNativeInvoke/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cheatoid%2FNativeInvoke/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cheatoid","download_url":"https://codeload.github.com/Cheatoid/NativeInvoke/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cheatoid%2FNativeInvoke/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30510950,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-14T14:51:43.155Z","status":"ssl_error","status_checked_at":"2026-03-14T14:47:41.964Z","response_time":57,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["csharp","csharp-sourcegenerator","dotnet","interop","pinvoke","source-generator","sourcegenerator"],"created_at":"2026-03-11T14:01:09.005Z","updated_at":"2026-04-01T22:48:00.467Z","avatar_url":"https://github.com/Cheatoid.png","language":"C#","readme":"[\u003cimg width=\"100%\" alt=\"NativeInvoke banner\" src=\"https://raw.github.com/Cheatoid/NativeInvoke/main/NativeInvoke.jpg\" /\u003e](https://github.com/Cheatoid/NativeInvoke) [\u003cimg src=\"https://capsule-render.vercel.app/api?type=waving\u0026color=0:00f7ff,100:ff00e6\u0026height=150\u0026section=header\u0026text=🌟+ᑎᗩTIᐯEIᑎᐯOKE+✨\u0026fontSize=40\u0026fontColor=ffffff\" width=\"100%\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n\u003cdiv align=\"center\"\u003e\n\n[![Stars](https://img.shields.io/github/stars/Cheatoid/NativeInvoke?style=flat-square\u0026color=0088aa\u0026labelColor=001122\u0026logoColor=ffffff)](https://github.com/Cheatoid/NativeInvoke/stargazers) \n[![Issues](https://img.shields.io/github/issues/Cheatoid/NativeInvoke?style=flat-square\u0026color=cc5500\u0026labelColor=aa4400)](https://github.com/Cheatoid/NativeInvoke/issues) \n[![Tests](https://img.shields.io/github/actions/workflow/status/Cheatoid/NativeInvoke/build-test.yml?style=flat-square\u0026color=0d591f\u0026labelColor=0d280f\u0026label=tests)](https://github.com/Cheatoid/NativeInvoke/actions/workflows/build-test.yml) \n[![License](https://img.shields.io/github/license/Cheatoid/NativeInvoke?style=flat-square\u0026color=ff00e6\u0026labelColor=2b001f)](https://github.com/Cheatoid/NativeInvoke/blob/main/LICENSE) \n[![Discord](https://img.shields.io/badge/Discord-Join-2c2f33?style=flat-square\u0026logo=discord\u0026logoColor=white\u0026labelColor=5865f2)](https://discord.gg/FVCeYxwWtB)  \n[![NativeInvoke](https://readme-typing-svg.herokuapp.com?font=Fira+Code\u0026duration=5000\u0026pause=2500\u0026size=32\u0026color=00F7FF\u0026center=true\u0026vCenter=true\u0026width=800\u0026lines=hello,+stranger;welcome+to+NativeInvoke+codebase;made+with+%E2%9D%A4%EF%B8%8F+by+Cheatoid)](https://github.com/Cheatoid/NativeInvoke)\n\u003c/div\u003e\n\n### *High-performance, source-generated P/Invoke*\n\nNativeInvoke is a modern, zero-overhead, generics-capable P/Invoke generator for .NET.  \nIt uses Roslyn source generation to enforce **blittable**, **function-pointer based**, **lazy-loaded** native bindings - without the runtime overhead of `DllImport`.\n\nYou write clean interfaces.  \nNativeInvoke generates the unsafe bits for you at compile-time.  \n- Cross-platform and AOT/JIT-friendly.\n- No `DllImport`.\n- No delegate allocation (no pinning).\n- No runtime dependencies.\n- No marshalling.\n- No reflection.\n- No dynamic codegen (dynamic IL).\n- Just pure compile-time generation glue.\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 🚀 Quick Installation\n\nInstall the NuGet package:\n```bash\ndotnet add package NativeInvoke\n```\n\nOr edit your `.csproj` to always stay up-to-date (followed by `dotnet restore --no-cache`):\n```xml\n\u003cItemGroup\u003e\n    \u003cPackageReference Include=\"NativeInvoke\" Version=\"*\"/\u003e\n\u003c/ItemGroup\u003e\n```\n\nHow floating versions work:\n- `*-*`: Latest version including pre-releases (e.g., `1.1.0-beta.1`, `2.0.0-alpha.2`)\n- `*`: Latest stable version only\n- `1.*`: Latest stable version with major version 1\n- `1.2.*`: Latest stable version with major.minor 1.2\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 🧠 Why NativeInvoke?\n\n| Feature                   | Benefit                                 |\n|---------------------------|-----------------------------------------|\n| **Source-generated**      | Zero runtime overhead                   |\n| **Function pointers**     | Faster than `DllImport`                 |\n| **Lazy-loading support**  | Load symbols/functions only when needed |\n| **Interface-based**       | Fully mockable for testing              |\n| **Generics support**      | Use generics in P/Invoke                |\n| **No static pollution**   | Clean public API surface                |\n| **.NET 9 `Lock` support** | Modern, allocation-free synchronization |\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 🛠 Requirements\n\n- C# 14 / .NET 9 or later\n- Unsafe code enabled (`\u003cAllowUnsafeBlocks\u003etrue\u003c/AllowUnsafeBlocks\u003e`)\n- Roslyn source generators enabled (default in SDK-style projects)\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## ✨ Example Usage\n\n\u003e 📚 [**See Attribute docs**](https://github.com/Cheatoid/NativeInvoke/blob/main/NativeInvoke/NativeImportAttribute.cs).  \n\nCheckout the [**full Example project**](https://github.com/Cheatoid/NativeInvoke/tree/main/Example) for more!  \n\n\u003cdetails\u003e\n\n\u003csummary\u003eClick here to toggle an example for \u003ca href=\"https://learn.microsoft.com/en-us/windows/win32/api/utilapiset/nf-utilapiset-beep\"\u003eplaying a \u003ci\u003ebeep\u003c/i\u003e sound on Windows platform\u003c/a\u003e (a.k.a. \u003ccode\u003eSystem.Console.Beep\u003c/code\u003e)\u003c/summary\u003e\n\n### 1. Define your native interface\n\n```csharp\nglobal using NativeInvoke; // Import our attributes in your project\nglobal using NIMA = NativeInvoke.NativeImportMethodAttribute;\n\nusing BOOL = int; // Win32 BOOL is 4-bytes (0=false, 1=true)\nusing DWORD = uint; // double-word\n\n#if NET6_0_OR_GREATER\n[System.Runtime.Versioning.SupportedOSPlatform(\"windows\")] // Optional (for clarity)\n#endif\npublic interface IKernel32\u003cTBool\u003e // Generics are supported!\n  where TBool : unmanaged\n{\n  [NIMA(\"Beep\")] // Optional; Use this attribute if you want to load a different name/ordinal,\n                 // or override a calling convention per function (defaults to platform-specific).\n  TBool Boop(DWORD frequency, DWORD duration);\n\n  [NIMA(null)] // Use null or empty string to skip generation; could also omit this and set ExplicitOnly=true\n  void IgnoreMe();\n}\n```\n\n### 2. Expose it via a `static partial` property\n\nThe property can be nested anywhere you want (class/struct/interface/record), and you can use any accessibility level you need - the generator will match your declaration.\n\n```csharp\npublic static partial class Win32\n{\n  private const string kernel32 = \"kernel32\";\n  [NativeImport(\n    kernel32 // Specify native library name\n    , EnforceBlittable = true // Whether to enforce blittable type validation (applies to all methods, can be overriden per-method)\n    , ExplicitOnly = false // Whether only methods explicitly marked with NIMA should be considered\n    , Inherited = true // Whether to consider inherited interface methods\n    , Lazy = false // Whether to use lazy or eager module loading\n    , CallingConvention = CallingConvention.StdCall // Define the default calling convention (default is platform-specific, applies to all methods, can be overriden per-method)\n    , SuppressGCTransition = false // Whether to suppress the GC transition (applies to all methods, can be overriden per-method)\n    , SymbolPrefix = \"\" // Define common prefix (prepended to method name unless using explicit entry point)\n    , SymbolSuffix = \"\" // Define common suffix (appended to method name unless using explicit entry point)\n  )]\n  public static partial IKernel32\u003cBOOL\u003e Kernel32 { get; }\n}\n```\n\n### 3. Call it like a normal .NET API\n\n```csharp\nWin32.Kernel32.Boop(600u, 300u);\n```\n\nUnder the hood, NativeInvoke generates:\n\n- A nested sealed `__Impl` class implementing your (generic) interface\n- Static (readonly) function pointer fields (`delegate* unmanaged`)\n- Lazy or eager symbol resolution (`NativeLibrary`)\n- A clean property implementation using the `field` keyword\n- Thread-safe lazy initialization using .NET 9 `Lock` type\n\nAll without touching your container type.\n\n\u003c/details\u003e\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 💡 Future/Experiments (ToDo list)\n\n- [ ] Support C# 9 / .NET 5 and later via `#if`; current source generator is relying on C# 14 features and .NET 9 API\n- [ ] Add support for loading symbol from numeric ordinal\n- [x] ~~Implement default symbol name prefix and suffix~~\n- [x] ~~Add `EnforceBlittable` and `ExplicitOnly` flags~~\n- [ ] Switch to `[UnmanagedCallConv]`/`typeof(CallConv*)` for future-proofed calling conventions (MemberFunction, Swift, etc.)\n- [x] ~~Format generated code using Roslyn, or use `IndentedTextWriter` for source-code generation~~\n- [x] ~~Append `Guid` to generated fields (to prevent name collisions for overloaded functions)~~\n- [x] ~~Make unit tests~~\n- [ ] Auto-generate proper page for docs and examples (maybe use GitHub io page or wiki)\n- [ ] Explore micro-optimization: IL weaver via `Fody`, replace interface dispatch and `DllImport` calls with `calli`\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 🙏 Contributing\n\nPRs, issues, and ideas are welcome.  \nNativeInvoke is built for developers who want **maximum performance** without sacrificing **clean API design**.\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 💖 Support\n\nIf you like this or you are using this in your project, consider:\n- [Becoming a ⭐](https://github.com/Cheatoid/NativeInvoke/stargazers) 🤩\n- Spreading the word\n\n[\u003cimg width=\"100%\" alt=\"separator\" src=\"https://raw.github.com/Cheatoid/gh_assets/_/images/rainbow-separator.png\" /\u003e](https://github.com/Cheatoid/NativeInvoke)\n\n## 📄 License\n\n[MIT](https://github.com/Cheatoid/NativeInvoke/blob/main/LICENSE) - do whatever you want, just don't blame me if you `calli` into oblivion.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheatoid%2Fnativeinvoke","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheatoid%2Fnativeinvoke","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheatoid%2Fnativeinvoke/lists"}