Skip to content

Add .NET P/Invoke skill - #19

Closed
AaronRobinsonMSFT wants to merge 12 commits into
dotnet:mainfrom
AaronRobinsonMSFT:interop_skill
Closed

Add .NET P/Invoke skill#19
AaronRobinsonMSFT wants to merge 12 commits into
dotnet:mainfrom
AaronRobinsonMSFT:interop_skill

Conversation

@AaronRobinsonMSFT

@AaronRobinsonMSFT AaronRobinsonMSFT commented Feb 12, 2026

Copy link
Copy Markdown
Member

Fixes #5

This pull request introduces a comprehensive .NET P/Invoke skill, including detailed documentation, diagnostics, and type mapping references. The main goal is to provide clear, authoritative guidance for correctly calling native C/C++ libraries from .NET using P/Invoke (DllImport) and source-generated interop (LibraryImport). The documentation covers best practices, common pitfalls, memory management, type mappings, error handling, and validation steps to prevent common bugs and ensure robust interop.

Key additions and improvements:

Core Documentation and Guidance:

  • Added a detailed SKILL.md that explains when and how to use P/Invoke and LibraryImport, including step-by-step workflows for declaration, string marshalling, memory management, SafeHandle usage, error handling, and callback patterns. It also provides migration guidance from DllImport to LibraryImport, cross-platform loading strategies, and a validation checklist.

Reference Material:

  • Added references/type-mapping.md with a full table mapping C/Win32 types to .NET types, highlighting dangerous types, blittable rules, and correct struct layout patterns to prevent subtle bugs.
  • Added references/diagnostics.md outlining common pitfalls, failure modes, debugging steps, and links to authoritative resources for diagnosing and resolving interop issues.

…ct references

- Expand frontmatter description with trigger info (when to use / not use)
- Remove redundant When to Use / When Not to Use body sections
- Trim type mapping to dangerous-only entries inline; full table in references/type-mapping.md
- Move blittable structs, common pitfalls, failure modes, resources to references/
- Reduce SKILL.md from ~510 to 285 lines
@AaronRobinsonMSFT

Copy link
Copy Markdown
Member Author

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a comprehensive .NET P/Invoke skill that provides authoritative guidance for calling native C/C++ libraries from .NET using both DllImport (classic) and LibraryImport (source-generated, .NET 7+). The skill is well-structured with detailed workflows, type mapping tables, diagnostic guidance, and best practices to prevent common P/Invoke bugs such as incorrect type mappings, memory leaks, and marshalling errors.

Changes:

  • Added SKILL.md (386 lines) with complete P/Invoke workflow covering declaration, string marshalling, memory management, SafeHandle usage, error handling, callbacks, cross-platform loading, and migration guidance
  • Added references/type-mapping.md with comprehensive native-to-.NET type mapping tables highlighting dangerous types, blittable types, and struct layout patterns
  • Added references/diagnostics.md with common pitfalls, failure modes, debugging approaches, and authoritative Microsoft documentation links

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
skills/dotnet-pinvoke/SKILL.md Main skill document with YAML frontmatter, comprehensive P/Invoke workflows, validation checklists, and cross-references to supporting materials
skills/dotnet-pinvoke/references/type-mapping.md Type mapping reference table covering primitive types, dangerous types, handles, strings, blittable types, and struct layout examples
skills/dotnet-pinvoke/references/diagnostics.md Diagnostic reference covering common pitfalls, failure modes, debugging approaches, and links to Microsoft documentation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread skills/dotnet-pinvoke/references/diagnostics.md Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jkotas

jkotas commented Feb 12, 2026

Copy link
Copy Markdown
Member

Do you have some good examples of interactions with this skill? Ideally comparing with what you get from raw copilot CLI to demonstrate the improvement.

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member Author

Do you have some good examples of interactions with this skill? Ideally comparing with what you get from raw copilot CLI to demonstrate the improvement.

@adityamandaleeka and I were discussing this. We are working on something for this.

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member Author

Do you have some good examples of interactions with this skill? Ideally comparing with what you get from raw copilot CLI to demonstrate the improvement.

I've been trying scenarios that between .NET Framework and .NET Core 8. The results are much better with this skill when making those decisions. It also relies on more modern methods with .NET being called out in the skill for some reason.

Comment thread src/dotnet/skills/dotnet-pinvoke/SKILL.md
Comment on lines +262 to +278
```csharp
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] // Only needed on Windows x86
private delegate void LogCallbackDelegate(int level, IntPtr message);

// CRITICAL: prevent delegate from being garbage collected
private static LogCallbackDelegate? s_logCallback;

public static void EnableLogging(Action<int, string> handler)
{
s_logCallback = (level, msgPtr) =>
{
string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;
handler(level, msg);
};
SetLogCallback(s_logCallback);
}
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an example with GC.KeepAlive as well for the more common scenario you'd end up in here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Let me know if you have another example in mind.

Comment thread src/dotnet/skills/dotnet-pinvoke/SKILL.md
Comment thread src/dotnet/skills/dotnet-pinvoke/SKILL.md
Comment thread skills/dotnet-pinvoke/SKILL.md Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skill or agent for handling .NET interop

5 participants