Add .NET P/Invoke skill - #19
Conversation
…er and README conventions
…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
… portability notes
There was a problem hiding this comment.
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.mdwith comprehensive native-to-.NET type mapping tables highlighting dangerous types, blittable types, and struct layout patterns - Added
references/diagnostics.mdwith 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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
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. |
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. |
| ```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); | ||
| } | ||
| ``` |
There was a problem hiding this comment.
Add an example with GC.KeepAlive as well for the more common scenario you'd end up in here?
There was a problem hiding this comment.
Added. Let me know if you have another example in mind.
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:
SKILL.mdthat explains when and how to use P/Invoke andLibraryImport, including step-by-step workflows for declaration, string marshalling, memory management, SafeHandle usage, error handling, and callback patterns. It also provides migration guidance fromDllImporttoLibraryImport, cross-platform loading strategies, and a validation checklist.Reference Material:
references/type-mapping.mdwith a full table mapping C/Win32 types to .NET types, highlighting dangerous types, blittable rules, and correct struct layout patterns to prevent subtle bugs.references/diagnostics.mdoutlining common pitfalls, failure modes, debugging steps, and links to authoritative resources for diagnosing and resolving interop issues.