dotnet-pinvoke

Configure .NET P/Invoke declarations for safe native interop.

2|Updated Mar 17, 2026
One-click install
npx skills add https://github.com/sayedihashimi/copilot-skill-eval --skill dotnet-pinvoke-sayedihashimi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-pinvoke
Source: https://github.com/sayedihashimi/copilot-skill-eval/tree/main/examples/aspnet-razor-pages/plugins/dotnet-skills/dotnet/skills/dotnet-pinvoke
Command: npx skills add https://github.com/sayedihashimi/copilot-skill-eval --skill dotnet-pinvoke-sayedihashimi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Calling native code from .NET is powerful but unforgiving; incorrect signatures, misapplied marshalling, and lifetime issues frequently cause crashes, memory leaks, and subtle data corruption at the boundary.

Core Features & Use Cases

  • Supports DllImport and LibraryImport usage across .NET Framework and .NET 7+ with guidance on when to prefer LibraryImport for AOT scenarios.
  • Provides correct type mappings (e.g., C long to CLong, size_t to nuint/UIntPtr) and explicit string encoding to avoid subtle marshaling bugs.
  • Encourages SafeHandle usage for native handles, clear memory ownership rules, and robust error handling (SetLastError / HRESULT patterns).
  • Covers common interop scenarios: wrapping native libraries, debugging native crashes, and migrating legacy P/Invoke code to source-generated LibraryImport.

Quick Start

Create a P/Invoke declaration for your native API and test with a small sample to verify correct interop.

Frequently Asked Questions about dotnet-pinvoke

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

FAQPage Schema
How do I safely call native libraries from .NET without memory leaks?

To safely call native libraries from .NET, use correct type mappings, SafeHandle for native handles, and explicit memory ownership rules to prevent leaks and crashes at the boundary.

What is the best way to migrate legacy DllImport to LibraryImport in .NET?

Migrating legacy P/Invoke to source-generated LibraryImport is best for .NET 7+ AOT scenarios, ensuring correct signatures, explicit string encoding, and robust error handling.

Does .NET P/Invoke support cross-platform library loading?

Yes, .NET P/Invoke supports cross-platform library loading by enforcing correct type mappings like C long to CLong and size_t to nuint, ensuring consistent interop across platforms.

How do I handle string encoding and marshalling in .NET interop?

Handle string marshalling in .NET interop by enforcing explicit string encoding and correct type mappings to avoid subtle data corruption and misapplied marshalling bugs.

Why does my .NET application crash when calling native code?

Your .NET application might crash calling native code due to incorrect signatures, misapplied marshalling, or lifetime issues at the boundary, requiring robust error handling and proper memory management.