dotnet/skills✦ 精选0°

dotnet-pinvoke

仓库创建 2026年2月4日最近提交 14 小时前SkillHot 收录 20 天前
▸ 精选理由

覆盖常见签名、串行化与内存泄漏陷阱,适合互操作审查与修复。

▸ 风险提示

涉及本地代码和内存管理,错误可能导致崩溃或安全漏洞。

这个 Skill 做什么

指导在 .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.

开发编程P/Invoke互操作内存安全通用
4.8k
Stars
351
Forks
40
仓库内 Skill
+610
7 日增星
安装 / 使用
给你的 Agent 一句话(通用)
帮我安装这个 skill:https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-advanced/skills/dotnet-pinvoke/SKILL.md
或 curl 直取 SKILL.md
curl -fsSL "https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-advanced/skills/dotnet-pinvoke/SKILL.md"
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
via SKILL·HOT · 数据来自 GitHub 公开信息 · 原文版权归作者所有