NativeInvoke 1.0.0

Additional Details

Update to latest version due to improvements and bug fixes.

There is a newer version of this package available.
See the version list below for details.
dotnet add package NativeInvoke --version 1.0.0
                    
NuGet\Install-Package NativeInvoke -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NativeInvoke" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NativeInvoke" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="NativeInvoke" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NativeInvoke --version 1.0.0
                    
#r "nuget: NativeInvoke, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NativeInvoke@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NativeInvoke&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=NativeInvoke&version=1.0.0
                    
Install as a Cake Tool

🌟 NativeInvoke

High-performance, source-generated P/Invoke

NativeInvoke is a modern, zero-overhead P/Invoke generator for .NET.
It uses Roslyn source generation to enforce blittable, function-pointer based, lazy-loaded native bindings - without the runtime overhead of DllImport.

You write clean interfaces.
NativeInvoke generates the unsafe bits.


🚀 Quick Installation

Install the NuGet package:

dotnet add package NativeInvoke

Or edit your .csproj (followed by dotnet restore):

<ItemGroup>
    <PackageReference Include="NativeInvoke" Version="1.0.0"/>
</ItemGroup>

🧠 Why NativeInvoke?

Feature Benefit
Source-generated Zero runtime overhead
Function pointers Faster than DllImport
Lazy-loading support Load symbols/functions only when needed
Interface-based Fully mockable for testing
No static pollution Clean public API surface
.NET 9 Lock support Modern, allocation-free synchronization
  • Cross-platform and AOT/JIT-friendly.
  • No DllImport.
  • No delegate allocation.
  • No runtime dependencies.
  • No marshalling.
  • No reflection.
  • No dynamic codegen (dynamic IL).
  • Just pure compile-time generation glue.

🛠 Requirements

  • C# 14 / .NET 9 or later
  • Unsafe code enabled (<AllowUnsafeBlocks>true</AllowUnsafeBlocks>)
  • Roslyn source generators enabled (default in SDK-style projects)

✨ Example Usage

For example to play a beep sound on Windows (without Console.Beep):

1. Define your native interface

global using NativeInvoke; // to import our attributes in your project

using BOOL = int; // Win32 BOOL is 4-bytes (0=false, 1=true)
using DWORD = uint; // double-word

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")] // Optional (for clarity)
#endif
public interface IKernel32
{
    [NativeImportMethod("Beep")] // Optional; Use this attribute if you want to load a different name/ordinal,
                                 // or override a calling convention per function (defaults to platform-specific).
    BOOL Boop(DWORD frequency, DWORD duration);

    [NativeImportMethod(null)] // Use null or empty string to skip generation.
    void IgnoreMe();
}

2. Expose it via a static partial property

The 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.

public static partial class Win32
{
    // Specify native library name.
    // Optionally set the default calling convention, symbol name prefix, or whether to use lazy loading.
    [NativeImport("kernel32", Lazy = true)]
    public static partial IKernel32 Kernel32 { get; }
}

3. Call it like a normal .NET API

Win32.Kernel32.Boop(600u, 300u);

Under the hood, NativeInvoke generates:

  • A nested sealed __Impl class implementing your interface
  • Static (readonly) function pointer fields (delegate* unmanaged)
  • Lazy or eager symbol resolution (NativeLibrary)
  • A clean property implementation using the field keyword
  • Thread-safe lazy initialization using .NET 9 Lock type

All without touching your container type.


💡 Future/Experiments (ToDo list)

  • Support C# 9 / .NET 5 and later via #if; current source generator is relying on C# 14 features and .NET 9 API
  • Add support for loading symbol from numeric ordinal (ushort)
  • Implement default symbol name prefix
  • Switch to typeof(CallConv*) for future-proofed calling conventions
  • Use IndentedTextWriter for source-code generation
  • Explore micro-optimization: IL weaver via Fody, replace interface dispatch and DllImport calls with calli

🙏 Contributing

PRs, issues, and ideas are welcome.
NativeInvoke is built for developers who want maximum performance without sacrificing clean API design.


💖 Support

If you like this or you are using this in your project, consider:


📄 License

MIT - do whatever you want, just don't blame me if you calli into oblivion.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.