Skip to content

Add .NET P/Invoke skill#54

Merged
AaronRobinsonMSFT merged 16 commits intodotnet:mainfrom
AaronRobinsonMSFT:interop_skill
Feb 20, 2026
Merged

Add .NET P/Invoke skill#54
AaronRobinsonMSFT merged 16 commits intodotnet:mainfrom
AaronRobinsonMSFT:interop_skill

Conversation

@AaronRobinsonMSFT
Copy link
Member

This pull request adds comprehensive reference documentation and new evaluation scenarios to improve .NET P/Invoke usability and correctness. The main changes include the addition of diagnostic and type mapping guides for native interop, as well as new test scenarios to ensure correct generation of P/Invoke declarations for both .NET 8 and .NET Framework targets.

Documentation Enhancements

  • Added a new diagnostics.md reference guide covering common P/Invoke pitfalls, failure modes, debugging strategies, and key resources. This guide helps developers avoid and troubleshoot interop bugs, with practical advice for signature matching, memory management, and error handling.
  • Added a type-mapping.md reference documenting precise mappings between C/Win32 types and .NET types, highlighting dangerous types, blittable types, and struct layout considerations. This is crucial for preventing subtle interop bugs due to type mismatches.

Testing Improvements

  • Introduced new evaluation scenarios in eval.yaml to test the generation of P/Invoke declarations from C headers for both .NET 8 (using LibraryImport) and .NET Framework (using DllImport). The scenarios assert correct type mapping (e.g., using nuint for size_t) and method signature style, ensuring robust code generation across platforms.

Copilot AI review requested due to automatic review settings February 20, 2026 04:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request introduces comprehensive P/Invoke guidance for .NET, covering both modern (LibraryImport) and legacy (DllImport) approaches to native interop. The skill provides practical documentation for type mapping, memory management, string marshalling, error handling, and cross-platform concerns, along with evaluation tests to ensure agents generate correct P/Invoke declarations.

Changes:

  • Added a new dotnet-pinvoke skill with main documentation (405 lines), type mapping reference, and diagnostics guide
  • Introduced evaluation scenarios testing P/Invoke code generation for both .NET 8 (LibraryImport) and .NET Framework (DllImport)
  • Documented dangerous type mappings (e.g., CLong, nuint), memory ownership patterns, SafeHandle usage, and callback handling

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
src/dotnet/skills/dotnet-pinvoke/SKILL.md Main skill documentation covering P/Invoke workflow, type mapping, memory management, callbacks, and validation (405 lines, within 500 line limit)
src/dotnet/skills/dotnet-pinvoke/references/type-mapping.md Comprehensive native-to-.NET type mapping reference with dangerous types, blittable types, and struct layout patterns
src/dotnet/skills/dotnet-pinvoke/references/diagnostics.md Common pitfalls table, failure modes, debugging strategies, and resource links
src/dotnet/tests/dotnet-pinvoke/eval.yaml Test scenarios for generating LibraryImport (.NET 8) and DllImport (.NET Framework) declarations from C headers

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

@danmoseley
Copy link
Member

some thoughts

  • may need some extra matter aimed at AI, eg., when to commit, how to validate, and in particular when to ask the user for a judgement call
  • the AI may choose to not load one or both of the reference files, but they have some important general info. I guess it should be very clear when they should be loaded, and if it's basically always then the material should be in the main md.
  • definition of when AI should consider it's done? eg all pinvoke headers match native headers exactly, strings have encoding, handles are safe, memory ownership is documented, struct sizes are validated.

@AaronRobinsonMSFT
Copy link
Member Author

eg., when to commit, how to validate, and in particular when to ask the user for a judgement call

SKILLs are about a single task, not a complete dev workflow. This is merely about helping to write P/Invokes. The validation metric is captured in the SKILL itself as to what is correct and not.

definition of when AI should consider it's done?

When the P/Invoke has been written.

I'm not entirely clear as to how you're viewing skills in this case. The layout and flow here follows the anthropic skills approach. How are you viewing these tools?

@danmoseley
Copy link
Member

I'm not entirely clear as to how you're viewing skills in this case. The layout and flow here follows the anthropic skills approach. How are you viewing these tools?

Well, I'm learning, but if you mean https://platform.claude.com/docs/en/agents-and-tools/agent-skills/best-practices it includes examples of 'workflows' with validation steps. perhaps it doesn't apply here.

@AaronRobinsonMSFT
Copy link
Member Author

I'm not entirely clear as to how you're viewing skills in this case. The layout and flow here follows the anthropic skills approach. How are you viewing these tools?

Well, I'm learning, but if you mean https://platform.claude.com/docs/en/agents-and-tools/agent-skills/best-practices it includes examples of 'workflows' with validation steps. perhaps it doesn't apply here.

Oh, nice! I didn't see find this guidance. Solid, let's suggest having it review this.

@danmoseley
Copy link
Member

added a note above about warning about docs

@AaronRobinsonMSFT
Copy link
Member Author

Validation for this skill can be found at #58.

@AaronRobinsonMSFT
Copy link
Member Author

Skill Validation Results

Skill Scenario Baseline With Skill Δ Verdict
csharp-scripts Test a C# language feature with a script 2.8/5 5.0/5 +2.2
dotnet-pinvoke Generate LibraryImport declaration from C header (.NET Core 8+) 4.0/5 5.0/5 +1.0
dotnet-pinvoke Generate LibraryImport declaration from C header (.NET Framework) 3.0/5 5.0/5 +2.0

Model: claude-opus-4.6 | Judge: claude-opus-4.6

Full results

Copy link
Member

@jeffschwMSFT jeffschwMSFT left a comment

Choose a reason for hiding this comment

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

lgtm


## Reference Files

- **[references/type-mapping.md](references/type-mapping.md)** — Complete native-to-.NET type mapping table, struct layout patterns, blittable type rules
Copy link
Member

Choose a reason for hiding this comment

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

in what circumstances would we not want it to read this file? if it should always read it then presumably it should be inline (optional load is the reason for the references pattern)

(I mentioned this earlier but not sure of your answer)

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you know the best way to encode that decision?

Copy link
Member Author

Choose a reason for hiding this comment

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

My latest commit has added something to the references below.

@lewing
Copy link
Member

lewing commented Feb 20, 2026

I ran my tool on it

dotnet-pinvoke Skill Assessment

What's Done Well

  1. Frontmattername and description are correct. Description includes trigger scenarios (Use when (1)...(4)) and anti-triggers (Do not use for COM interop...). Within 1024-char limit.
  2. Knowledge-driven archetype — Correctly chosen. This is domain knowledge applied once per task. No scripts needed.
  3. Progressive disclosure — Heavy content properly split: SKILL.md has the workflow, references/type-mapping.md and references/diagnostics.md hold deep tables.
  4. Domain depth — Excellent coverage: 8 workflow steps, real code examples, multiple memory ownership models, SafeHandle patterns, callback handling, migration guide.
  5. Review checklist — Concrete, actionable validation items at the end.
  6. "Trust the header" agent behavior note — Smart domain-specific guidance.

Issues (by severity)

# Severity Issue Recommendation
1 Critical No stop signals — The skill has 8 steps and the agent could over-investigate by doing all 8 for a simple "map this one function" request. Add a ## Stop Signals section: "If the user provides a single function signature, skip steps 7-8. Don't migrate existing DllImport to LibraryImport unless asked. Don't add CsWin32 unless the target is Win32 APIs."
2 ⚠️ Moderate No "When to Use" section — The description has trigger scenarios but the body jumps straight to ## Inputs. Skill-builder requires 5-8 explicit trigger scenarios in a dedicated section. Add ## When to Use This Skill before Inputs with concrete trigger phrases.
3 ⚠️ Moderate Token budget concern — SKILL.md is ~407 lines (~5K+ tokens). For an "applied once" knowledge skill this is acceptable (up to 15K), but the dangerous types table is duplicated between SKILL.md and type-mapping.md. Trim the duplicate table in SKILL.md to just the top 3 with "see references/type-mapping.md for the complete table".
4 ⚠️ Moderate Description doesn't use USE FOR: / DO NOT USE FOR: keywords — It uses natural language which works but is weaker for routing than the structured keyword pattern. Reformat to USE FOR: writing P/Invoke declarations, reviewing native interop code... DO NOT USE FOR: COM interop, C++/CLI...
5 💡 Nice-to-have No inline anti-patterns — The skill has great positive guidance but doesn't call out the top 3-5 mistakes inline near the steps where they'd occur. The diagnostics reference covers pitfalls, but the most critical ones should be inline. Add 3-5 > ❌ NEVER callouts inline near Steps 2, 4, and 5.
6 💡 Nice-to-have Missing INVOKES: in description — While this skill doesn't invoke MCP tools or scripts, it's worth noting it's pure knowledge. Not strictly needed but consistent with the pattern. N/A — no tools to declare.

Summary

This is a strong knowledge-driven skill with excellent domain depth from Aaron Robinson (the .NET interop expert). The #1 fix is adding stop signals — without them, an agent given a simple "write a P/Invoke for CreateFile" will march through all 8 steps including migration and tooling sections. The #2 fix (When to Use section) is structural and easy. Both are quick wins.

Source

  • Skill: plugins/AaronRobinsonMSFT/skills/dotnet-pinvoke/
  • Assessed against: skill-builder patterns (skill-patterns.md, anti-patterns.md)

was the result and

2847e13

Was the first round of improvements it suggested. Thoughts?

@AaronRobinsonMSFT
Copy link
Member Author

Was the first round of improvements it suggested. Thoughts?

I think this is a valid set of feedback. Some I think are obvious, others I'm less inclined to agree with but I'd like to discuss in a PR. Let's go with your suggestion here and when this gets merged, we can consider that feedback.

@AaronRobinsonMSFT
Copy link
Member Author

I'm going to merge this for now. Let's iterate on it with Larry's tooling.

@AaronRobinsonMSFT AaronRobinsonMSFT merged commit 5e0845e into dotnet:main Feb 20, 2026
1 check passed
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the interop_skill branch February 20, 2026 22:01
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.

5 participants