dotnet-pinvoke

Audit .NET P/Invoke and LibraryImport declarations against C/C++ ABI expectations.

1|Updated May 21, 2026
One-click install
npx skills add https://github.com/1k-off/umbraco-observability-playground --skill dotnet-pinvoke-1k-off
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-pinvoke
Source: https://github.com/1k-off/umbraco-observability-playground/tree/main/.agents/skills/dotnet-pinvoke
Command: npx skills add https://github.com/1k-off/umbraco-observability-playground --skill dotnet-pinvoke-1k-off

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevent crashes, memory corruption, and silent data issues caused by incorrect .NET P/Invoke or LibraryImport signatures when calling native C/C++ code.

Core Features & Use Cases

  • Correct signatures and calling conventions: Map native function parameters and return types precisely, including platform-specific details like CLong and size_t equivalents.
  • Safe string and memory handling: Choose correct string encoding/marshalling rules and follow the native ownership contract so allocations are freed by the matching allocator.
  • Robust handle and callback patterns: Use SafeHandle for native handles and root delegates or use UnmanagedCallersOnly to avoid GC-related intermittent failures.

Quick Start

Ask: "Review my C header-derived P/Invoke/LibraryImport declarations for type sizes, string marshalling, calling convention (if needed), SafeHandle usage, and memory ownership, and then tell me exactly what to change to stop the AccessViolationException."

Frequently Asked Questions about dotnet-pinvoke

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

FAQPage Schema
Why does my .NET P/Invoke call throw an AccessViolationException?

An AccessViolationException during P/Invoke usually happens when .NET interop signatures mismatch C/C++ ABI expectations, causing memory corruption. Correcting type mappings, calling conventions, and string marshalling prevents these crashes.

How do I map C long and size_t types correctly in .NET LibraryImport declarations?

To map C long and size_t correctly in .NET LibraryImport declarations, use CLong for C long values and nuint or UIntPtr for size_t. Precise type mappings ensure parameters match native ABI expectations across platforms.

What is the best way to handle native memory ownership and string marshalling in .NET interop?

The best way to handle native memory ownership in .NET interop is to follow the native ownership contract and free allocations using the matching allocator. Explicit string encoding and correct marshalling rules prevent silent data corruption.

How do I prevent intermittent GC failures when passing callbacks to native C code?

To prevent intermittent GC failures when passing callbacks to native C code, root your delegates manually or use UnmanagedCallersOnly. This stops the garbage collector from collecting delegates while native code executes.

When do I need SafeHandle for native handles in .NET P/Invoke?

You need SafeHandle for native handles in .NET P/Invoke whenever you wrap unmanaged OS resources. Using SafeHandle ensures robust handle lifecycle management and prevents resource leaks during asynchronous exceptions or GC collection.