dotnet-pinvoke
覆盖常见签名、串行化与内存泄漏陷阱,适合互操作审查与修复。
涉及本地代码和内存管理,错误可能导致崩溃或安全漏洞。
指导在 .NET 中正确声明并调用本地 C/C++ 库,处理字符串和内存问题。
帮你在 .NET 世界里稳健调用本地 C/C++ 库,解决最常见的坑:函数签名不对、字符串乱码、内存生存期和泄漏问题。适合写新的 P/Invoke 或 LibraryImport 声明、排查崩溃或边界内存损坏、把 C/C++ 包装成可被 .NET 调用的接口。会讲清怎么做 marshaling、什么时候用 SafeHandle、以及跨平台的常见做法,避免隐身的崩溃和数据损坏。
▸ 展开 SKILL.md 英文原文
Correctly call native (C/C++) libraries from .NET using P/Invoke and LibraryImport. Covers function signatures, string marshalling, memory lifetime, SafeHandle, and cross-platform patterns. USE FOR: writing new P/Invoke or LibraryImport declarations, reviewing or debugging existing native interop code, wrapping a C or C++ library for use in .NET, diagnosing crashes, memory leaks, or corruption at the managed/native boundary. DO NOT USE FOR: COM interop, C++/CLI mixed-mode assemblies, or pure managed code with no native dependencies.
帮我安装这个 skill:https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-advanced/skills/dotnet-pinvoke/SKILL.mdcurl -fsSL "https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-advanced/skills/dotnet-pinvoke/SKILL.md"# .NET P/Invoke Calling native code from .NET is powerful but unforgiving. Incorrect signatures, garbled strings, and leaked or freed memory are the most common sources of bugs — all can manifest as intermittent crashes, silent data corruption, or access violations far from the actual defect. This skill covers both `DllImport` (available since .NET Framework 1.0) and `LibraryImport` (source-generated, .NET 7+). When targeting .NET Framework, always use `DllImport`. When targeting .NET 7+, prefer `LibraryImport` for new code. When native AOT is a requirement, `LibraryImport` is the only option. ## When to Use This Skill - Writing a new `[DllImport]` or `[LibraryImport]` declaration from a