dotnet-pinvoke

Write and review P/Invoke and LibraryImport declarations for calling native C/C++ libraries from .NET.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/D1ssolve/craft-agents --skill dotnet-pinvoke-d1ssolve
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-pinvoke
Source: https://github.com/D1ssolve/craft-agents/tree/main/skills/dotnet-pinvoke
Command: npx skills add https://github.com/D1ssolve/craft-agents --skill dotnet-pinvoke-d1ssolve

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Calling native C/C++ libraries from .NET is error-prone: incorrect signatures, wrong string encodings, and mismatched memory ownership cause crashes, silent data corruption, and memory leaks that surface far from the actual defect. This Skill provides a structured workflow for writing correct P/Invoke and LibraryImport declarations and diagnosing failures at the managed/native boundary. ## Core Features & Use Cases - Declaration authoring: Maps native C/C++ types to correct .NET types (CLong, nuint, SafeHandle) and generates DllImport or LibraryImport declarations based on the target framework. - Memory and string safety: Enforces explicit string encoding, correct allocator pairing, SafeHandle usage for native handles, and delegate rooting for callbacks. - Diagnostics and migration: Provides failure-mode tables for AccessViolationException and DllNotFoundException, plus a step-by-step DllImport-to-LibraryImport migration path for AOT/trimming compatibility. - Use Case: Given a C header for a compression library, generate a complete .NET wrapper with LibraryImport declarations, SafeHandle-based resource management, and UTF-8 string marshalling, then validate it with interop analyzers. ## Quick Start Ask the agent to write a LibraryImport wrapper for the functions declared in your native C header file, targeting .NET 8.

Frequently Asked Questions about dotnet-pinvoke

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

FAQPage Schema
How do I call a C library from .NET using P/Invoke?

Map each native function signature to a DllImport or LibraryImport declaration, matching types exactly (nuint for size_t, CLong for C long). Specify string encoding explicitly and use SafeHandle for native handles instead of raw IntPtr.

Should I use DllImport or LibraryImport in .NET?

Use LibraryImport for new code targeting .NET 7 or later, since it is source-generated and AOT/trim safe. Use DllImport when targeting .NET Framework, and note that LibraryImport is the only option when native AOT is required.

What .NET type maps to C long in P/Invoke?

C long maps to CLong because it is 32-bit on Windows but 64-bit on 64-bit Unix. Never use int or long. With LibraryImport, CLong requires applying [assembly: DisableRuntimeMarshalling].

Why does my P/Invoke call throw AccessViolationException?

AccessViolationException usually indicates a signature mismatch, use-after-free, or missing pinning. Compare managed and native signatures byte-for-byte, verify Marshal.SizeOf matches native sizeof, and pin objects held across calls with GCHandle or fixed.

How do I prevent callback delegates from being garbage collected in P/Invoke?

Keep a rooted static reference to the delegate for its entire native lifetime, since a collected delegate causes a crash. On .NET 8+, prefer UnmanagedCallersOnly function pointers, which avoid delegates entirely.

When should I not use P/Invoke for native interop?

Avoid this approach for COM interop, C++/CLI mixed-mode assemblies, or pure managed code with no native dependencies. For Win32 APIs specifically, prefer the CsWin32 source generator over hand-written signatures.