/dotnet-native-interop:dotnet-native-interopfrom dotnet-native-interop
Writes, reviews and debugs .NET native interop with P/Invoke and LibraryImport, covering signatures from C headers, string and struct marshalling, memory ownership, SafeHandle, callbacks and library loading. Use for wrapping a C library, a DllNotFoundException or AccessViolationException at a native call, or moving DllImport to LibraryImport for AOT.
Ships in dotnet-native-interop. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Interop bugs rarely fail at the faulty line. A wrong type size, encoding or allocator shows up later as an intermittent crash, corrupted data or an access violation somewhere else. Match the native contract exactly, then prove it with a round-trip test.
Not for: COM interop or C++/CLI; general C# work (csharp); trimming and AOT readiness across a whole app (dotnet-upgrade-guide); native crash dumps from production (dotnet-diagnostics); build and packaging of native assets (msbuild, nuget).
<TargetFramework>), RIDs and <PlatformTarget>. They decide DllImport vs LibraryImport and whether calling conventions matter.search_code for DllImport/LibraryImport, trace_path for callers), else the C# language server (csharp-ls) or Grep.DllImport to LibraryImport unless the user asks or AOT or trimming is a requirement.DllImport | LibraryImport | |
|---|---|---|
| Mechanism | Runtime marshalling | Source generator, compile time |
| Native AOT and trim safe | No | Yes |
| Strings | CharSet | StringMarshalling |
| OS error code | SetLastError = true | SetLastPInvokeError = true |
| Available | .NET Framework and every .NET | .NET 7+ |
.NET Framework: DllImport. .NET 7+: LibraryImport for new code, and it is the only option under Native AOT.
The mappings behind most bugs:
| C / Win32 | .NET | Why |
|---|---|---|
long, unsigned long | CLong, CULong | 32-bit on Windows, 64-bit on 64-bit Unix. With LibraryImport they need [assembly: DisableRuntimeMarshalling] (else SYSLIB1051) |
size_t | nuint | Pointer-sized. Never ulong or int |
BOOL (Win32) | int, or bool with [MarshalAs(UnmanagedType.Bool)] | 4 bytes |
bool (C99) | [MarshalAs(UnmanagedType.U1)] bool | 1 byte; the default bool marshalling is 4 bytes |
HANDLE, owned resource | a SafeHandle subclass | No leak on exceptions, no double free |
wchar_t*, LPWSTR | string, UTF-16 | Windows only: wchar_t is usually 32-bit elsewhere |
char* | string with explicit UTF-8 or ANSI | No safe default |
Read references/type-mapping.md for the full table, blittable rules, struct layout, packing and unions.
// int32_t process_records(const Record* records, size_t count, uint32_t* out_processed);
[LibraryImport("mylib", EntryPoint = "process_records")]
internal static partial int ProcessRecords([In] Record[] records, nuint count, out uint outProcessed);
EntryPoint when the C# name differs from the export, else EntryPointNotFoundException."mylib" resolves to mylib.dll, libmylib.so or libmylib.dylib. Read references/loading-and-tooling.md for resolvers and per-OS names.<PlatformTarget>x86</PlatformTarget>, win-x86 RID, build scripts), say so and add [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] (or CallingConvention = CallingConvention.Cdecl on DllImport) to every declaration.W variant with UTF-16 (StringMarshalling.Utf16 / CharSet.Unicode). Use the A variant only for a stated reason.StringMarshalling.Utf8, or [MarshalAs(UnmanagedType.LPUTF8Str)] on DllImport).CharSet.Auto or a default.char[], byte[] or Span<byte> with a length. Don't introduce StringBuilder.Exactly one side owns each allocation, and it is freed with the matching allocator. Marshal.FreeHGlobal on malloc'd memory corrupts the heap. Prefer, in order: caller allocates and frees (Span<byte> + length); callee allocates and the library's own free function releases it in finally; a SafeHandle whose ReleaseHandle calls the library's destroy function.
Read references/memory-and-callbacks.md for the three ownership models, a complete SafeHandle wrapper, pinning (fixed vs GCHandle), SetLastPInvokeError and HRESULT handling, and callbacks (UnmanagedCallersOnly function pointers, or rooted delegates with GC.KeepAlive).
partial and the method static partial.[DllImport] to [LibraryImport]; CharSet to StringMarshalling; SetLastError to SetLastPInvokeError.CallingConvention unless Windows x86 is a target.MarshalAs on every bool.<IsAotCompatible>true</IsAotCompatible> (or <EnableTrimAnalyzer> and <EnableAotAnalyzer>) and clear SYSLIB1050-SYSLIB1057 diagnostics. SYSLIB1054 is the analyzer that flags a DllImport the generator could handle.EntryPoint, encoding, x86 calling convention.Marshal.SizeOf<T>() (or sizeof(T) for blittable structs) equals the native sizeof, with field offsets checked for packed structs.IntPtr handle escapes the interop layer; delegates passed as callbacks stay rooted.SetLastPInvokeError/SetLastError is set on APIs that report OS error codes.For crashes, DllNotFoundException, EntryPointNotFoundException or silent corruption, read references/diagnostics.md.
CLong with LibraryImport fails with SYSLIB1051 until the assembly has [assembly: DisableRuntimeMarshalling], which also changes how bool and char marshal across the whole assembly.#if WINDOWS / LINUX / MACOS are defined only for OS-specific TFMs (net10.0-windows), not for net10.0. Use a NativeLibrary resolver for portable TFMs.Marshal.GetFunctionPointerForDelegate does not root the delegate. A collected delegate crashes in native code later.[UnmanagedCallersOnly] method must be static, use blittable parameters, and never let an exception escape to native code.extern "C" are name-mangled; check the export table before blaming the signature.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub adminhead/claude-plugins --plugin dotnet-native-interop