dotnet-pinvoke

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

5.1k|377|Updated Feb 3, 2026
One-click install
npx skills add https://github.com/dotnet/skills --skill dotnet-pinvoke
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-pinvoke
Source: https://github.com/dotnet/skills/tree/main/plugins/dotnet/skills/dotnet-pinvoke
Command: npx skills add https://github.com/dotnet/skills --skill dotnet-pinvoke

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps developers correctly and safely call native C/C++ libraries from .NET applications, preventing common crashes and data corruption issues.

Core Features & Use Cases

  • Signature Mapping: Translates native C/C++ function signatures and data types into their correct .NET equivalents.
  • Memory Management: Guides on correctly handling memory allocation, deallocation, and lifetime across the managed/native boundary.
  • Use Case: You need to wrap a C library for use in a .NET Core application. This Skill will help you define the correct P/Invoke signatures, manage string marshalling, and ensure native handles are properly released.

Quick Start

Use the dotnet-pinvoke skill to generate a C# signature for the native C function int process_records(const Record* records, size_t count, uint32_t* out_processed); given the Record struct definition.

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 C# without crashing my .NET application?

Calling a C library from C# requires correctly mapping native function signatures and managing memory lifetime across the managed/native boundary using P/Invoke. This skill generates the exact C# signatures and SafeHandle wrappers needed to prevent interop crashes and data corruption.

What is the best way to migrate DllImport to LibraryImport for AOT and trimming support?

Migrating DllImport to LibraryImport for AOT and trimming support involves updating your native interop declarations to use the source-generated LibraryImport attribute. This skill guides the migration process to ensure type marshalling and cross-platform compatibility are preserved.

How do I marshal C strings and structs when using P/Invoke in .NET?

Marshalling C strings and structs in .NET P/Invoke requires translating native data types into their correct managed equivalents and handling memory allocation properly. This skill provides the correct signature mappings and memory management guidance to ensure safe data transfer.

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

Your application likely crashes during native code calls due to improper type marshalling, unmanaged memory leaks, or incorrect native handle release. This skill helps debug interop crashes by addressing function signature mapping, memory lifetime management, and SafeHandle usage.

Do I need SafeHandle when wrapping native libraries in .NET?

You need SafeHandle when wrapping native libraries to guarantee that native handles are properly released even during asynchronous exceptions. This skill guides SafeHandle usage to ensure robust memory lifetime management across the managed and native boundary.

Can I use P/Invoke for cross-platform native interop in .NET Core?

Yes, you can use P/Invoke for cross-platform native interop in .NET Core by correctly mapping function signatures and ensuring compatible type marshalling. This skill facilitates safe native C/C++ library invocation across different operating systems.