dotnet-native-interop

Generate C# P/Invoke signatures for calling native C/C++ libraries.

71|10|Updated Feb 11, 2026
One-click install
npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-native-interop-wshaddix
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-native-interop
Source: https://github.com/wshaddix/dotnet-skills/tree/main/skills/dotnet-native-interop
Command: npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-native-interop-wshaddix

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you safely and efficiently call native C/C++ libraries from your .NET applications, overcoming the complexities of cross-platform compatibility and data marshalling.

Core Features & Use Cases

  • Modern P/Invoke: Utilize [LibraryImport] for AOT-compatible, compile-time marshalling.
  • Cross-Platform Resolution: Handle different library names and paths across Windows, macOS, and Linux.
  • Safe Resource Management: Employ SafeHandle to prevent memory leaks.
  • Use Case: Integrate a high-performance native image processing library into your .NET MAUI application, ensuring it works seamlessly on both Windows and macOS.

Quick Start

Use the dotnet-native-interop skill to generate a C# signature for calling the process_data function from mylib.dll which accepts a UTF-8 string and returns an integer.

Frequently Asked Questions about dotnet-native-interop

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call C/C++ libraries from C# using P/Invoke?

To call C/C++ libraries from C# using P/Invoke, you define static external methods with the [LibraryImport] or [DllImport] attribute, specifying the native library and function signatures. This enables your .NET application to invoke unmanaged code directly.

What is the difference between [LibraryImport] and [DllImport] in .NET?

[LibraryImport] is the modern .NET 7+ attribute that uses source generation for AOT-compatible marshalling at compile time, whereas [DllImport] is the legacy approach relying on runtime marshalling. [LibraryImport] improves performance and supports ahead-of-time compilation scenarios.

How do I resolve native library paths across Windows, macOS, and Linux in .NET?

You resolve native library paths across Windows, macOS, and Linux in .NET by using NativeLibrary.SetDllImportResolver. This allows custom runtime logic to find and load the correct platform-specific binary, handling different naming conventions and directory structures.

How do I safely manage native memory and prevent leaks when using native interop?

You safely manage native memory and prevent leaks in native interop by wrapping unmanaged resources with SafeHandle. This ensures deterministic cleanup of native memory allocations when the .NET garbage collector collects the wrapper object.

Does .NET native interop support AOT compilation for iOS and Android?

Yes, .NET native interop supports AOT compilation for iOS and Android by using the [LibraryImport] attribute. This source-generated approach ensures marshalling code is created at compile time, avoiding reflection dependencies incompatible with AOT environments.