[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-dotnet-dotnet-pinvoke":3,"mdc--uy6743-key":34,"related-repo-dotnet-dotnet-pinvoke":3773,"related-org-dotnet-dotnet-pinvoke":3882},{"slug":4,"name":4,"fn":5,"description":6,"org":7,"tags":11,"stars":22,"repoUrl":23,"updatedAt":24,"license":25,"forks":26,"topics":27,"repo":29,"sourceUrl":32,"mdContent":33},"dotnet-pinvoke","implement P\u002FInvoke for native libraries","Correctly call native (C\u002FC++) libraries from .NET using P\u002FInvoke and LibraryImport. Covers function signatures, string marshalling, memory lifetime, SafeHandle, and cross-platform patterns. USE FOR: writing new P\u002FInvoke 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\u002Fnative boundary. DO NOT USE FOR: COM interop, C++\u002FCLI mixed-mode assemblies, or pure managed code with no native dependencies.\n",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},"dotnet",".NET (Microsoft)","https:\u002F\u002Fpexgzepcugksgbtrxkhf.supabase.co\u002Fstorage\u002Fv1\u002Fobject\u002Fpublic\u002Forg-logos\u002Fdotnet.png",[12,16,19],{"name":13,"slug":14,"type":15},".NET","net","tag",{"name":17,"slug":18,"type":15},"C#","c",{"name":20,"slug":21,"type":15},"API Development","api-development",4576,"https:\u002F\u002Fgithub.com\u002Fdotnet\u002Fskills","2026-07-12T08:24:16.11292","MIT",332,[28],"agent-skills",{"repoUrl":23,"stars":22,"forks":26,"topics":30,"description":31},[28],"Repository for skills to assist AI coding agents with .NET and C#","https:\u002F\u002Fgithub.com\u002Fdotnet\u002Fskills\u002Ftree\u002FHEAD\u002Fplugins\u002Fdotnet-advanced\u002Fskills\u002Fdotnet-pinvoke","---\nname: dotnet-pinvoke\ndescription: >\n  Correctly call native (C\u002FC++) libraries from .NET using P\u002FInvoke and LibraryImport.\n  Covers function signatures, string marshalling, memory lifetime, SafeHandle, and\n  cross-platform patterns.\n  USE FOR: writing new P\u002FInvoke or LibraryImport declarations, reviewing or debugging\n  existing native interop code, wrapping a C or C++ library for use in .NET, diagnosing\n  crashes, memory leaks, or corruption at the managed\u002Fnative boundary.\n  DO NOT USE FOR: COM interop, C++\u002FCLI mixed-mode assemblies, or pure managed code with\n  no native dependencies.\nlicense: MIT\n---\n\n# .NET P\u002FInvoke\n\nCalling 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.\n\nThis 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.\n\n## When to Use This Skill\n\n- Writing a new `[DllImport]` or `[LibraryImport]` declaration from a C\u002FC++ header\n- Reviewing P\u002FInvoke signatures for correctness (type sizes, calling conventions, string encoding)\n- Wrapping an entire C library for use from .NET\n- Debugging `AccessViolationException`, `DllNotFoundException`, or silent data corruption at the native boundary\n- Migrating `DllImport` declarations to `LibraryImport` for AOT\u002Ftrimming compatibility\n- Diagnosing memory leaks or heap corruption involving native handles or buffers\n\n## Stop Signals\n\n- **Single function?** Map the signature (Steps 1-3), handle strings\u002Fmemory only if relevant, skip tooling and migration sections.\n- **Don't migrate** existing `DllImport` to `LibraryImport` unless the user asks or AOT\u002Ftrimming is an explicit requirement.\n- **Don't recommend CsWin32** unless the target is specifically Win32 APIs.\n- **Don't generate callbacks** (Step 8) unless the native API requires function pointers.\n- **Review request?** Use the validation checklist — don't rewrite working code.\n\n## Inputs\n\n| Input | Required | Description |\n|-------|----------|-------------|\n| Native header or documentation | Yes | C\u002FC++ function signatures, struct definitions, calling conventions |\n| Target framework | Yes | Determines whether to use `DllImport` or `LibraryImport` |\n| Target platforms | Recommended | Affects type sizes (`long`, `size_t`) and library naming |\n| Memory ownership contract | Yes | Who allocates and who frees each buffer or handle |\n\n**Agent behavior:** When documentation and native headers diverge, always trust the header. Online documentation (including official Win32 API docs) frequently omits or simplifies details about types, calling conventions, and struct layout that are critical for correct P\u002FInvoke signatures.\n\n---\n\n## Workflow\n\n### Step 1: Choose DllImport or LibraryImport\n\n| Aspect | `DllImport` | `LibraryImport` (.NET 7+) |\n|--------|-------------|---------------------------|\n| **Mechanism** | Runtime marshalling | Source generator (compile-time) |\n| **AOT \u002F Trim safe** | No | Yes |\n| **String marshalling** | `CharSet` enum | `StringMarshalling` enum |\n| **Error handling** | `SetLastError` | `SetLastPInvokeError` |\n| **Availability** | .NET Framework 1.0+ | .NET 7+ only |\n\n### Step 2: Map Native Types to .NET Types\n\nThe most dangerous mappings — these cause the majority of bugs:\n\n| C \u002F Win32 Type | .NET Type | Why |\n|----------------|-----------|-----|\n| `long` | **`CLong`** | 32-bit on Windows, 64-bit on 64-bit Unix. With `LibraryImport`, requires `[assembly: DisableRuntimeMarshalling]` |\n| `size_t` | `nuint` \u002F `UIntPtr` | Pointer-sized. Use `nuint` on .NET 8+ and `UIntPtr` on earlier .NET. Never use `ulong` |\n| `BOOL` (Win32) | `int` | Not `bool` — Win32 `BOOL` is 4 bytes |\n| `bool` (C99) | `[MarshalAs(UnmanagedType.U1)] bool` | Must specify 1-byte marshal |\n| `HANDLE`, `HWND` | `SafeHandle` | Prefer over raw `IntPtr` |\n| `LPWSTR` \u002F `wchar_t*` | `string` | UTF-16 on Windows (lowest cost for `in` strings). Avoid in cross-platform code — `wchar_t` width is compiler-defined (typically UTF-32 on non-Windows) |\n| `LPSTR` \u002F `char*` | `string` | Must specify encoding (ANSI or UTF-8). Always requires marshalling cost for `in` parameters |\n\n**For the complete type mapping table, struct layout, and blittable type rules**, see [references\u002Ftype-mapping.md](references\u002Ftype-mapping.md).\n\n> ❌ **NEVER** use `int` or `long` for C `long` — it's 32-bit on Windows, 64-bit on Unix. Always use `CLong`.\n> ❌ **NEVER** use `ulong` for `size_t` — causes stack corruption on 32-bit. Use `nuint` or `UIntPtr`.\n> ❌ **NEVER** use `bool` without `MarshalAs` — the default marshal size is wrong.\n\n### Step 3: Write the Declaration\n\nGiven a C header:\n\n```c\nint32_t process_records(const Record* records, size_t count, uint32_t* out_processed);\n```\n\n**DllImport:**\n\n```csharp\n[DllImport(\"mylib\")]\nprivate static extern int ProcessRecords(\n    [In] Record[] records, UIntPtr count, out uint outProcessed);\n```\n\n**LibraryImport:**\n\n```csharp\n[LibraryImport(\"mylib\")]\ninternal static partial int ProcessRecords(\n    [In] Record[] records, nuint count, out uint outProcessed);\n```\n\nCalling conventions only need to be specified when targeting Windows x86 (32-bit), where `Cdecl` and `StdCall` differ. On x64, ARM, and ARM64, there is a single calling convention and the attribute is unnecessary.\n\n**Agent behavior:** If you detect that Windows x86 is a target — through project properties (e.g., `\u003CPlatformTarget>x86\u003C\u002FPlatformTarget>`), runtime identifiers (e.g., `win-x86`), build scripts, comments, or developer instructions — flag this to the developer and recommend explicit calling conventions on all P\u002FInvoke declarations.\n\n```csharp\n\u002F\u002F DllImport (x86 targets)\n[DllImport(\"mylib\", CallingConvention = CallingConvention.Cdecl)]\n\n\u002F\u002F LibraryImport (x86 targets)\n[LibraryImport(\"mylib\")]\n[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]\n```\n\nIf the managed method name differs from the native export name, specify `EntryPoint` to avoid `EntryPointNotFoundException`:\n\n```csharp\n\u002F\u002F DllImport\n[DllImport(\"mylib\", EntryPoint = \"process_records\")]\nprivate static extern int ProcessRecords(\n    [In] Record[] records, UIntPtr count, out uint outProcessed);\n\n\u002F\u002F LibraryImport\n[LibraryImport(\"mylib\", EntryPoint = \"process_records\")]\ninternal static partial int ProcessRecords(\n    [In] Record[] records, nuint count, out uint outProcessed);\n```\n\n### Step 4: Handle Strings Correctly\n\n1. **Know what encoding the native function expects.** There is no safe default.\n2. **Windows APIs:** Always call the `W` (UTF-16) variant. The `A` variant needs a specific reason and explicit ANSI encoding.\n3. **Cross-platform C libraries:** Usually expect UTF-8.\n4. **Specify encoding explicitly.** Never rely on `CharSet.Auto`.\n5. **Never introduce `StringBuilder` for output buffers.**\n\n> ❌ **NEVER** rely on `CharSet.Auto` or omit string encoding — there is no safe default.\n\n```csharp\n\u002F\u002F DllImport — Windows API (UTF-16)\n[DllImport(\"kernel32.dll\", CharSet = CharSet.Unicode, SetLastError = true)]\nprivate static extern int GetModuleFileNameW(\n    IntPtr hModule, [Out] char[] filename, int size);\n\n\u002F\u002F DllImport — Cross-platform C library (UTF-8)\n[DllImport(\"mylib\")]\nprivate static extern int SetName(\n    [MarshalAs(UnmanagedType.LPUTF8Str)] string name);\n\n\u002F\u002F LibraryImport — UTF-16\n[LibraryImport(\"kernel32\", StringMarshalling = StringMarshalling.Utf16,\n    SetLastPInvokeError = true)]\ninternal static partial int GetModuleFileNameW(\n    IntPtr hModule, [Out] char[] filename, int size);\n\n\u002F\u002F LibraryImport — UTF-8\n[LibraryImport(\"mylib\", StringMarshalling = StringMarshalling.Utf8)]\ninternal static partial int SetName(string name);\n```\n\n**String lifetime warning:** Marshalled strings are freed after the call returns. If native code stores the pointer (instead of copying), the lifetime must be manually managed. On Windows or .NET Framework, `CoTaskMemAlloc`\u002F`CoTaskMemFree` is the first choice for cross-boundary ownership; on non-Windows targets, use `NativeMemory` APIs. The library may have its own allocator that must be used instead.\n\n### Step 5: Establish Memory Ownership\n\nWhen memory crosses the boundary, exactly one side must own it — and both sides must agree.\n\n> ❌ **NEVER** free with a mismatched allocator — `Marshal.FreeHGlobal` on `malloc`'d memory is heap corruption.\n\n**Model 1 — Caller allocates, caller frees (safest):**\n\n```csharp\n[LibraryImport(\"mylib\")]\nprivate static partial int GetName(\n    Span\u003Cbyte> buffer, nuint bufferSize, out nuint actualSize);\n\npublic static string GetName()\n{\n    Span\u003Cbyte> buffer = stackalloc byte[256];\n    int result = GetName(buffer, (nuint)buffer.Length, out nuint actualSize);\n    if (result != 0) throw new InvalidOperationException($\"Failed: {result}\");\n    return Encoding.UTF8.GetString(buffer[..(int)actualSize]);\n}\n```\n\n**Model 2 — Callee allocates, caller frees (common in Win32):**\n\n```csharp\n[LibraryImport(\"mylib\")]\nprivate static partial IntPtr GetVersion();\n[LibraryImport(\"mylib\")]\nprivate static partial void FreeString(IntPtr s);\n\npublic static string GetVersion()\n{\n    IntPtr ptr = GetVersion();\n    try { return Marshal.PtrToStringUTF8(ptr) ?? throw new InvalidOperationException(); }\n    finally { FreeString(ptr); } \u002F\u002F Must use the library's own free function\n}\n```\n\n**Critical rule:** Always free with the matching allocator. Never use `Marshal.FreeHGlobal` or `Marshal.FreeCoTaskMem` on `malloc`'d memory.\n\n**Model 3 — Handle-based (callee allocates, callee frees):** Use `SafeHandle` (see Step 6).\n\n**Pinning managed objects** — when native code stores the pointer or runs asynchronously:\n\n```csharp\n\u002F\u002F Synchronous: use fixed\npublic static unsafe void ProcessSync(byte[] data)\n{\n    fixed (byte* ptr = data) { ProcessData(ptr, (nuint)data.Length); }\n}\n\n\u002F\u002F Asynchronous: use GCHandle\nvar gcHandle = GCHandle.Alloc(data, GCHandleType.Pinned);\n\u002F\u002F Must keep pinned until native processing completes, then call gcHandle.Free()\n```\n\n### Step 6: Use SafeHandle for Native Handles\n\nRaw `IntPtr` leaks on exceptions and has no double-free protection. `SafeHandle` is non-negotiable.\n\n```csharp\ninternal sealed class MyLibHandle : SafeHandleZeroOrMinusOneIsInvalid\n{\n    \u002F\u002F Required by the marshalling infrastructure to instantiate the handle.\n    \u002F\u002F Do not remove — there are no direct callers.\n    private MyLibHandle() : base(ownsHandle: true) { }\n\n    [LibraryImport(\"mylib\", StringMarshalling = StringMarshalling.Utf8)]\n    private static partial MyLibHandle CreateHandle(string config);\n\n    [LibraryImport(\"mylib\")]\n    private static partial int UseHandle(MyLibHandle h, ReadOnlySpan\u003Cbyte> data, nuint len);\n\n    [LibraryImport(\"mylib\")]\n    private static partial void DestroyHandle(IntPtr h);\n\n    protected override bool ReleaseHandle() { DestroyHandle(handle); return true; }\n\n    public static MyLibHandle Create(string config)\n    {\n        var h = CreateHandle(config);\n        if (h.IsInvalid) throw new InvalidOperationException(\"Failed to create handle\");\n        return h;\n    }\n\n    public int Use(ReadOnlySpan\u003Cbyte> data) => UseHandle(this, data, (nuint)data.Length);\n}\n\n\u002F\u002F Usage: SafeHandle is IDisposable\nusing var handle = MyLibHandle.Create(\"config=value\");\nint result = handle.Use(myData);\n```\n\n### Step 7: Handle Errors\n\n```csharp\n\u002F\u002F Win32 APIs — check SetLastError\n[LibraryImport(\"kernel32\", SetLastPInvokeError = true)]\n[return: MarshalAs(UnmanagedType.Bool)]\ninternal static partial bool CloseHandle(IntPtr hObject);\n\nif (!CloseHandle(handle))\n    throw new Win32Exception(Marshal.GetLastPInvokeError());\n\n\u002F\u002F HRESULT APIs\nint hr = NativeDoWork(context);\nMarshal.ThrowExceptionForHR(hr);\n```\n\n### Step 8: Handle Callbacks (if needed)\n\n**Preferred (.NET 8+): `UnmanagedCallersOnly`** — avoids delegates entirely, no GC lifetime risk:\n\n```csharp\n[UnmanagedCallersOnly]\nprivate static void LogCallback(int level, IntPtr message)\n{\n    string msg = Marshal.PtrToStringUTF8(message) ?? string.Empty;\n    Console.WriteLine($\"[{level}] {msg}\");\n}\n\n[LibraryImport(\"mylib\")]\nprivate static unsafe partial void SetLogCallback(\n    delegate* unmanaged\u003Cint, IntPtr, void> cb);\n\nunsafe { SetLogCallback(&LogCallback); }\n```\n\nThe method must be `static`, must not throw exceptions back to native code, and can only use blittable parameter types.\n\n**Fallback (older TFMs or when instance state is needed): delegate with rooting**\n\n```csharp\n[UnmanagedFunctionPointer(CallingConvention.Cdecl)] \u002F\u002F Only needed on Windows x86\nprivate delegate void LogCallbackDelegate(int level, IntPtr message);\n\n\u002F\u002F CRITICAL: prevent delegate from being garbage collected\nprivate static LogCallbackDelegate? s_logCallback;\n\npublic static void EnableLogging(Action\u003Cint, string> handler)\n{\n    s_logCallback = (level, msgPtr) =>\n    {\n        string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;\n        handler(level, msg);\n    };\n    SetLogCallback(s_logCallback);\n}\n```\n\nIf native code stores the function pointer, the delegate **must** stay rooted for its entire lifetime. A collected delegate means a crash.\n\n**`GC.KeepAlive` for short-lived callbacks:** When converting a delegate to a function pointer with `Marshal.GetFunctionPointerForDelegate`, the GC does not track the relationship between the pointer and the delegate. Use `GC.KeepAlive` to prevent collection before the native call completes:\n\n```csharp\nvar callback = new LogCallbackDelegate((level, msgPtr) =>\n{\n    string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;\n    Console.WriteLine($\"[{level}] {msg}\");\n});\n\nIntPtr fnPtr = Marshal.GetFunctionPointerForDelegate(callback);\nNativeUsesCallback(fnPtr);\nGC.KeepAlive(callback); \u002F\u002F prevent collection — fnPtr does not root the delegate\n```\n\n---\n\n## Cross-Platform Library Loading\n\nUse `NativeLibrary.SetDllImportResolver` for complex scenarios, or conditional compilation for simple cases. Use `CLong`\u002F`CULong` for C `long`\u002F`unsigned long`. Note: `CLong`\u002F`CULong` with `LibraryImport` requires `[assembly: DisableRuntimeMarshalling]`.\n\n```csharp\n\u002F\u002F Trivial: use platform naming convention\n\u002F\u002F The default naming convention adds corresponding prefix and extension when\n\u002F\u002F searching the nativelibrary. The resultant file name will be mylib.dll on\n\u002F\u002F Windows, libmylib.so on Linux and libmylib.dylib on macOS.\nprivate const string LibName = \"mylib\";\n\n\u002F\u002F Simple: conditional compilation\n\u002F\u002F WINDOWS, LINUX, MACOS are predefined only when targeting an OS-specific TFM\n\u002F\u002F (e.g., net8.0-windows). For portable TFMs (e.g., net8.0), these symbols are\n\u002F\u002F not defined — use the runtime resolver approach below instead.\n#if WINDOWS\n    private const string LibName = \"mylib.dll\";\n#elif LINUX\n    private const string LibName = \"libmylib.so\";\n#elif MACOS\n    private const string LibName = \"libmylib.dylib\";\n#endif\n\n\u002F\u002F Complex: runtime resolver\n\u002F\u002F When targeting netstandard2.0 or other frameworks when OperatingSystem.IsXXX\n\u002F\u002F is not available, use RuntimeInformation.IsOSPlatform(OSPlatform.XXX) api.\nNativeLibrary.SetDllImportResolver(typeof(MyLib).Assembly,\n    (name, assembly, searchPath) =>\n    {\n        if (name != \"mylib\") return IntPtr.Zero;\n        string libName = OperatingSystem.IsWindows()\n            ? \"mylib.dll\"\n            : OperatingSystem.IsMacOS()\n                ? \"libmylib.dylib\" : \"libmylib.so\";\n        NativeLibrary.TryLoad(libName, assembly, searchPath, out var handle);\n        return handle;\n    });\n```\n\n---\n\n## Migrating DllImport to LibraryImport\n\nFor codebases targeting .NET 7+, migrating provides AOT compatibility and trimming safety.\n\n1. Add `partial` to the containing class and make the method `static partial`\n2. Replace `[DllImport]` with `[LibraryImport]`\n3. Replace `CharSet` with `StringMarshalling`\n4. Replace `SetLastError = true` with `SetLastPInvokeError = true`\n5. Remove `CallingConvention` unless targeting Windows x86\n6. Build and fix `SYSLIB1054`–`SYSLIB1057` analyzer warnings\n\nEnable the interop analyzers:\n\n```xml\n\u003CPropertyGroup>\n    \u003CEnableTrimAnalyzer>true\u003C\u002FEnableTrimAnalyzer>\n    \u003CEnableAotAnalyzer>true\u003C\u002FEnableAotAnalyzer>\n\u003C\u002FPropertyGroup>\n```\n\n---\n\n## Tooling\n\n### CsWin32 (Win32 APIs)\n\nFor Win32 P\u002FInvoke, prefer [Microsoft.Windows.CsWin32](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FCsWin32) over hand-written signatures. It source-generates correct declarations from metadata. Add a `NativeMethods.txt` listing the APIs you need:\n\n```bash\ndotnet add package Microsoft.Windows.CsWin32\n```\n\n### CsWinRT (WinRT APIs)\n\nFor WinRT interop, use [Microsoft.Windows.CsWinRT](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FCsWinRT) to generate .NET projections from `.winmd` files.\n\n### Objective Sharpie (Objective-C APIs)\n\nFor binding Objective-C libraries (macOS\u002FiOS), use [Objective Sharpie](https:\u002F\u002Flearn.microsoft.com\u002Fprevious-versions\u002Fxamarin\u002Fcross-platform\u002Fmacios\u002Fbinding\u002Fobjective-sharpie) to generate initial P\u002FInvoke and binding definitions from Objective-C headers.\n\n---\n\n## Validation\n\n### Review checklist\n\n- [ ] Every signature matches the native header exactly (types, sizes)\n- [ ] Calling convention specified if targeting Windows x86; omitted otherwise\n- [ ] String encoding is explicit — no reliance on defaults or `CharSet.Auto`\n- [ ] Memory ownership is documented and matched (who allocates, who frees, with what)\n- [ ] `SafeHandle` used for all native handles (no raw `IntPtr` escaping the interop layer)\n- [ ] Delegates passed as callbacks are rooted to prevent GC collection\n- [ ] `SetLastError`\u002F`SetLastPInvokeError` set for APIs that use OS error codes\n- [ ] Struct layout matches native (packing, alignment, field order)\n- [ ] `CLong`\u002F`CULong` used for C `long`\u002F`unsigned long` in cross-platform code\n- [ ] If using `CLong`\u002F`CULong` with `LibraryImport`, `[assembly: DisableRuntimeMarshalling]` is applied\n- [ ] No `bool` without explicit `MarshalAs` — always specify `UnmanagedType.Bool` (4-byte) or `UnmanagedType.U1` (1-byte) to ensure normalization across the language boundary.\n\n### Runnable validation steps\n\n1. **Build with interop analyzers enabled** — confirm zero `SYSLIB1054`–`SYSLIB1057` warnings:\n   ```xml\n   \u003CEnableTrimAnalyzer>true\u003C\u002FEnableTrimAnalyzer>\n   \u003CEnableAotAnalyzer>true\u003C\u002FEnableAotAnalyzer>\n   ```\n2. **Verify struct sizes match** — for every struct crossing the boundary, assert `Marshal.SizeOf\u003CT>()` equals the native `sizeof`\n3. **Round-trip test** — call the native function with known inputs and verify expected outputs\n4. **Test with non-ASCII strings** — pass strings containing characters outside the ASCII range to confirm encoding is correct\n\n## Reference Files\n\n- **[references\u002Ftype-mapping.md](references\u002Ftype-mapping.md)** — Complete native-to-.NET type mapping table, struct layout patterns, blittable type rules. **Load when** encountering types not covered in Step 2 above, or when working with struct layout or blittable type questions.\n- **[references\u002Fdiagnostics.md](references\u002Fdiagnostics.md)** — Common pitfalls, failure modes and recovery, debugging approach, external resources. **Load when** debugging an existing P\u002FInvoke failure or reviewing interop code for correctness issues.\n",{"data":35,"body":36},{"name":4,"description":6,"license":25},{"type":37,"children":38},"root",[39,48,54,97,104,185,191,259,265,394,404,408,414,421,579,585,590,908,926,1020,1026,1031,1051,1059,1094,1102,1133,1154,1179,1237,1258,1335,1341,1424,1444,1608,1642,1648,1653,1682,1690,1783,1791,1881,1911,1928,1938,2014,2020,2039,2287,2293,2386,2392,2408,2506,2519,2527,2649,2661,2692,2768,2771,2777,2843,3105,3108,3114,3119,3223,3228,3269,3272,3278,3284,3307,3340,3346,3368,3374,3388,3391,3397,3403,3621,3627,3720,3726,3767],{"type":40,"tag":41,"props":42,"children":44},"element","h1",{"id":43},"net-pinvoke",[45],{"type":46,"value":47},"text",".NET P\u002FInvoke",{"type":40,"tag":49,"props":50,"children":51},"p",{},[52],{"type":46,"value":53},"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.",{"type":40,"tag":49,"props":55,"children":56},{},[57,59,66,68,74,76,81,83,88,90,95],{"type":46,"value":58},"This skill covers both ",{"type":40,"tag":60,"props":61,"children":63},"code",{"className":62},[],[64],{"type":46,"value":65},"DllImport",{"type":46,"value":67}," (available since .NET Framework 1.0) and ",{"type":40,"tag":60,"props":69,"children":71},{"className":70},[],[72],{"type":46,"value":73},"LibraryImport",{"type":46,"value":75}," (source-generated, .NET 7+). When targeting .NET Framework, always use ",{"type":40,"tag":60,"props":77,"children":79},{"className":78},[],[80],{"type":46,"value":65},{"type":46,"value":82},". When targeting .NET 7+, prefer ",{"type":40,"tag":60,"props":84,"children":86},{"className":85},[],[87],{"type":46,"value":73},{"type":46,"value":89}," for new code. When native AOT is a requirement, ",{"type":40,"tag":60,"props":91,"children":93},{"className":92},[],[94],{"type":46,"value":73},{"type":46,"value":96}," is the only option.",{"type":40,"tag":98,"props":99,"children":101},"h2",{"id":100},"when-to-use-this-skill",[102],{"type":46,"value":103},"When to Use This Skill",{"type":40,"tag":105,"props":106,"children":107},"ul",{},[108,130,135,140,161,180],{"type":40,"tag":109,"props":110,"children":111},"li",{},[112,114,120,122,128],{"type":46,"value":113},"Writing a new ",{"type":40,"tag":60,"props":115,"children":117},{"className":116},[],[118],{"type":46,"value":119},"[DllImport]",{"type":46,"value":121}," or ",{"type":40,"tag":60,"props":123,"children":125},{"className":124},[],[126],{"type":46,"value":127},"[LibraryImport]",{"type":46,"value":129}," declaration from a C\u002FC++ header",{"type":40,"tag":109,"props":131,"children":132},{},[133],{"type":46,"value":134},"Reviewing P\u002FInvoke signatures for correctness (type sizes, calling conventions, string encoding)",{"type":40,"tag":109,"props":136,"children":137},{},[138],{"type":46,"value":139},"Wrapping an entire C library for use from .NET",{"type":40,"tag":109,"props":141,"children":142},{},[143,145,151,153,159],{"type":46,"value":144},"Debugging ",{"type":40,"tag":60,"props":146,"children":148},{"className":147},[],[149],{"type":46,"value":150},"AccessViolationException",{"type":46,"value":152},", ",{"type":40,"tag":60,"props":154,"children":156},{"className":155},[],[157],{"type":46,"value":158},"DllNotFoundException",{"type":46,"value":160},", or silent data corruption at the native boundary",{"type":40,"tag":109,"props":162,"children":163},{},[164,166,171,173,178],{"type":46,"value":165},"Migrating ",{"type":40,"tag":60,"props":167,"children":169},{"className":168},[],[170],{"type":46,"value":65},{"type":46,"value":172}," declarations to ",{"type":40,"tag":60,"props":174,"children":176},{"className":175},[],[177],{"type":46,"value":73},{"type":46,"value":179}," for AOT\u002Ftrimming compatibility",{"type":40,"tag":109,"props":181,"children":182},{},[183],{"type":46,"value":184},"Diagnosing memory leaks or heap corruption involving native handles or buffers",{"type":40,"tag":98,"props":186,"children":188},{"id":187},"stop-signals",[189],{"type":46,"value":190},"Stop Signals",{"type":40,"tag":105,"props":192,"children":193},{},[194,205,229,239,249],{"type":40,"tag":109,"props":195,"children":196},{},[197,203],{"type":40,"tag":198,"props":199,"children":200},"strong",{},[201],{"type":46,"value":202},"Single function?",{"type":46,"value":204}," Map the signature (Steps 1-3), handle strings\u002Fmemory only if relevant, skip tooling and migration sections.",{"type":40,"tag":109,"props":206,"children":207},{},[208,213,215,220,222,227],{"type":40,"tag":198,"props":209,"children":210},{},[211],{"type":46,"value":212},"Don't migrate",{"type":46,"value":214}," existing ",{"type":40,"tag":60,"props":216,"children":218},{"className":217},[],[219],{"type":46,"value":65},{"type":46,"value":221}," to ",{"type":40,"tag":60,"props":223,"children":225},{"className":224},[],[226],{"type":46,"value":73},{"type":46,"value":228}," unless the user asks or AOT\u002Ftrimming is an explicit requirement.",{"type":40,"tag":109,"props":230,"children":231},{},[232,237],{"type":40,"tag":198,"props":233,"children":234},{},[235],{"type":46,"value":236},"Don't recommend CsWin32",{"type":46,"value":238}," unless the target is specifically Win32 APIs.",{"type":40,"tag":109,"props":240,"children":241},{},[242,247],{"type":40,"tag":198,"props":243,"children":244},{},[245],{"type":46,"value":246},"Don't generate callbacks",{"type":46,"value":248}," (Step 8) unless the native API requires function pointers.",{"type":40,"tag":109,"props":250,"children":251},{},[252,257],{"type":40,"tag":198,"props":253,"children":254},{},[255],{"type":46,"value":256},"Review request?",{"type":46,"value":258}," Use the validation checklist — don't rewrite working code.",{"type":40,"tag":98,"props":260,"children":262},{"id":261},"inputs",[263],{"type":46,"value":264},"Inputs",{"type":40,"tag":266,"props":267,"children":268},"table",{},[269,293],{"type":40,"tag":270,"props":271,"children":272},"thead",{},[273],{"type":40,"tag":274,"props":275,"children":276},"tr",{},[277,283,288],{"type":40,"tag":278,"props":279,"children":280},"th",{},[281],{"type":46,"value":282},"Input",{"type":40,"tag":278,"props":284,"children":285},{},[286],{"type":46,"value":287},"Required",{"type":40,"tag":278,"props":289,"children":290},{},[291],{"type":46,"value":292},"Description",{"type":40,"tag":294,"props":295,"children":296},"tbody",{},[297,316,344,377],{"type":40,"tag":274,"props":298,"children":299},{},[300,306,311],{"type":40,"tag":301,"props":302,"children":303},"td",{},[304],{"type":46,"value":305},"Native header or documentation",{"type":40,"tag":301,"props":307,"children":308},{},[309],{"type":46,"value":310},"Yes",{"type":40,"tag":301,"props":312,"children":313},{},[314],{"type":46,"value":315},"C\u002FC++ function signatures, struct definitions, calling conventions",{"type":40,"tag":274,"props":317,"children":318},{},[319,324,328],{"type":40,"tag":301,"props":320,"children":321},{},[322],{"type":46,"value":323},"Target framework",{"type":40,"tag":301,"props":325,"children":326},{},[327],{"type":46,"value":310},{"type":40,"tag":301,"props":329,"children":330},{},[331,333,338,339],{"type":46,"value":332},"Determines whether to use ",{"type":40,"tag":60,"props":334,"children":336},{"className":335},[],[337],{"type":46,"value":65},{"type":46,"value":121},{"type":40,"tag":60,"props":340,"children":342},{"className":341},[],[343],{"type":46,"value":73},{"type":40,"tag":274,"props":345,"children":346},{},[347,352,357],{"type":40,"tag":301,"props":348,"children":349},{},[350],{"type":46,"value":351},"Target platforms",{"type":40,"tag":301,"props":353,"children":354},{},[355],{"type":46,"value":356},"Recommended",{"type":40,"tag":301,"props":358,"children":359},{},[360,362,368,369,375],{"type":46,"value":361},"Affects type sizes (",{"type":40,"tag":60,"props":363,"children":365},{"className":364},[],[366],{"type":46,"value":367},"long",{"type":46,"value":152},{"type":40,"tag":60,"props":370,"children":372},{"className":371},[],[373],{"type":46,"value":374},"size_t",{"type":46,"value":376},") and library naming",{"type":40,"tag":274,"props":378,"children":379},{},[380,385,389],{"type":40,"tag":301,"props":381,"children":382},{},[383],{"type":46,"value":384},"Memory ownership contract",{"type":40,"tag":301,"props":386,"children":387},{},[388],{"type":46,"value":310},{"type":40,"tag":301,"props":390,"children":391},{},[392],{"type":46,"value":393},"Who allocates and who frees each buffer or handle",{"type":40,"tag":49,"props":395,"children":396},{},[397,402],{"type":40,"tag":198,"props":398,"children":399},{},[400],{"type":46,"value":401},"Agent behavior:",{"type":46,"value":403}," When documentation and native headers diverge, always trust the header. Online documentation (including official Win32 API docs) frequently omits or simplifies details about types, calling conventions, and struct layout that are critical for correct P\u002FInvoke signatures.",{"type":40,"tag":405,"props":406,"children":407},"hr",{},[],{"type":40,"tag":98,"props":409,"children":411},{"id":410},"workflow",[412],{"type":46,"value":413},"Workflow",{"type":40,"tag":415,"props":416,"children":418},"h3",{"id":417},"step-1-choose-dllimport-or-libraryimport",[419],{"type":46,"value":420},"Step 1: Choose DllImport or LibraryImport",{"type":40,"tag":266,"props":422,"children":423},{},[424,453],{"type":40,"tag":270,"props":425,"children":426},{},[427],{"type":40,"tag":274,"props":428,"children":429},{},[430,435,443],{"type":40,"tag":278,"props":431,"children":432},{},[433],{"type":46,"value":434},"Aspect",{"type":40,"tag":278,"props":436,"children":437},{},[438],{"type":40,"tag":60,"props":439,"children":441},{"className":440},[],[442],{"type":46,"value":65},{"type":40,"tag":278,"props":444,"children":445},{},[446,451],{"type":40,"tag":60,"props":447,"children":449},{"className":448},[],[450],{"type":46,"value":73},{"type":46,"value":452}," (.NET 7+)",{"type":40,"tag":294,"props":454,"children":455},{},[456,477,497,529,558],{"type":40,"tag":274,"props":457,"children":458},{},[459,467,472],{"type":40,"tag":301,"props":460,"children":461},{},[462],{"type":40,"tag":198,"props":463,"children":464},{},[465],{"type":46,"value":466},"Mechanism",{"type":40,"tag":301,"props":468,"children":469},{},[470],{"type":46,"value":471},"Runtime marshalling",{"type":40,"tag":301,"props":473,"children":474},{},[475],{"type":46,"value":476},"Source generator (compile-time)",{"type":40,"tag":274,"props":478,"children":479},{},[480,488,493],{"type":40,"tag":301,"props":481,"children":482},{},[483],{"type":40,"tag":198,"props":484,"children":485},{},[486],{"type":46,"value":487},"AOT \u002F Trim safe",{"type":40,"tag":301,"props":489,"children":490},{},[491],{"type":46,"value":492},"No",{"type":40,"tag":301,"props":494,"children":495},{},[496],{"type":46,"value":310},{"type":40,"tag":274,"props":498,"children":499},{},[500,508,519],{"type":40,"tag":301,"props":501,"children":502},{},[503],{"type":40,"tag":198,"props":504,"children":505},{},[506],{"type":46,"value":507},"String marshalling",{"type":40,"tag":301,"props":509,"children":510},{},[511,517],{"type":40,"tag":60,"props":512,"children":514},{"className":513},[],[515],{"type":46,"value":516},"CharSet",{"type":46,"value":518}," enum",{"type":40,"tag":301,"props":520,"children":521},{},[522,528],{"type":40,"tag":60,"props":523,"children":525},{"className":524},[],[526],{"type":46,"value":527},"StringMarshalling",{"type":46,"value":518},{"type":40,"tag":274,"props":530,"children":531},{},[532,540,549],{"type":40,"tag":301,"props":533,"children":534},{},[535],{"type":40,"tag":198,"props":536,"children":537},{},[538],{"type":46,"value":539},"Error handling",{"type":40,"tag":301,"props":541,"children":542},{},[543],{"type":40,"tag":60,"props":544,"children":546},{"className":545},[],[547],{"type":46,"value":548},"SetLastError",{"type":40,"tag":301,"props":550,"children":551},{},[552],{"type":40,"tag":60,"props":553,"children":555},{"className":554},[],[556],{"type":46,"value":557},"SetLastPInvokeError",{"type":40,"tag":274,"props":559,"children":560},{},[561,569,574],{"type":40,"tag":301,"props":562,"children":563},{},[564],{"type":40,"tag":198,"props":565,"children":566},{},[567],{"type":46,"value":568},"Availability",{"type":40,"tag":301,"props":570,"children":571},{},[572],{"type":46,"value":573},".NET Framework 1.0+",{"type":40,"tag":301,"props":575,"children":576},{},[577],{"type":46,"value":578},".NET 7+ only",{"type":40,"tag":415,"props":580,"children":582},{"id":581},"step-2-map-native-types-to-net-types",[583],{"type":46,"value":584},"Step 2: Map Native Types to .NET Types",{"type":40,"tag":49,"props":586,"children":587},{},[588],{"type":46,"value":589},"The most dangerous mappings — these cause the majority of bugs:",{"type":40,"tag":266,"props":591,"children":592},{},[593,614],{"type":40,"tag":270,"props":594,"children":595},{},[596],{"type":40,"tag":274,"props":597,"children":598},{},[599,604,609],{"type":40,"tag":278,"props":600,"children":601},{},[602],{"type":46,"value":603},"C \u002F Win32 Type",{"type":40,"tag":278,"props":605,"children":606},{},[607],{"type":46,"value":608},".NET Type",{"type":40,"tag":278,"props":610,"children":611},{},[612],{"type":46,"value":613},"Why",{"type":40,"tag":294,"props":615,"children":616},{},[617,658,711,754,781,820,869],{"type":40,"tag":274,"props":618,"children":619},{},[620,628,640],{"type":40,"tag":301,"props":621,"children":622},{},[623],{"type":40,"tag":60,"props":624,"children":626},{"className":625},[],[627],{"type":46,"value":367},{"type":40,"tag":301,"props":629,"children":630},{},[631],{"type":40,"tag":198,"props":632,"children":633},{},[634],{"type":40,"tag":60,"props":635,"children":637},{"className":636},[],[638],{"type":46,"value":639},"CLong",{"type":40,"tag":301,"props":641,"children":642},{},[643,645,650,652],{"type":46,"value":644},"32-bit on Windows, 64-bit on 64-bit Unix. With ",{"type":40,"tag":60,"props":646,"children":648},{"className":647},[],[649],{"type":46,"value":73},{"type":46,"value":651},", requires ",{"type":40,"tag":60,"props":653,"children":655},{"className":654},[],[656],{"type":46,"value":657},"[assembly: DisableRuntimeMarshalling]",{"type":40,"tag":274,"props":659,"children":660},{},[661,669,686],{"type":40,"tag":301,"props":662,"children":663},{},[664],{"type":40,"tag":60,"props":665,"children":667},{"className":666},[],[668],{"type":46,"value":374},{"type":40,"tag":301,"props":670,"children":671},{},[672,678,680],{"type":40,"tag":60,"props":673,"children":675},{"className":674},[],[676],{"type":46,"value":677},"nuint",{"type":46,"value":679}," \u002F ",{"type":40,"tag":60,"props":681,"children":683},{"className":682},[],[684],{"type":46,"value":685},"UIntPtr",{"type":40,"tag":301,"props":687,"children":688},{},[689,691,696,698,703,705],{"type":46,"value":690},"Pointer-sized. Use ",{"type":40,"tag":60,"props":692,"children":694},{"className":693},[],[695],{"type":46,"value":677},{"type":46,"value":697}," on .NET 8+ and ",{"type":40,"tag":60,"props":699,"children":701},{"className":700},[],[702],{"type":46,"value":685},{"type":46,"value":704}," on earlier .NET. Never use ",{"type":40,"tag":60,"props":706,"children":708},{"className":707},[],[709],{"type":46,"value":710},"ulong",{"type":40,"tag":274,"props":712,"children":713},{},[714,725,734],{"type":40,"tag":301,"props":715,"children":716},{},[717,723],{"type":40,"tag":60,"props":718,"children":720},{"className":719},[],[721],{"type":46,"value":722},"BOOL",{"type":46,"value":724}," (Win32)",{"type":40,"tag":301,"props":726,"children":727},{},[728],{"type":40,"tag":60,"props":729,"children":731},{"className":730},[],[732],{"type":46,"value":733},"int",{"type":40,"tag":301,"props":735,"children":736},{},[737,739,745,747,752],{"type":46,"value":738},"Not ",{"type":40,"tag":60,"props":740,"children":742},{"className":741},[],[743],{"type":46,"value":744},"bool",{"type":46,"value":746}," — Win32 ",{"type":40,"tag":60,"props":748,"children":750},{"className":749},[],[751],{"type":46,"value":722},{"type":46,"value":753}," is 4 bytes",{"type":40,"tag":274,"props":755,"children":756},{},[757,767,776],{"type":40,"tag":301,"props":758,"children":759},{},[760,765],{"type":40,"tag":60,"props":761,"children":763},{"className":762},[],[764],{"type":46,"value":744},{"type":46,"value":766}," (C99)",{"type":40,"tag":301,"props":768,"children":769},{},[770],{"type":40,"tag":60,"props":771,"children":773},{"className":772},[],[774],{"type":46,"value":775},"[MarshalAs(UnmanagedType.U1)] bool",{"type":40,"tag":301,"props":777,"children":778},{},[779],{"type":46,"value":780},"Must specify 1-byte marshal",{"type":40,"tag":274,"props":782,"children":783},{},[784,800,809],{"type":40,"tag":301,"props":785,"children":786},{},[787,793,794],{"type":40,"tag":60,"props":788,"children":790},{"className":789},[],[791],{"type":46,"value":792},"HANDLE",{"type":46,"value":152},{"type":40,"tag":60,"props":795,"children":797},{"className":796},[],[798],{"type":46,"value":799},"HWND",{"type":40,"tag":301,"props":801,"children":802},{},[803],{"type":40,"tag":60,"props":804,"children":806},{"className":805},[],[807],{"type":46,"value":808},"SafeHandle",{"type":40,"tag":301,"props":810,"children":811},{},[812,814],{"type":46,"value":813},"Prefer over raw ",{"type":40,"tag":60,"props":815,"children":817},{"className":816},[],[818],{"type":46,"value":819},"IntPtr",{"type":40,"tag":274,"props":821,"children":822},{},[823,839,848],{"type":40,"tag":301,"props":824,"children":825},{},[826,832,833],{"type":40,"tag":60,"props":827,"children":829},{"className":828},[],[830],{"type":46,"value":831},"LPWSTR",{"type":46,"value":679},{"type":40,"tag":60,"props":834,"children":836},{"className":835},[],[837],{"type":46,"value":838},"wchar_t*",{"type":40,"tag":301,"props":840,"children":841},{},[842],{"type":40,"tag":60,"props":843,"children":845},{"className":844},[],[846],{"type":46,"value":847},"string",{"type":40,"tag":301,"props":849,"children":850},{},[851,853,859,861,867],{"type":46,"value":852},"UTF-16 on Windows (lowest cost for ",{"type":40,"tag":60,"props":854,"children":856},{"className":855},[],[857],{"type":46,"value":858},"in",{"type":46,"value":860}," strings). Avoid in cross-platform code — ",{"type":40,"tag":60,"props":862,"children":864},{"className":863},[],[865],{"type":46,"value":866},"wchar_t",{"type":46,"value":868}," width is compiler-defined (typically UTF-32 on non-Windows)",{"type":40,"tag":274,"props":870,"children":871},{},[872,888,896],{"type":40,"tag":301,"props":873,"children":874},{},[875,881,882],{"type":40,"tag":60,"props":876,"children":878},{"className":877},[],[879],{"type":46,"value":880},"LPSTR",{"type":46,"value":679},{"type":40,"tag":60,"props":883,"children":885},{"className":884},[],[886],{"type":46,"value":887},"char*",{"type":40,"tag":301,"props":889,"children":890},{},[891],{"type":40,"tag":60,"props":892,"children":894},{"className":893},[],[895],{"type":46,"value":847},{"type":40,"tag":301,"props":897,"children":898},{},[899,901,906],{"type":46,"value":900},"Must specify encoding (ANSI or UTF-8). Always requires marshalling cost for ",{"type":40,"tag":60,"props":902,"children":904},{"className":903},[],[905],{"type":46,"value":858},{"type":46,"value":907}," parameters",{"type":40,"tag":49,"props":909,"children":910},{},[911,916,918,924],{"type":40,"tag":198,"props":912,"children":913},{},[914],{"type":46,"value":915},"For the complete type mapping table, struct layout, and blittable type rules",{"type":46,"value":917},", see ",{"type":40,"tag":919,"props":920,"children":922},"a",{"href":921},"references\u002Ftype-mapping.md",[923],{"type":46,"value":921},{"type":46,"value":925},".",{"type":40,"tag":927,"props":928,"children":929},"blockquote",{},[930],{"type":40,"tag":49,"props":931,"children":932},{},[933,935,940,942,947,948,953,955,960,962,967,969,973,974,979,981,986,988,993,994,999,1000,1004,1005,1010,1012,1018],{"type":46,"value":934},"❌ ",{"type":40,"tag":198,"props":936,"children":937},{},[938],{"type":46,"value":939},"NEVER",{"type":46,"value":941}," use ",{"type":40,"tag":60,"props":943,"children":945},{"className":944},[],[946],{"type":46,"value":733},{"type":46,"value":121},{"type":40,"tag":60,"props":949,"children":951},{"className":950},[],[952],{"type":46,"value":367},{"type":46,"value":954}," for C ",{"type":40,"tag":60,"props":956,"children":958},{"className":957},[],[959],{"type":46,"value":367},{"type":46,"value":961}," — it's 32-bit on Windows, 64-bit on Unix. Always use ",{"type":40,"tag":60,"props":963,"children":965},{"className":964},[],[966],{"type":46,"value":639},{"type":46,"value":968},".\n❌ ",{"type":40,"tag":198,"props":970,"children":971},{},[972],{"type":46,"value":939},{"type":46,"value":941},{"type":40,"tag":60,"props":975,"children":977},{"className":976},[],[978],{"type":46,"value":710},{"type":46,"value":980}," for ",{"type":40,"tag":60,"props":982,"children":984},{"className":983},[],[985],{"type":46,"value":374},{"type":46,"value":987}," — causes stack corruption on 32-bit. Use ",{"type":40,"tag":60,"props":989,"children":991},{"className":990},[],[992],{"type":46,"value":677},{"type":46,"value":121},{"type":40,"tag":60,"props":995,"children":997},{"className":996},[],[998],{"type":46,"value":685},{"type":46,"value":968},{"type":40,"tag":198,"props":1001,"children":1002},{},[1003],{"type":46,"value":939},{"type":46,"value":941},{"type":40,"tag":60,"props":1006,"children":1008},{"className":1007},[],[1009],{"type":46,"value":744},{"type":46,"value":1011}," without ",{"type":40,"tag":60,"props":1013,"children":1015},{"className":1014},[],[1016],{"type":46,"value":1017},"MarshalAs",{"type":46,"value":1019}," — the default marshal size is wrong.",{"type":40,"tag":415,"props":1021,"children":1023},{"id":1022},"step-3-write-the-declaration",[1024],{"type":46,"value":1025},"Step 3: Write the Declaration",{"type":40,"tag":49,"props":1027,"children":1028},{},[1029],{"type":46,"value":1030},"Given a C header:",{"type":40,"tag":1032,"props":1033,"children":1037},"pre",{"className":1034,"code":1035,"language":18,"meta":1036,"style":1036},"language-c shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","int32_t process_records(const Record* records, size_t count, uint32_t* out_processed);\n","",[1038],{"type":40,"tag":60,"props":1039,"children":1040},{"__ignoreMap":1036},[1041],{"type":40,"tag":1042,"props":1043,"children":1046},"span",{"class":1044,"line":1045},"line",1,[1047],{"type":40,"tag":1042,"props":1048,"children":1049},{},[1050],{"type":46,"value":1035},{"type":40,"tag":49,"props":1052,"children":1053},{},[1054],{"type":40,"tag":198,"props":1055,"children":1056},{},[1057],{"type":46,"value":1058},"DllImport:",{"type":40,"tag":1032,"props":1060,"children":1064},{"className":1061,"code":1062,"language":1063,"meta":1036,"style":1036},"language-csharp shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","[DllImport(\"mylib\")]\nprivate static extern int ProcessRecords(\n    [In] Record[] records, UIntPtr count, out uint outProcessed);\n","csharp",[1065],{"type":40,"tag":60,"props":1066,"children":1067},{"__ignoreMap":1036},[1068,1076,1085],{"type":40,"tag":1042,"props":1069,"children":1070},{"class":1044,"line":1045},[1071],{"type":40,"tag":1042,"props":1072,"children":1073},{},[1074],{"type":46,"value":1075},"[DllImport(\"mylib\")]\n",{"type":40,"tag":1042,"props":1077,"children":1079},{"class":1044,"line":1078},2,[1080],{"type":40,"tag":1042,"props":1081,"children":1082},{},[1083],{"type":46,"value":1084},"private static extern int ProcessRecords(\n",{"type":40,"tag":1042,"props":1086,"children":1088},{"class":1044,"line":1087},3,[1089],{"type":40,"tag":1042,"props":1090,"children":1091},{},[1092],{"type":46,"value":1093},"    [In] Record[] records, UIntPtr count, out uint outProcessed);\n",{"type":40,"tag":49,"props":1095,"children":1096},{},[1097],{"type":40,"tag":198,"props":1098,"children":1099},{},[1100],{"type":46,"value":1101},"LibraryImport:",{"type":40,"tag":1032,"props":1103,"children":1105},{"className":1061,"code":1104,"language":1063,"meta":1036,"style":1036},"[LibraryImport(\"mylib\")]\ninternal static partial int ProcessRecords(\n    [In] Record[] records, nuint count, out uint outProcessed);\n",[1106],{"type":40,"tag":60,"props":1107,"children":1108},{"__ignoreMap":1036},[1109,1117,1125],{"type":40,"tag":1042,"props":1110,"children":1111},{"class":1044,"line":1045},[1112],{"type":40,"tag":1042,"props":1113,"children":1114},{},[1115],{"type":46,"value":1116},"[LibraryImport(\"mylib\")]\n",{"type":40,"tag":1042,"props":1118,"children":1119},{"class":1044,"line":1078},[1120],{"type":40,"tag":1042,"props":1121,"children":1122},{},[1123],{"type":46,"value":1124},"internal static partial int ProcessRecords(\n",{"type":40,"tag":1042,"props":1126,"children":1127},{"class":1044,"line":1087},[1128],{"type":40,"tag":1042,"props":1129,"children":1130},{},[1131],{"type":46,"value":1132},"    [In] Record[] records, nuint count, out uint outProcessed);\n",{"type":40,"tag":49,"props":1134,"children":1135},{},[1136,1138,1144,1146,1152],{"type":46,"value":1137},"Calling conventions only need to be specified when targeting Windows x86 (32-bit), where ",{"type":40,"tag":60,"props":1139,"children":1141},{"className":1140},[],[1142],{"type":46,"value":1143},"Cdecl",{"type":46,"value":1145}," and ",{"type":40,"tag":60,"props":1147,"children":1149},{"className":1148},[],[1150],{"type":46,"value":1151},"StdCall",{"type":46,"value":1153}," differ. On x64, ARM, and ARM64, there is a single calling convention and the attribute is unnecessary.",{"type":40,"tag":49,"props":1155,"children":1156},{},[1157,1161,1163,1169,1171,1177],{"type":40,"tag":198,"props":1158,"children":1159},{},[1160],{"type":46,"value":401},{"type":46,"value":1162}," If you detect that Windows x86 is a target — through project properties (e.g., ",{"type":40,"tag":60,"props":1164,"children":1166},{"className":1165},[],[1167],{"type":46,"value":1168},"\u003CPlatformTarget>x86\u003C\u002FPlatformTarget>",{"type":46,"value":1170},"), runtime identifiers (e.g., ",{"type":40,"tag":60,"props":1172,"children":1174},{"className":1173},[],[1175],{"type":46,"value":1176},"win-x86",{"type":46,"value":1178},"), build scripts, comments, or developer instructions — flag this to the developer and recommend explicit calling conventions on all P\u002FInvoke declarations.",{"type":40,"tag":1032,"props":1180,"children":1182},{"className":1061,"code":1181,"language":1063,"meta":1036,"style":1036},"\u002F\u002F DllImport (x86 targets)\n[DllImport(\"mylib\", CallingConvention = CallingConvention.Cdecl)]\n\n\u002F\u002F LibraryImport (x86 targets)\n[LibraryImport(\"mylib\")]\n[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]\n",[1183],{"type":40,"tag":60,"props":1184,"children":1185},{"__ignoreMap":1036},[1186,1194,1202,1211,1220,1228],{"type":40,"tag":1042,"props":1187,"children":1188},{"class":1044,"line":1045},[1189],{"type":40,"tag":1042,"props":1190,"children":1191},{},[1192],{"type":46,"value":1193},"\u002F\u002F DllImport (x86 targets)\n",{"type":40,"tag":1042,"props":1195,"children":1196},{"class":1044,"line":1078},[1197],{"type":40,"tag":1042,"props":1198,"children":1199},{},[1200],{"type":46,"value":1201},"[DllImport(\"mylib\", CallingConvention = CallingConvention.Cdecl)]\n",{"type":40,"tag":1042,"props":1203,"children":1204},{"class":1044,"line":1087},[1205],{"type":40,"tag":1042,"props":1206,"children":1208},{"emptyLinePlaceholder":1207},true,[1209],{"type":46,"value":1210},"\n",{"type":40,"tag":1042,"props":1212,"children":1214},{"class":1044,"line":1213},4,[1215],{"type":40,"tag":1042,"props":1216,"children":1217},{},[1218],{"type":46,"value":1219},"\u002F\u002F LibraryImport (x86 targets)\n",{"type":40,"tag":1042,"props":1221,"children":1223},{"class":1044,"line":1222},5,[1224],{"type":40,"tag":1042,"props":1225,"children":1226},{},[1227],{"type":46,"value":1116},{"type":40,"tag":1042,"props":1229,"children":1231},{"class":1044,"line":1230},6,[1232],{"type":40,"tag":1042,"props":1233,"children":1234},{},[1235],{"type":46,"value":1236},"[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]\n",{"type":40,"tag":49,"props":1238,"children":1239},{},[1240,1242,1248,1250,1256],{"type":46,"value":1241},"If the managed method name differs from the native export name, specify ",{"type":40,"tag":60,"props":1243,"children":1245},{"className":1244},[],[1246],{"type":46,"value":1247},"EntryPoint",{"type":46,"value":1249}," to avoid ",{"type":40,"tag":60,"props":1251,"children":1253},{"className":1252},[],[1254],{"type":46,"value":1255},"EntryPointNotFoundException",{"type":46,"value":1257},":",{"type":40,"tag":1032,"props":1259,"children":1261},{"className":1061,"code":1260,"language":1063,"meta":1036,"style":1036},"\u002F\u002F DllImport\n[DllImport(\"mylib\", EntryPoint = \"process_records\")]\nprivate static extern int ProcessRecords(\n    [In] Record[] records, UIntPtr count, out uint outProcessed);\n\n\u002F\u002F LibraryImport\n[LibraryImport(\"mylib\", EntryPoint = \"process_records\")]\ninternal static partial int ProcessRecords(\n    [In] Record[] records, nuint count, out uint outProcessed);\n",[1262],{"type":40,"tag":60,"props":1263,"children":1264},{"__ignoreMap":1036},[1265,1273,1281,1288,1295,1302,1310,1319,1327],{"type":40,"tag":1042,"props":1266,"children":1267},{"class":1044,"line":1045},[1268],{"type":40,"tag":1042,"props":1269,"children":1270},{},[1271],{"type":46,"value":1272},"\u002F\u002F DllImport\n",{"type":40,"tag":1042,"props":1274,"children":1275},{"class":1044,"line":1078},[1276],{"type":40,"tag":1042,"props":1277,"children":1278},{},[1279],{"type":46,"value":1280},"[DllImport(\"mylib\", EntryPoint = \"process_records\")]\n",{"type":40,"tag":1042,"props":1282,"children":1283},{"class":1044,"line":1087},[1284],{"type":40,"tag":1042,"props":1285,"children":1286},{},[1287],{"type":46,"value":1084},{"type":40,"tag":1042,"props":1289,"children":1290},{"class":1044,"line":1213},[1291],{"type":40,"tag":1042,"props":1292,"children":1293},{},[1294],{"type":46,"value":1093},{"type":40,"tag":1042,"props":1296,"children":1297},{"class":1044,"line":1222},[1298],{"type":40,"tag":1042,"props":1299,"children":1300},{"emptyLinePlaceholder":1207},[1301],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1303,"children":1304},{"class":1044,"line":1230},[1305],{"type":40,"tag":1042,"props":1306,"children":1307},{},[1308],{"type":46,"value":1309},"\u002F\u002F LibraryImport\n",{"type":40,"tag":1042,"props":1311,"children":1313},{"class":1044,"line":1312},7,[1314],{"type":40,"tag":1042,"props":1315,"children":1316},{},[1317],{"type":46,"value":1318},"[LibraryImport(\"mylib\", EntryPoint = \"process_records\")]\n",{"type":40,"tag":1042,"props":1320,"children":1322},{"class":1044,"line":1321},8,[1323],{"type":40,"tag":1042,"props":1324,"children":1325},{},[1326],{"type":46,"value":1124},{"type":40,"tag":1042,"props":1328,"children":1330},{"class":1044,"line":1329},9,[1331],{"type":40,"tag":1042,"props":1332,"children":1333},{},[1334],{"type":46,"value":1132},{"type":40,"tag":415,"props":1336,"children":1338},{"id":1337},"step-4-handle-strings-correctly",[1339],{"type":46,"value":1340},"Step 4: Handle Strings Correctly",{"type":40,"tag":1342,"props":1343,"children":1344},"ol",{},[1345,1355,1381,1391,1408],{"type":40,"tag":109,"props":1346,"children":1347},{},[1348,1353],{"type":40,"tag":198,"props":1349,"children":1350},{},[1351],{"type":46,"value":1352},"Know what encoding the native function expects.",{"type":46,"value":1354}," There is no safe default.",{"type":40,"tag":109,"props":1356,"children":1357},{},[1358,1363,1365,1371,1373,1379],{"type":40,"tag":198,"props":1359,"children":1360},{},[1361],{"type":46,"value":1362},"Windows APIs:",{"type":46,"value":1364}," Always call the ",{"type":40,"tag":60,"props":1366,"children":1368},{"className":1367},[],[1369],{"type":46,"value":1370},"W",{"type":46,"value":1372}," (UTF-16) variant. The ",{"type":40,"tag":60,"props":1374,"children":1376},{"className":1375},[],[1377],{"type":46,"value":1378},"A",{"type":46,"value":1380}," variant needs a specific reason and explicit ANSI encoding.",{"type":40,"tag":109,"props":1382,"children":1383},{},[1384,1389],{"type":40,"tag":198,"props":1385,"children":1386},{},[1387],{"type":46,"value":1388},"Cross-platform C libraries:",{"type":46,"value":1390}," Usually expect UTF-8.",{"type":40,"tag":109,"props":1392,"children":1393},{},[1394,1399,1401,1407],{"type":40,"tag":198,"props":1395,"children":1396},{},[1397],{"type":46,"value":1398},"Specify encoding explicitly.",{"type":46,"value":1400}," Never rely on ",{"type":40,"tag":60,"props":1402,"children":1404},{"className":1403},[],[1405],{"type":46,"value":1406},"CharSet.Auto",{"type":46,"value":925},{"type":40,"tag":109,"props":1409,"children":1410},{},[1411],{"type":40,"tag":198,"props":1412,"children":1413},{},[1414,1416,1422],{"type":46,"value":1415},"Never introduce ",{"type":40,"tag":60,"props":1417,"children":1419},{"className":1418},[],[1420],{"type":46,"value":1421},"StringBuilder",{"type":46,"value":1423}," for output buffers.",{"type":40,"tag":927,"props":1425,"children":1426},{},[1427],{"type":40,"tag":49,"props":1428,"children":1429},{},[1430,1431,1435,1437,1442],{"type":46,"value":934},{"type":40,"tag":198,"props":1432,"children":1433},{},[1434],{"type":46,"value":939},{"type":46,"value":1436}," rely on ",{"type":40,"tag":60,"props":1438,"children":1440},{"className":1439},[],[1441],{"type":46,"value":1406},{"type":46,"value":1443}," or omit string encoding — there is no safe default.",{"type":40,"tag":1032,"props":1445,"children":1447},{"className":1061,"code":1446,"language":1063,"meta":1036,"style":1036},"\u002F\u002F DllImport — Windows API (UTF-16)\n[DllImport(\"kernel32.dll\", CharSet = CharSet.Unicode, SetLastError = true)]\nprivate static extern int GetModuleFileNameW(\n    IntPtr hModule, [Out] char[] filename, int size);\n\n\u002F\u002F DllImport — Cross-platform C library (UTF-8)\n[DllImport(\"mylib\")]\nprivate static extern int SetName(\n    [MarshalAs(UnmanagedType.LPUTF8Str)] string name);\n\n\u002F\u002F LibraryImport — UTF-16\n[LibraryImport(\"kernel32\", StringMarshalling = StringMarshalling.Utf16,\n    SetLastPInvokeError = true)]\ninternal static partial int GetModuleFileNameW(\n    IntPtr hModule, [Out] char[] filename, int size);\n\n\u002F\u002F LibraryImport — UTF-8\n[LibraryImport(\"mylib\", StringMarshalling = StringMarshalling.Utf8)]\ninternal static partial int SetName(string name);\n",[1448],{"type":40,"tag":60,"props":1449,"children":1450},{"__ignoreMap":1036},[1451,1459,1467,1475,1483,1490,1498,1505,1513,1521,1529,1538,1547,1556,1565,1573,1581,1590,1599],{"type":40,"tag":1042,"props":1452,"children":1453},{"class":1044,"line":1045},[1454],{"type":40,"tag":1042,"props":1455,"children":1456},{},[1457],{"type":46,"value":1458},"\u002F\u002F DllImport — Windows API (UTF-16)\n",{"type":40,"tag":1042,"props":1460,"children":1461},{"class":1044,"line":1078},[1462],{"type":40,"tag":1042,"props":1463,"children":1464},{},[1465],{"type":46,"value":1466},"[DllImport(\"kernel32.dll\", CharSet = CharSet.Unicode, SetLastError = true)]\n",{"type":40,"tag":1042,"props":1468,"children":1469},{"class":1044,"line":1087},[1470],{"type":40,"tag":1042,"props":1471,"children":1472},{},[1473],{"type":46,"value":1474},"private static extern int GetModuleFileNameW(\n",{"type":40,"tag":1042,"props":1476,"children":1477},{"class":1044,"line":1213},[1478],{"type":40,"tag":1042,"props":1479,"children":1480},{},[1481],{"type":46,"value":1482},"    IntPtr hModule, [Out] char[] filename, int size);\n",{"type":40,"tag":1042,"props":1484,"children":1485},{"class":1044,"line":1222},[1486],{"type":40,"tag":1042,"props":1487,"children":1488},{"emptyLinePlaceholder":1207},[1489],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1491,"children":1492},{"class":1044,"line":1230},[1493],{"type":40,"tag":1042,"props":1494,"children":1495},{},[1496],{"type":46,"value":1497},"\u002F\u002F DllImport — Cross-platform C library (UTF-8)\n",{"type":40,"tag":1042,"props":1499,"children":1500},{"class":1044,"line":1312},[1501],{"type":40,"tag":1042,"props":1502,"children":1503},{},[1504],{"type":46,"value":1075},{"type":40,"tag":1042,"props":1506,"children":1507},{"class":1044,"line":1321},[1508],{"type":40,"tag":1042,"props":1509,"children":1510},{},[1511],{"type":46,"value":1512},"private static extern int SetName(\n",{"type":40,"tag":1042,"props":1514,"children":1515},{"class":1044,"line":1329},[1516],{"type":40,"tag":1042,"props":1517,"children":1518},{},[1519],{"type":46,"value":1520},"    [MarshalAs(UnmanagedType.LPUTF8Str)] string name);\n",{"type":40,"tag":1042,"props":1522,"children":1524},{"class":1044,"line":1523},10,[1525],{"type":40,"tag":1042,"props":1526,"children":1527},{"emptyLinePlaceholder":1207},[1528],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1530,"children":1532},{"class":1044,"line":1531},11,[1533],{"type":40,"tag":1042,"props":1534,"children":1535},{},[1536],{"type":46,"value":1537},"\u002F\u002F LibraryImport — UTF-16\n",{"type":40,"tag":1042,"props":1539,"children":1541},{"class":1044,"line":1540},12,[1542],{"type":40,"tag":1042,"props":1543,"children":1544},{},[1545],{"type":46,"value":1546},"[LibraryImport(\"kernel32\", StringMarshalling = StringMarshalling.Utf16,\n",{"type":40,"tag":1042,"props":1548,"children":1550},{"class":1044,"line":1549},13,[1551],{"type":40,"tag":1042,"props":1552,"children":1553},{},[1554],{"type":46,"value":1555},"    SetLastPInvokeError = true)]\n",{"type":40,"tag":1042,"props":1557,"children":1559},{"class":1044,"line":1558},14,[1560],{"type":40,"tag":1042,"props":1561,"children":1562},{},[1563],{"type":46,"value":1564},"internal static partial int GetModuleFileNameW(\n",{"type":40,"tag":1042,"props":1566,"children":1568},{"class":1044,"line":1567},15,[1569],{"type":40,"tag":1042,"props":1570,"children":1571},{},[1572],{"type":46,"value":1482},{"type":40,"tag":1042,"props":1574,"children":1576},{"class":1044,"line":1575},16,[1577],{"type":40,"tag":1042,"props":1578,"children":1579},{"emptyLinePlaceholder":1207},[1580],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1582,"children":1584},{"class":1044,"line":1583},17,[1585],{"type":40,"tag":1042,"props":1586,"children":1587},{},[1588],{"type":46,"value":1589},"\u002F\u002F LibraryImport — UTF-8\n",{"type":40,"tag":1042,"props":1591,"children":1593},{"class":1044,"line":1592},18,[1594],{"type":40,"tag":1042,"props":1595,"children":1596},{},[1597],{"type":46,"value":1598},"[LibraryImport(\"mylib\", StringMarshalling = StringMarshalling.Utf8)]\n",{"type":40,"tag":1042,"props":1600,"children":1602},{"class":1044,"line":1601},19,[1603],{"type":40,"tag":1042,"props":1604,"children":1605},{},[1606],{"type":46,"value":1607},"internal static partial int SetName(string name);\n",{"type":40,"tag":49,"props":1609,"children":1610},{},[1611,1616,1618,1624,1626,1632,1634,1640],{"type":40,"tag":198,"props":1612,"children":1613},{},[1614],{"type":46,"value":1615},"String lifetime warning:",{"type":46,"value":1617}," Marshalled strings are freed after the call returns. If native code stores the pointer (instead of copying), the lifetime must be manually managed. On Windows or .NET Framework, ",{"type":40,"tag":60,"props":1619,"children":1621},{"className":1620},[],[1622],{"type":46,"value":1623},"CoTaskMemAlloc",{"type":46,"value":1625},"\u002F",{"type":40,"tag":60,"props":1627,"children":1629},{"className":1628},[],[1630],{"type":46,"value":1631},"CoTaskMemFree",{"type":46,"value":1633}," is the first choice for cross-boundary ownership; on non-Windows targets, use ",{"type":40,"tag":60,"props":1635,"children":1637},{"className":1636},[],[1638],{"type":46,"value":1639},"NativeMemory",{"type":46,"value":1641}," APIs. The library may have its own allocator that must be used instead.",{"type":40,"tag":415,"props":1643,"children":1645},{"id":1644},"step-5-establish-memory-ownership",[1646],{"type":46,"value":1647},"Step 5: Establish Memory Ownership",{"type":40,"tag":49,"props":1649,"children":1650},{},[1651],{"type":46,"value":1652},"When memory crosses the boundary, exactly one side must own it — and both sides must agree.",{"type":40,"tag":927,"props":1654,"children":1655},{},[1656],{"type":40,"tag":49,"props":1657,"children":1658},{},[1659,1660,1664,1666,1672,1674,1680],{"type":46,"value":934},{"type":40,"tag":198,"props":1661,"children":1662},{},[1663],{"type":46,"value":939},{"type":46,"value":1665}," free with a mismatched allocator — ",{"type":40,"tag":60,"props":1667,"children":1669},{"className":1668},[],[1670],{"type":46,"value":1671},"Marshal.FreeHGlobal",{"type":46,"value":1673}," on ",{"type":40,"tag":60,"props":1675,"children":1677},{"className":1676},[],[1678],{"type":46,"value":1679},"malloc",{"type":46,"value":1681},"'d memory is heap corruption.",{"type":40,"tag":49,"props":1683,"children":1684},{},[1685],{"type":40,"tag":198,"props":1686,"children":1687},{},[1688],{"type":46,"value":1689},"Model 1 — Caller allocates, caller frees (safest):",{"type":40,"tag":1032,"props":1691,"children":1693},{"className":1061,"code":1692,"language":1063,"meta":1036,"style":1036},"[LibraryImport(\"mylib\")]\nprivate static partial int GetName(\n    Span\u003Cbyte> buffer, nuint bufferSize, out nuint actualSize);\n\npublic static string GetName()\n{\n    Span\u003Cbyte> buffer = stackalloc byte[256];\n    int result = GetName(buffer, (nuint)buffer.Length, out nuint actualSize);\n    if (result != 0) throw new InvalidOperationException($\"Failed: {result}\");\n    return Encoding.UTF8.GetString(buffer[..(int)actualSize]);\n}\n",[1694],{"type":40,"tag":60,"props":1695,"children":1696},{"__ignoreMap":1036},[1697,1704,1712,1720,1727,1735,1743,1751,1759,1767,1775],{"type":40,"tag":1042,"props":1698,"children":1699},{"class":1044,"line":1045},[1700],{"type":40,"tag":1042,"props":1701,"children":1702},{},[1703],{"type":46,"value":1116},{"type":40,"tag":1042,"props":1705,"children":1706},{"class":1044,"line":1078},[1707],{"type":40,"tag":1042,"props":1708,"children":1709},{},[1710],{"type":46,"value":1711},"private static partial int GetName(\n",{"type":40,"tag":1042,"props":1713,"children":1714},{"class":1044,"line":1087},[1715],{"type":40,"tag":1042,"props":1716,"children":1717},{},[1718],{"type":46,"value":1719},"    Span\u003Cbyte> buffer, nuint bufferSize, out nuint actualSize);\n",{"type":40,"tag":1042,"props":1721,"children":1722},{"class":1044,"line":1213},[1723],{"type":40,"tag":1042,"props":1724,"children":1725},{"emptyLinePlaceholder":1207},[1726],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1728,"children":1729},{"class":1044,"line":1222},[1730],{"type":40,"tag":1042,"props":1731,"children":1732},{},[1733],{"type":46,"value":1734},"public static string GetName()\n",{"type":40,"tag":1042,"props":1736,"children":1737},{"class":1044,"line":1230},[1738],{"type":40,"tag":1042,"props":1739,"children":1740},{},[1741],{"type":46,"value":1742},"{\n",{"type":40,"tag":1042,"props":1744,"children":1745},{"class":1044,"line":1312},[1746],{"type":40,"tag":1042,"props":1747,"children":1748},{},[1749],{"type":46,"value":1750},"    Span\u003Cbyte> buffer = stackalloc byte[256];\n",{"type":40,"tag":1042,"props":1752,"children":1753},{"class":1044,"line":1321},[1754],{"type":40,"tag":1042,"props":1755,"children":1756},{},[1757],{"type":46,"value":1758},"    int result = GetName(buffer, (nuint)buffer.Length, out nuint actualSize);\n",{"type":40,"tag":1042,"props":1760,"children":1761},{"class":1044,"line":1329},[1762],{"type":40,"tag":1042,"props":1763,"children":1764},{},[1765],{"type":46,"value":1766},"    if (result != 0) throw new InvalidOperationException($\"Failed: {result}\");\n",{"type":40,"tag":1042,"props":1768,"children":1769},{"class":1044,"line":1523},[1770],{"type":40,"tag":1042,"props":1771,"children":1772},{},[1773],{"type":46,"value":1774},"    return Encoding.UTF8.GetString(buffer[..(int)actualSize]);\n",{"type":40,"tag":1042,"props":1776,"children":1777},{"class":1044,"line":1531},[1778],{"type":40,"tag":1042,"props":1779,"children":1780},{},[1781],{"type":46,"value":1782},"}\n",{"type":40,"tag":49,"props":1784,"children":1785},{},[1786],{"type":40,"tag":198,"props":1787,"children":1788},{},[1789],{"type":46,"value":1790},"Model 2 — Callee allocates, caller frees (common in Win32):",{"type":40,"tag":1032,"props":1792,"children":1794},{"className":1061,"code":1793,"language":1063,"meta":1036,"style":1036},"[LibraryImport(\"mylib\")]\nprivate static partial IntPtr GetVersion();\n[LibraryImport(\"mylib\")]\nprivate static partial void FreeString(IntPtr s);\n\npublic static string GetVersion()\n{\n    IntPtr ptr = GetVersion();\n    try { return Marshal.PtrToStringUTF8(ptr) ?? throw new InvalidOperationException(); }\n    finally { FreeString(ptr); } \u002F\u002F Must use the library's own free function\n}\n",[1795],{"type":40,"tag":60,"props":1796,"children":1797},{"__ignoreMap":1036},[1798,1805,1813,1820,1828,1835,1843,1850,1858,1866,1874],{"type":40,"tag":1042,"props":1799,"children":1800},{"class":1044,"line":1045},[1801],{"type":40,"tag":1042,"props":1802,"children":1803},{},[1804],{"type":46,"value":1116},{"type":40,"tag":1042,"props":1806,"children":1807},{"class":1044,"line":1078},[1808],{"type":40,"tag":1042,"props":1809,"children":1810},{},[1811],{"type":46,"value":1812},"private static partial IntPtr GetVersion();\n",{"type":40,"tag":1042,"props":1814,"children":1815},{"class":1044,"line":1087},[1816],{"type":40,"tag":1042,"props":1817,"children":1818},{},[1819],{"type":46,"value":1116},{"type":40,"tag":1042,"props":1821,"children":1822},{"class":1044,"line":1213},[1823],{"type":40,"tag":1042,"props":1824,"children":1825},{},[1826],{"type":46,"value":1827},"private static partial void FreeString(IntPtr s);\n",{"type":40,"tag":1042,"props":1829,"children":1830},{"class":1044,"line":1222},[1831],{"type":40,"tag":1042,"props":1832,"children":1833},{"emptyLinePlaceholder":1207},[1834],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1836,"children":1837},{"class":1044,"line":1230},[1838],{"type":40,"tag":1042,"props":1839,"children":1840},{},[1841],{"type":46,"value":1842},"public static string GetVersion()\n",{"type":40,"tag":1042,"props":1844,"children":1845},{"class":1044,"line":1312},[1846],{"type":40,"tag":1042,"props":1847,"children":1848},{},[1849],{"type":46,"value":1742},{"type":40,"tag":1042,"props":1851,"children":1852},{"class":1044,"line":1321},[1853],{"type":40,"tag":1042,"props":1854,"children":1855},{},[1856],{"type":46,"value":1857},"    IntPtr ptr = GetVersion();\n",{"type":40,"tag":1042,"props":1859,"children":1860},{"class":1044,"line":1329},[1861],{"type":40,"tag":1042,"props":1862,"children":1863},{},[1864],{"type":46,"value":1865},"    try { return Marshal.PtrToStringUTF8(ptr) ?? throw new InvalidOperationException(); }\n",{"type":40,"tag":1042,"props":1867,"children":1868},{"class":1044,"line":1523},[1869],{"type":40,"tag":1042,"props":1870,"children":1871},{},[1872],{"type":46,"value":1873},"    finally { FreeString(ptr); } \u002F\u002F Must use the library's own free function\n",{"type":40,"tag":1042,"props":1875,"children":1876},{"class":1044,"line":1531},[1877],{"type":40,"tag":1042,"props":1878,"children":1879},{},[1880],{"type":46,"value":1782},{"type":40,"tag":49,"props":1882,"children":1883},{},[1884,1889,1891,1896,1897,1903,1904,1909],{"type":40,"tag":198,"props":1885,"children":1886},{},[1887],{"type":46,"value":1888},"Critical rule:",{"type":46,"value":1890}," Always free with the matching allocator. Never use ",{"type":40,"tag":60,"props":1892,"children":1894},{"className":1893},[],[1895],{"type":46,"value":1671},{"type":46,"value":121},{"type":40,"tag":60,"props":1898,"children":1900},{"className":1899},[],[1901],{"type":46,"value":1902},"Marshal.FreeCoTaskMem",{"type":46,"value":1673},{"type":40,"tag":60,"props":1905,"children":1907},{"className":1906},[],[1908],{"type":46,"value":1679},{"type":46,"value":1910},"'d memory.",{"type":40,"tag":49,"props":1912,"children":1913},{},[1914,1919,1921,1926],{"type":40,"tag":198,"props":1915,"children":1916},{},[1917],{"type":46,"value":1918},"Model 3 — Handle-based (callee allocates, callee frees):",{"type":46,"value":1920}," Use ",{"type":40,"tag":60,"props":1922,"children":1924},{"className":1923},[],[1925],{"type":46,"value":808},{"type":46,"value":1927}," (see Step 6).",{"type":40,"tag":49,"props":1929,"children":1930},{},[1931,1936],{"type":40,"tag":198,"props":1932,"children":1933},{},[1934],{"type":46,"value":1935},"Pinning managed objects",{"type":46,"value":1937}," — when native code stores the pointer or runs asynchronously:",{"type":40,"tag":1032,"props":1939,"children":1941},{"className":1061,"code":1940,"language":1063,"meta":1036,"style":1036},"\u002F\u002F Synchronous: use fixed\npublic static unsafe void ProcessSync(byte[] data)\n{\n    fixed (byte* ptr = data) { ProcessData(ptr, (nuint)data.Length); }\n}\n\n\u002F\u002F Asynchronous: use GCHandle\nvar gcHandle = GCHandle.Alloc(data, GCHandleType.Pinned);\n\u002F\u002F Must keep pinned until native processing completes, then call gcHandle.Free()\n",[1942],{"type":40,"tag":60,"props":1943,"children":1944},{"__ignoreMap":1036},[1945,1953,1961,1968,1976,1983,1990,1998,2006],{"type":40,"tag":1042,"props":1946,"children":1947},{"class":1044,"line":1045},[1948],{"type":40,"tag":1042,"props":1949,"children":1950},{},[1951],{"type":46,"value":1952},"\u002F\u002F Synchronous: use fixed\n",{"type":40,"tag":1042,"props":1954,"children":1955},{"class":1044,"line":1078},[1956],{"type":40,"tag":1042,"props":1957,"children":1958},{},[1959],{"type":46,"value":1960},"public static unsafe void ProcessSync(byte[] data)\n",{"type":40,"tag":1042,"props":1962,"children":1963},{"class":1044,"line":1087},[1964],{"type":40,"tag":1042,"props":1965,"children":1966},{},[1967],{"type":46,"value":1742},{"type":40,"tag":1042,"props":1969,"children":1970},{"class":1044,"line":1213},[1971],{"type":40,"tag":1042,"props":1972,"children":1973},{},[1974],{"type":46,"value":1975},"    fixed (byte* ptr = data) { ProcessData(ptr, (nuint)data.Length); }\n",{"type":40,"tag":1042,"props":1977,"children":1978},{"class":1044,"line":1222},[1979],{"type":40,"tag":1042,"props":1980,"children":1981},{},[1982],{"type":46,"value":1782},{"type":40,"tag":1042,"props":1984,"children":1985},{"class":1044,"line":1230},[1986],{"type":40,"tag":1042,"props":1987,"children":1988},{"emptyLinePlaceholder":1207},[1989],{"type":46,"value":1210},{"type":40,"tag":1042,"props":1991,"children":1992},{"class":1044,"line":1312},[1993],{"type":40,"tag":1042,"props":1994,"children":1995},{},[1996],{"type":46,"value":1997},"\u002F\u002F Asynchronous: use GCHandle\n",{"type":40,"tag":1042,"props":1999,"children":2000},{"class":1044,"line":1321},[2001],{"type":40,"tag":1042,"props":2002,"children":2003},{},[2004],{"type":46,"value":2005},"var gcHandle = GCHandle.Alloc(data, GCHandleType.Pinned);\n",{"type":40,"tag":1042,"props":2007,"children":2008},{"class":1044,"line":1329},[2009],{"type":40,"tag":1042,"props":2010,"children":2011},{},[2012],{"type":46,"value":2013},"\u002F\u002F Must keep pinned until native processing completes, then call gcHandle.Free()\n",{"type":40,"tag":415,"props":2015,"children":2017},{"id":2016},"step-6-use-safehandle-for-native-handles",[2018],{"type":46,"value":2019},"Step 6: Use SafeHandle for Native Handles",{"type":40,"tag":49,"props":2021,"children":2022},{},[2023,2025,2030,2032,2037],{"type":46,"value":2024},"Raw ",{"type":40,"tag":60,"props":2026,"children":2028},{"className":2027},[],[2029],{"type":46,"value":819},{"type":46,"value":2031}," leaks on exceptions and has no double-free protection. ",{"type":40,"tag":60,"props":2033,"children":2035},{"className":2034},[],[2036],{"type":46,"value":808},{"type":46,"value":2038}," is non-negotiable.",{"type":40,"tag":1032,"props":2040,"children":2042},{"className":1061,"code":2041,"language":1063,"meta":1036,"style":1036},"internal sealed class MyLibHandle : SafeHandleZeroOrMinusOneIsInvalid\n{\n    \u002F\u002F Required by the marshalling infrastructure to instantiate the handle.\n    \u002F\u002F Do not remove — there are no direct callers.\n    private MyLibHandle() : base(ownsHandle: true) { }\n\n    [LibraryImport(\"mylib\", StringMarshalling = StringMarshalling.Utf8)]\n    private static partial MyLibHandle CreateHandle(string config);\n\n    [LibraryImport(\"mylib\")]\n    private static partial int UseHandle(MyLibHandle h, ReadOnlySpan\u003Cbyte> data, nuint len);\n\n    [LibraryImport(\"mylib\")]\n    private static partial void DestroyHandle(IntPtr h);\n\n    protected override bool ReleaseHandle() { DestroyHandle(handle); return true; }\n\n    public static MyLibHandle Create(string config)\n    {\n        var h = CreateHandle(config);\n        if (h.IsInvalid) throw new InvalidOperationException(\"Failed to create handle\");\n        return h;\n    }\n\n    public int Use(ReadOnlySpan\u003Cbyte> data) => UseHandle(this, data, (nuint)data.Length);\n}\n\n\u002F\u002F Usage: SafeHandle is IDisposable\nusing var handle = MyLibHandle.Create(\"config=value\");\nint result = handle.Use(myData);\n",[2043],{"type":40,"tag":60,"props":2044,"children":2045},{"__ignoreMap":1036},[2046,2054,2061,2069,2077,2085,2092,2100,2108,2115,2123,2131,2138,2145,2153,2160,2168,2175,2183,2191,2200,2209,2218,2227,2235,2244,2252,2260,2269,2278],{"type":40,"tag":1042,"props":2047,"children":2048},{"class":1044,"line":1045},[2049],{"type":40,"tag":1042,"props":2050,"children":2051},{},[2052],{"type":46,"value":2053},"internal sealed class MyLibHandle : SafeHandleZeroOrMinusOneIsInvalid\n",{"type":40,"tag":1042,"props":2055,"children":2056},{"class":1044,"line":1078},[2057],{"type":40,"tag":1042,"props":2058,"children":2059},{},[2060],{"type":46,"value":1742},{"type":40,"tag":1042,"props":2062,"children":2063},{"class":1044,"line":1087},[2064],{"type":40,"tag":1042,"props":2065,"children":2066},{},[2067],{"type":46,"value":2068},"    \u002F\u002F Required by the marshalling infrastructure to instantiate the handle.\n",{"type":40,"tag":1042,"props":2070,"children":2071},{"class":1044,"line":1213},[2072],{"type":40,"tag":1042,"props":2073,"children":2074},{},[2075],{"type":46,"value":2076},"    \u002F\u002F Do not remove — there are no direct callers.\n",{"type":40,"tag":1042,"props":2078,"children":2079},{"class":1044,"line":1222},[2080],{"type":40,"tag":1042,"props":2081,"children":2082},{},[2083],{"type":46,"value":2084},"    private MyLibHandle() : base(ownsHandle: true) { }\n",{"type":40,"tag":1042,"props":2086,"children":2087},{"class":1044,"line":1230},[2088],{"type":40,"tag":1042,"props":2089,"children":2090},{"emptyLinePlaceholder":1207},[2091],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2093,"children":2094},{"class":1044,"line":1312},[2095],{"type":40,"tag":1042,"props":2096,"children":2097},{},[2098],{"type":46,"value":2099},"    [LibraryImport(\"mylib\", StringMarshalling = StringMarshalling.Utf8)]\n",{"type":40,"tag":1042,"props":2101,"children":2102},{"class":1044,"line":1321},[2103],{"type":40,"tag":1042,"props":2104,"children":2105},{},[2106],{"type":46,"value":2107},"    private static partial MyLibHandle CreateHandle(string config);\n",{"type":40,"tag":1042,"props":2109,"children":2110},{"class":1044,"line":1329},[2111],{"type":40,"tag":1042,"props":2112,"children":2113},{"emptyLinePlaceholder":1207},[2114],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2116,"children":2117},{"class":1044,"line":1523},[2118],{"type":40,"tag":1042,"props":2119,"children":2120},{},[2121],{"type":46,"value":2122},"    [LibraryImport(\"mylib\")]\n",{"type":40,"tag":1042,"props":2124,"children":2125},{"class":1044,"line":1531},[2126],{"type":40,"tag":1042,"props":2127,"children":2128},{},[2129],{"type":46,"value":2130},"    private static partial int UseHandle(MyLibHandle h, ReadOnlySpan\u003Cbyte> data, nuint len);\n",{"type":40,"tag":1042,"props":2132,"children":2133},{"class":1044,"line":1540},[2134],{"type":40,"tag":1042,"props":2135,"children":2136},{"emptyLinePlaceholder":1207},[2137],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2139,"children":2140},{"class":1044,"line":1549},[2141],{"type":40,"tag":1042,"props":2142,"children":2143},{},[2144],{"type":46,"value":2122},{"type":40,"tag":1042,"props":2146,"children":2147},{"class":1044,"line":1558},[2148],{"type":40,"tag":1042,"props":2149,"children":2150},{},[2151],{"type":46,"value":2152},"    private static partial void DestroyHandle(IntPtr h);\n",{"type":40,"tag":1042,"props":2154,"children":2155},{"class":1044,"line":1567},[2156],{"type":40,"tag":1042,"props":2157,"children":2158},{"emptyLinePlaceholder":1207},[2159],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2161,"children":2162},{"class":1044,"line":1575},[2163],{"type":40,"tag":1042,"props":2164,"children":2165},{},[2166],{"type":46,"value":2167},"    protected override bool ReleaseHandle() { DestroyHandle(handle); return true; }\n",{"type":40,"tag":1042,"props":2169,"children":2170},{"class":1044,"line":1583},[2171],{"type":40,"tag":1042,"props":2172,"children":2173},{"emptyLinePlaceholder":1207},[2174],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2176,"children":2177},{"class":1044,"line":1592},[2178],{"type":40,"tag":1042,"props":2179,"children":2180},{},[2181],{"type":46,"value":2182},"    public static MyLibHandle Create(string config)\n",{"type":40,"tag":1042,"props":2184,"children":2185},{"class":1044,"line":1601},[2186],{"type":40,"tag":1042,"props":2187,"children":2188},{},[2189],{"type":46,"value":2190},"    {\n",{"type":40,"tag":1042,"props":2192,"children":2194},{"class":1044,"line":2193},20,[2195],{"type":40,"tag":1042,"props":2196,"children":2197},{},[2198],{"type":46,"value":2199},"        var h = CreateHandle(config);\n",{"type":40,"tag":1042,"props":2201,"children":2203},{"class":1044,"line":2202},21,[2204],{"type":40,"tag":1042,"props":2205,"children":2206},{},[2207],{"type":46,"value":2208},"        if (h.IsInvalid) throw new InvalidOperationException(\"Failed to create handle\");\n",{"type":40,"tag":1042,"props":2210,"children":2212},{"class":1044,"line":2211},22,[2213],{"type":40,"tag":1042,"props":2214,"children":2215},{},[2216],{"type":46,"value":2217},"        return h;\n",{"type":40,"tag":1042,"props":2219,"children":2221},{"class":1044,"line":2220},23,[2222],{"type":40,"tag":1042,"props":2223,"children":2224},{},[2225],{"type":46,"value":2226},"    }\n",{"type":40,"tag":1042,"props":2228,"children":2230},{"class":1044,"line":2229},24,[2231],{"type":40,"tag":1042,"props":2232,"children":2233},{"emptyLinePlaceholder":1207},[2234],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2236,"children":2238},{"class":1044,"line":2237},25,[2239],{"type":40,"tag":1042,"props":2240,"children":2241},{},[2242],{"type":46,"value":2243},"    public int Use(ReadOnlySpan\u003Cbyte> data) => UseHandle(this, data, (nuint)data.Length);\n",{"type":40,"tag":1042,"props":2245,"children":2247},{"class":1044,"line":2246},26,[2248],{"type":40,"tag":1042,"props":2249,"children":2250},{},[2251],{"type":46,"value":1782},{"type":40,"tag":1042,"props":2253,"children":2255},{"class":1044,"line":2254},27,[2256],{"type":40,"tag":1042,"props":2257,"children":2258},{"emptyLinePlaceholder":1207},[2259],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2261,"children":2263},{"class":1044,"line":2262},28,[2264],{"type":40,"tag":1042,"props":2265,"children":2266},{},[2267],{"type":46,"value":2268},"\u002F\u002F Usage: SafeHandle is IDisposable\n",{"type":40,"tag":1042,"props":2270,"children":2272},{"class":1044,"line":2271},29,[2273],{"type":40,"tag":1042,"props":2274,"children":2275},{},[2276],{"type":46,"value":2277},"using var handle = MyLibHandle.Create(\"config=value\");\n",{"type":40,"tag":1042,"props":2279,"children":2281},{"class":1044,"line":2280},30,[2282],{"type":40,"tag":1042,"props":2283,"children":2284},{},[2285],{"type":46,"value":2286},"int result = handle.Use(myData);\n",{"type":40,"tag":415,"props":2288,"children":2290},{"id":2289},"step-7-handle-errors",[2291],{"type":46,"value":2292},"Step 7: Handle Errors",{"type":40,"tag":1032,"props":2294,"children":2296},{"className":1061,"code":2295,"language":1063,"meta":1036,"style":1036},"\u002F\u002F Win32 APIs — check SetLastError\n[LibraryImport(\"kernel32\", SetLastPInvokeError = true)]\n[return: MarshalAs(UnmanagedType.Bool)]\ninternal static partial bool CloseHandle(IntPtr hObject);\n\nif (!CloseHandle(handle))\n    throw new Win32Exception(Marshal.GetLastPInvokeError());\n\n\u002F\u002F HRESULT APIs\nint hr = NativeDoWork(context);\nMarshal.ThrowExceptionForHR(hr);\n",[2297],{"type":40,"tag":60,"props":2298,"children":2299},{"__ignoreMap":1036},[2300,2308,2316,2324,2332,2339,2347,2355,2362,2370,2378],{"type":40,"tag":1042,"props":2301,"children":2302},{"class":1044,"line":1045},[2303],{"type":40,"tag":1042,"props":2304,"children":2305},{},[2306],{"type":46,"value":2307},"\u002F\u002F Win32 APIs — check SetLastError\n",{"type":40,"tag":1042,"props":2309,"children":2310},{"class":1044,"line":1078},[2311],{"type":40,"tag":1042,"props":2312,"children":2313},{},[2314],{"type":46,"value":2315},"[LibraryImport(\"kernel32\", SetLastPInvokeError = true)]\n",{"type":40,"tag":1042,"props":2317,"children":2318},{"class":1044,"line":1087},[2319],{"type":40,"tag":1042,"props":2320,"children":2321},{},[2322],{"type":46,"value":2323},"[return: MarshalAs(UnmanagedType.Bool)]\n",{"type":40,"tag":1042,"props":2325,"children":2326},{"class":1044,"line":1213},[2327],{"type":40,"tag":1042,"props":2328,"children":2329},{},[2330],{"type":46,"value":2331},"internal static partial bool CloseHandle(IntPtr hObject);\n",{"type":40,"tag":1042,"props":2333,"children":2334},{"class":1044,"line":1222},[2335],{"type":40,"tag":1042,"props":2336,"children":2337},{"emptyLinePlaceholder":1207},[2338],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2340,"children":2341},{"class":1044,"line":1230},[2342],{"type":40,"tag":1042,"props":2343,"children":2344},{},[2345],{"type":46,"value":2346},"if (!CloseHandle(handle))\n",{"type":40,"tag":1042,"props":2348,"children":2349},{"class":1044,"line":1312},[2350],{"type":40,"tag":1042,"props":2351,"children":2352},{},[2353],{"type":46,"value":2354},"    throw new Win32Exception(Marshal.GetLastPInvokeError());\n",{"type":40,"tag":1042,"props":2356,"children":2357},{"class":1044,"line":1321},[2358],{"type":40,"tag":1042,"props":2359,"children":2360},{"emptyLinePlaceholder":1207},[2361],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2363,"children":2364},{"class":1044,"line":1329},[2365],{"type":40,"tag":1042,"props":2366,"children":2367},{},[2368],{"type":46,"value":2369},"\u002F\u002F HRESULT APIs\n",{"type":40,"tag":1042,"props":2371,"children":2372},{"class":1044,"line":1523},[2373],{"type":40,"tag":1042,"props":2374,"children":2375},{},[2376],{"type":46,"value":2377},"int hr = NativeDoWork(context);\n",{"type":40,"tag":1042,"props":2379,"children":2380},{"class":1044,"line":1531},[2381],{"type":40,"tag":1042,"props":2382,"children":2383},{},[2384],{"type":46,"value":2385},"Marshal.ThrowExceptionForHR(hr);\n",{"type":40,"tag":415,"props":2387,"children":2389},{"id":2388},"step-8-handle-callbacks-if-needed",[2390],{"type":46,"value":2391},"Step 8: Handle Callbacks (if needed)",{"type":40,"tag":49,"props":2393,"children":2394},{},[2395,2406],{"type":40,"tag":198,"props":2396,"children":2397},{},[2398,2400],{"type":46,"value":2399},"Preferred (.NET 8+): ",{"type":40,"tag":60,"props":2401,"children":2403},{"className":2402},[],[2404],{"type":46,"value":2405},"UnmanagedCallersOnly",{"type":46,"value":2407}," — avoids delegates entirely, no GC lifetime risk:",{"type":40,"tag":1032,"props":2409,"children":2411},{"className":1061,"code":2410,"language":1063,"meta":1036,"style":1036},"[UnmanagedCallersOnly]\nprivate static void LogCallback(int level, IntPtr message)\n{\n    string msg = Marshal.PtrToStringUTF8(message) ?? string.Empty;\n    Console.WriteLine($\"[{level}] {msg}\");\n}\n\n[LibraryImport(\"mylib\")]\nprivate static unsafe partial void SetLogCallback(\n    delegate* unmanaged\u003Cint, IntPtr, void> cb);\n\nunsafe { SetLogCallback(&LogCallback); }\n",[2412],{"type":40,"tag":60,"props":2413,"children":2414},{"__ignoreMap":1036},[2415,2423,2431,2438,2446,2454,2461,2468,2475,2483,2491,2498],{"type":40,"tag":1042,"props":2416,"children":2417},{"class":1044,"line":1045},[2418],{"type":40,"tag":1042,"props":2419,"children":2420},{},[2421],{"type":46,"value":2422},"[UnmanagedCallersOnly]\n",{"type":40,"tag":1042,"props":2424,"children":2425},{"class":1044,"line":1078},[2426],{"type":40,"tag":1042,"props":2427,"children":2428},{},[2429],{"type":46,"value":2430},"private static void LogCallback(int level, IntPtr message)\n",{"type":40,"tag":1042,"props":2432,"children":2433},{"class":1044,"line":1087},[2434],{"type":40,"tag":1042,"props":2435,"children":2436},{},[2437],{"type":46,"value":1742},{"type":40,"tag":1042,"props":2439,"children":2440},{"class":1044,"line":1213},[2441],{"type":40,"tag":1042,"props":2442,"children":2443},{},[2444],{"type":46,"value":2445},"    string msg = Marshal.PtrToStringUTF8(message) ?? string.Empty;\n",{"type":40,"tag":1042,"props":2447,"children":2448},{"class":1044,"line":1222},[2449],{"type":40,"tag":1042,"props":2450,"children":2451},{},[2452],{"type":46,"value":2453},"    Console.WriteLine($\"[{level}] {msg}\");\n",{"type":40,"tag":1042,"props":2455,"children":2456},{"class":1044,"line":1230},[2457],{"type":40,"tag":1042,"props":2458,"children":2459},{},[2460],{"type":46,"value":1782},{"type":40,"tag":1042,"props":2462,"children":2463},{"class":1044,"line":1312},[2464],{"type":40,"tag":1042,"props":2465,"children":2466},{"emptyLinePlaceholder":1207},[2467],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2469,"children":2470},{"class":1044,"line":1321},[2471],{"type":40,"tag":1042,"props":2472,"children":2473},{},[2474],{"type":46,"value":1116},{"type":40,"tag":1042,"props":2476,"children":2477},{"class":1044,"line":1329},[2478],{"type":40,"tag":1042,"props":2479,"children":2480},{},[2481],{"type":46,"value":2482},"private static unsafe partial void SetLogCallback(\n",{"type":40,"tag":1042,"props":2484,"children":2485},{"class":1044,"line":1523},[2486],{"type":40,"tag":1042,"props":2487,"children":2488},{},[2489],{"type":46,"value":2490},"    delegate* unmanaged\u003Cint, IntPtr, void> cb);\n",{"type":40,"tag":1042,"props":2492,"children":2493},{"class":1044,"line":1531},[2494],{"type":40,"tag":1042,"props":2495,"children":2496},{"emptyLinePlaceholder":1207},[2497],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2499,"children":2500},{"class":1044,"line":1540},[2501],{"type":40,"tag":1042,"props":2502,"children":2503},{},[2504],{"type":46,"value":2505},"unsafe { SetLogCallback(&LogCallback); }\n",{"type":40,"tag":49,"props":2507,"children":2508},{},[2509,2511,2517],{"type":46,"value":2510},"The method must be ",{"type":40,"tag":60,"props":2512,"children":2514},{"className":2513},[],[2515],{"type":46,"value":2516},"static",{"type":46,"value":2518},", must not throw exceptions back to native code, and can only use blittable parameter types.",{"type":40,"tag":49,"props":2520,"children":2521},{},[2522],{"type":40,"tag":198,"props":2523,"children":2524},{},[2525],{"type":46,"value":2526},"Fallback (older TFMs or when instance state is needed): delegate with rooting",{"type":40,"tag":1032,"props":2528,"children":2530},{"className":1061,"code":2529,"language":1063,"meta":1036,"style":1036},"[UnmanagedFunctionPointer(CallingConvention.Cdecl)] \u002F\u002F Only needed on Windows x86\nprivate delegate void LogCallbackDelegate(int level, IntPtr message);\n\n\u002F\u002F CRITICAL: prevent delegate from being garbage collected\nprivate static LogCallbackDelegate? s_logCallback;\n\npublic static void EnableLogging(Action\u003Cint, string> handler)\n{\n    s_logCallback = (level, msgPtr) =>\n    {\n        string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;\n        handler(level, msg);\n    };\n    SetLogCallback(s_logCallback);\n}\n",[2531],{"type":40,"tag":60,"props":2532,"children":2533},{"__ignoreMap":1036},[2534,2542,2550,2557,2565,2573,2580,2588,2595,2603,2610,2618,2626,2634,2642],{"type":40,"tag":1042,"props":2535,"children":2536},{"class":1044,"line":1045},[2537],{"type":40,"tag":1042,"props":2538,"children":2539},{},[2540],{"type":46,"value":2541},"[UnmanagedFunctionPointer(CallingConvention.Cdecl)] \u002F\u002F Only needed on Windows x86\n",{"type":40,"tag":1042,"props":2543,"children":2544},{"class":1044,"line":1078},[2545],{"type":40,"tag":1042,"props":2546,"children":2547},{},[2548],{"type":46,"value":2549},"private delegate void LogCallbackDelegate(int level, IntPtr message);\n",{"type":40,"tag":1042,"props":2551,"children":2552},{"class":1044,"line":1087},[2553],{"type":40,"tag":1042,"props":2554,"children":2555},{"emptyLinePlaceholder":1207},[2556],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2558,"children":2559},{"class":1044,"line":1213},[2560],{"type":40,"tag":1042,"props":2561,"children":2562},{},[2563],{"type":46,"value":2564},"\u002F\u002F CRITICAL: prevent delegate from being garbage collected\n",{"type":40,"tag":1042,"props":2566,"children":2567},{"class":1044,"line":1222},[2568],{"type":40,"tag":1042,"props":2569,"children":2570},{},[2571],{"type":46,"value":2572},"private static LogCallbackDelegate? s_logCallback;\n",{"type":40,"tag":1042,"props":2574,"children":2575},{"class":1044,"line":1230},[2576],{"type":40,"tag":1042,"props":2577,"children":2578},{"emptyLinePlaceholder":1207},[2579],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2581,"children":2582},{"class":1044,"line":1312},[2583],{"type":40,"tag":1042,"props":2584,"children":2585},{},[2586],{"type":46,"value":2587},"public static void EnableLogging(Action\u003Cint, string> handler)\n",{"type":40,"tag":1042,"props":2589,"children":2590},{"class":1044,"line":1321},[2591],{"type":40,"tag":1042,"props":2592,"children":2593},{},[2594],{"type":46,"value":1742},{"type":40,"tag":1042,"props":2596,"children":2597},{"class":1044,"line":1329},[2598],{"type":40,"tag":1042,"props":2599,"children":2600},{},[2601],{"type":46,"value":2602},"    s_logCallback = (level, msgPtr) =>\n",{"type":40,"tag":1042,"props":2604,"children":2605},{"class":1044,"line":1523},[2606],{"type":40,"tag":1042,"props":2607,"children":2608},{},[2609],{"type":46,"value":2190},{"type":40,"tag":1042,"props":2611,"children":2612},{"class":1044,"line":1531},[2613],{"type":40,"tag":1042,"props":2614,"children":2615},{},[2616],{"type":46,"value":2617},"        string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;\n",{"type":40,"tag":1042,"props":2619,"children":2620},{"class":1044,"line":1540},[2621],{"type":40,"tag":1042,"props":2622,"children":2623},{},[2624],{"type":46,"value":2625},"        handler(level, msg);\n",{"type":40,"tag":1042,"props":2627,"children":2628},{"class":1044,"line":1549},[2629],{"type":40,"tag":1042,"props":2630,"children":2631},{},[2632],{"type":46,"value":2633},"    };\n",{"type":40,"tag":1042,"props":2635,"children":2636},{"class":1044,"line":1558},[2637],{"type":40,"tag":1042,"props":2638,"children":2639},{},[2640],{"type":46,"value":2641},"    SetLogCallback(s_logCallback);\n",{"type":40,"tag":1042,"props":2643,"children":2644},{"class":1044,"line":1567},[2645],{"type":40,"tag":1042,"props":2646,"children":2647},{},[2648],{"type":46,"value":1782},{"type":40,"tag":49,"props":2650,"children":2651},{},[2652,2654,2659],{"type":46,"value":2653},"If native code stores the function pointer, the delegate ",{"type":40,"tag":198,"props":2655,"children":2656},{},[2657],{"type":46,"value":2658},"must",{"type":46,"value":2660}," stay rooted for its entire lifetime. A collected delegate means a crash.",{"type":40,"tag":49,"props":2662,"children":2663},{},[2664,2675,2677,2683,2685,2690],{"type":40,"tag":198,"props":2665,"children":2666},{},[2667,2673],{"type":40,"tag":60,"props":2668,"children":2670},{"className":2669},[],[2671],{"type":46,"value":2672},"GC.KeepAlive",{"type":46,"value":2674}," for short-lived callbacks:",{"type":46,"value":2676}," When converting a delegate to a function pointer with ",{"type":40,"tag":60,"props":2678,"children":2680},{"className":2679},[],[2681],{"type":46,"value":2682},"Marshal.GetFunctionPointerForDelegate",{"type":46,"value":2684},", the GC does not track the relationship between the pointer and the delegate. Use ",{"type":40,"tag":60,"props":2686,"children":2688},{"className":2687},[],[2689],{"type":46,"value":2672},{"type":46,"value":2691}," to prevent collection before the native call completes:",{"type":40,"tag":1032,"props":2693,"children":2695},{"className":1061,"code":2694,"language":1063,"meta":1036,"style":1036},"var callback = new LogCallbackDelegate((level, msgPtr) =>\n{\n    string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;\n    Console.WriteLine($\"[{level}] {msg}\");\n});\n\nIntPtr fnPtr = Marshal.GetFunctionPointerForDelegate(callback);\nNativeUsesCallback(fnPtr);\nGC.KeepAlive(callback); \u002F\u002F prevent collection — fnPtr does not root the delegate\n",[2696],{"type":40,"tag":60,"props":2697,"children":2698},{"__ignoreMap":1036},[2699,2707,2714,2722,2729,2737,2744,2752,2760],{"type":40,"tag":1042,"props":2700,"children":2701},{"class":1044,"line":1045},[2702],{"type":40,"tag":1042,"props":2703,"children":2704},{},[2705],{"type":46,"value":2706},"var callback = new LogCallbackDelegate((level, msgPtr) =>\n",{"type":40,"tag":1042,"props":2708,"children":2709},{"class":1044,"line":1078},[2710],{"type":40,"tag":1042,"props":2711,"children":2712},{},[2713],{"type":46,"value":1742},{"type":40,"tag":1042,"props":2715,"children":2716},{"class":1044,"line":1087},[2717],{"type":40,"tag":1042,"props":2718,"children":2719},{},[2720],{"type":46,"value":2721},"    string msg = Marshal.PtrToStringUTF8(msgPtr) ?? string.Empty;\n",{"type":40,"tag":1042,"props":2723,"children":2724},{"class":1044,"line":1213},[2725],{"type":40,"tag":1042,"props":2726,"children":2727},{},[2728],{"type":46,"value":2453},{"type":40,"tag":1042,"props":2730,"children":2731},{"class":1044,"line":1222},[2732],{"type":40,"tag":1042,"props":2733,"children":2734},{},[2735],{"type":46,"value":2736},"});\n",{"type":40,"tag":1042,"props":2738,"children":2739},{"class":1044,"line":1230},[2740],{"type":40,"tag":1042,"props":2741,"children":2742},{"emptyLinePlaceholder":1207},[2743],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2745,"children":2746},{"class":1044,"line":1312},[2747],{"type":40,"tag":1042,"props":2748,"children":2749},{},[2750],{"type":46,"value":2751},"IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate(callback);\n",{"type":40,"tag":1042,"props":2753,"children":2754},{"class":1044,"line":1321},[2755],{"type":40,"tag":1042,"props":2756,"children":2757},{},[2758],{"type":46,"value":2759},"NativeUsesCallback(fnPtr);\n",{"type":40,"tag":1042,"props":2761,"children":2762},{"class":1044,"line":1329},[2763],{"type":40,"tag":1042,"props":2764,"children":2765},{},[2766],{"type":46,"value":2767},"GC.KeepAlive(callback); \u002F\u002F prevent collection — fnPtr does not root the delegate\n",{"type":40,"tag":405,"props":2769,"children":2770},{},[],{"type":40,"tag":98,"props":2772,"children":2774},{"id":2773},"cross-platform-library-loading",[2775],{"type":46,"value":2776},"Cross-Platform Library Loading",{"type":40,"tag":49,"props":2778,"children":2779},{},[2780,2782,2788,2790,2795,2796,2802,2803,2808,2809,2815,2817,2822,2823,2828,2830,2835,2837,2842],{"type":46,"value":2781},"Use ",{"type":40,"tag":60,"props":2783,"children":2785},{"className":2784},[],[2786],{"type":46,"value":2787},"NativeLibrary.SetDllImportResolver",{"type":46,"value":2789}," for complex scenarios, or conditional compilation for simple cases. Use ",{"type":40,"tag":60,"props":2791,"children":2793},{"className":2792},[],[2794],{"type":46,"value":639},{"type":46,"value":1625},{"type":40,"tag":60,"props":2797,"children":2799},{"className":2798},[],[2800],{"type":46,"value":2801},"CULong",{"type":46,"value":954},{"type":40,"tag":60,"props":2804,"children":2806},{"className":2805},[],[2807],{"type":46,"value":367},{"type":46,"value":1625},{"type":40,"tag":60,"props":2810,"children":2812},{"className":2811},[],[2813],{"type":46,"value":2814},"unsigned long",{"type":46,"value":2816},". Note: ",{"type":40,"tag":60,"props":2818,"children":2820},{"className":2819},[],[2821],{"type":46,"value":639},{"type":46,"value":1625},{"type":40,"tag":60,"props":2824,"children":2826},{"className":2825},[],[2827],{"type":46,"value":2801},{"type":46,"value":2829}," with ",{"type":40,"tag":60,"props":2831,"children":2833},{"className":2832},[],[2834],{"type":46,"value":73},{"type":46,"value":2836}," requires ",{"type":40,"tag":60,"props":2838,"children":2840},{"className":2839},[],[2841],{"type":46,"value":657},{"type":46,"value":925},{"type":40,"tag":1032,"props":2844,"children":2846},{"className":1061,"code":2845,"language":1063,"meta":1036,"style":1036},"\u002F\u002F Trivial: use platform naming convention\n\u002F\u002F The default naming convention adds corresponding prefix and extension when\n\u002F\u002F searching the nativelibrary. The resultant file name will be mylib.dll on\n\u002F\u002F Windows, libmylib.so on Linux and libmylib.dylib on macOS.\nprivate const string LibName = \"mylib\";\n\n\u002F\u002F Simple: conditional compilation\n\u002F\u002F WINDOWS, LINUX, MACOS are predefined only when targeting an OS-specific TFM\n\u002F\u002F (e.g., net8.0-windows). For portable TFMs (e.g., net8.0), these symbols are\n\u002F\u002F not defined — use the runtime resolver approach below instead.\n#if WINDOWS\n    private const string LibName = \"mylib.dll\";\n#elif LINUX\n    private const string LibName = \"libmylib.so\";\n#elif MACOS\n    private const string LibName = \"libmylib.dylib\";\n#endif\n\n\u002F\u002F Complex: runtime resolver\n\u002F\u002F When targeting netstandard2.0 or other frameworks when OperatingSystem.IsXXX\n\u002F\u002F is not available, use RuntimeInformation.IsOSPlatform(OSPlatform.XXX) api.\nNativeLibrary.SetDllImportResolver(typeof(MyLib).Assembly,\n    (name, assembly, searchPath) =>\n    {\n        if (name != \"mylib\") return IntPtr.Zero;\n        string libName = OperatingSystem.IsWindows()\n            ? \"mylib.dll\"\n            : OperatingSystem.IsMacOS()\n                ? \"libmylib.dylib\" : \"libmylib.so\";\n        NativeLibrary.TryLoad(libName, assembly, searchPath, out var handle);\n        return handle;\n    });\n",[2847],{"type":40,"tag":60,"props":2848,"children":2849},{"__ignoreMap":1036},[2850,2858,2866,2874,2882,2890,2897,2905,2913,2921,2929,2937,2945,2953,2961,2969,2977,2985,2992,3000,3008,3016,3024,3032,3039,3047,3055,3063,3071,3079,3087,3096],{"type":40,"tag":1042,"props":2851,"children":2852},{"class":1044,"line":1045},[2853],{"type":40,"tag":1042,"props":2854,"children":2855},{},[2856],{"type":46,"value":2857},"\u002F\u002F Trivial: use platform naming convention\n",{"type":40,"tag":1042,"props":2859,"children":2860},{"class":1044,"line":1078},[2861],{"type":40,"tag":1042,"props":2862,"children":2863},{},[2864],{"type":46,"value":2865},"\u002F\u002F The default naming convention adds corresponding prefix and extension when\n",{"type":40,"tag":1042,"props":2867,"children":2868},{"class":1044,"line":1087},[2869],{"type":40,"tag":1042,"props":2870,"children":2871},{},[2872],{"type":46,"value":2873},"\u002F\u002F searching the nativelibrary. The resultant file name will be mylib.dll on\n",{"type":40,"tag":1042,"props":2875,"children":2876},{"class":1044,"line":1213},[2877],{"type":40,"tag":1042,"props":2878,"children":2879},{},[2880],{"type":46,"value":2881},"\u002F\u002F Windows, libmylib.so on Linux and libmylib.dylib on macOS.\n",{"type":40,"tag":1042,"props":2883,"children":2884},{"class":1044,"line":1222},[2885],{"type":40,"tag":1042,"props":2886,"children":2887},{},[2888],{"type":46,"value":2889},"private const string LibName = \"mylib\";\n",{"type":40,"tag":1042,"props":2891,"children":2892},{"class":1044,"line":1230},[2893],{"type":40,"tag":1042,"props":2894,"children":2895},{"emptyLinePlaceholder":1207},[2896],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2898,"children":2899},{"class":1044,"line":1312},[2900],{"type":40,"tag":1042,"props":2901,"children":2902},{},[2903],{"type":46,"value":2904},"\u002F\u002F Simple: conditional compilation\n",{"type":40,"tag":1042,"props":2906,"children":2907},{"class":1044,"line":1321},[2908],{"type":40,"tag":1042,"props":2909,"children":2910},{},[2911],{"type":46,"value":2912},"\u002F\u002F WINDOWS, LINUX, MACOS are predefined only when targeting an OS-specific TFM\n",{"type":40,"tag":1042,"props":2914,"children":2915},{"class":1044,"line":1329},[2916],{"type":40,"tag":1042,"props":2917,"children":2918},{},[2919],{"type":46,"value":2920},"\u002F\u002F (e.g., net8.0-windows). For portable TFMs (e.g., net8.0), these symbols are\n",{"type":40,"tag":1042,"props":2922,"children":2923},{"class":1044,"line":1523},[2924],{"type":40,"tag":1042,"props":2925,"children":2926},{},[2927],{"type":46,"value":2928},"\u002F\u002F not defined — use the runtime resolver approach below instead.\n",{"type":40,"tag":1042,"props":2930,"children":2931},{"class":1044,"line":1531},[2932],{"type":40,"tag":1042,"props":2933,"children":2934},{},[2935],{"type":46,"value":2936},"#if WINDOWS\n",{"type":40,"tag":1042,"props":2938,"children":2939},{"class":1044,"line":1540},[2940],{"type":40,"tag":1042,"props":2941,"children":2942},{},[2943],{"type":46,"value":2944},"    private const string LibName = \"mylib.dll\";\n",{"type":40,"tag":1042,"props":2946,"children":2947},{"class":1044,"line":1549},[2948],{"type":40,"tag":1042,"props":2949,"children":2950},{},[2951],{"type":46,"value":2952},"#elif LINUX\n",{"type":40,"tag":1042,"props":2954,"children":2955},{"class":1044,"line":1558},[2956],{"type":40,"tag":1042,"props":2957,"children":2958},{},[2959],{"type":46,"value":2960},"    private const string LibName = \"libmylib.so\";\n",{"type":40,"tag":1042,"props":2962,"children":2963},{"class":1044,"line":1567},[2964],{"type":40,"tag":1042,"props":2965,"children":2966},{},[2967],{"type":46,"value":2968},"#elif MACOS\n",{"type":40,"tag":1042,"props":2970,"children":2971},{"class":1044,"line":1575},[2972],{"type":40,"tag":1042,"props":2973,"children":2974},{},[2975],{"type":46,"value":2976},"    private const string LibName = \"libmylib.dylib\";\n",{"type":40,"tag":1042,"props":2978,"children":2979},{"class":1044,"line":1583},[2980],{"type":40,"tag":1042,"props":2981,"children":2982},{},[2983],{"type":46,"value":2984},"#endif\n",{"type":40,"tag":1042,"props":2986,"children":2987},{"class":1044,"line":1592},[2988],{"type":40,"tag":1042,"props":2989,"children":2990},{"emptyLinePlaceholder":1207},[2991],{"type":46,"value":1210},{"type":40,"tag":1042,"props":2993,"children":2994},{"class":1044,"line":1601},[2995],{"type":40,"tag":1042,"props":2996,"children":2997},{},[2998],{"type":46,"value":2999},"\u002F\u002F Complex: runtime resolver\n",{"type":40,"tag":1042,"props":3001,"children":3002},{"class":1044,"line":2193},[3003],{"type":40,"tag":1042,"props":3004,"children":3005},{},[3006],{"type":46,"value":3007},"\u002F\u002F When targeting netstandard2.0 or other frameworks when OperatingSystem.IsXXX\n",{"type":40,"tag":1042,"props":3009,"children":3010},{"class":1044,"line":2202},[3011],{"type":40,"tag":1042,"props":3012,"children":3013},{},[3014],{"type":46,"value":3015},"\u002F\u002F is not available, use RuntimeInformation.IsOSPlatform(OSPlatform.XXX) api.\n",{"type":40,"tag":1042,"props":3017,"children":3018},{"class":1044,"line":2211},[3019],{"type":40,"tag":1042,"props":3020,"children":3021},{},[3022],{"type":46,"value":3023},"NativeLibrary.SetDllImportResolver(typeof(MyLib).Assembly,\n",{"type":40,"tag":1042,"props":3025,"children":3026},{"class":1044,"line":2220},[3027],{"type":40,"tag":1042,"props":3028,"children":3029},{},[3030],{"type":46,"value":3031},"    (name, assembly, searchPath) =>\n",{"type":40,"tag":1042,"props":3033,"children":3034},{"class":1044,"line":2229},[3035],{"type":40,"tag":1042,"props":3036,"children":3037},{},[3038],{"type":46,"value":2190},{"type":40,"tag":1042,"props":3040,"children":3041},{"class":1044,"line":2237},[3042],{"type":40,"tag":1042,"props":3043,"children":3044},{},[3045],{"type":46,"value":3046},"        if (name != \"mylib\") return IntPtr.Zero;\n",{"type":40,"tag":1042,"props":3048,"children":3049},{"class":1044,"line":2246},[3050],{"type":40,"tag":1042,"props":3051,"children":3052},{},[3053],{"type":46,"value":3054},"        string libName = OperatingSystem.IsWindows()\n",{"type":40,"tag":1042,"props":3056,"children":3057},{"class":1044,"line":2254},[3058],{"type":40,"tag":1042,"props":3059,"children":3060},{},[3061],{"type":46,"value":3062},"            ? \"mylib.dll\"\n",{"type":40,"tag":1042,"props":3064,"children":3065},{"class":1044,"line":2262},[3066],{"type":40,"tag":1042,"props":3067,"children":3068},{},[3069],{"type":46,"value":3070},"            : OperatingSystem.IsMacOS()\n",{"type":40,"tag":1042,"props":3072,"children":3073},{"class":1044,"line":2271},[3074],{"type":40,"tag":1042,"props":3075,"children":3076},{},[3077],{"type":46,"value":3078},"                ? \"libmylib.dylib\" : \"libmylib.so\";\n",{"type":40,"tag":1042,"props":3080,"children":3081},{"class":1044,"line":2280},[3082],{"type":40,"tag":1042,"props":3083,"children":3084},{},[3085],{"type":46,"value":3086},"        NativeLibrary.TryLoad(libName, assembly, searchPath, out var handle);\n",{"type":40,"tag":1042,"props":3088,"children":3090},{"class":1044,"line":3089},31,[3091],{"type":40,"tag":1042,"props":3092,"children":3093},{},[3094],{"type":46,"value":3095},"        return handle;\n",{"type":40,"tag":1042,"props":3097,"children":3099},{"class":1044,"line":3098},32,[3100],{"type":40,"tag":1042,"props":3101,"children":3102},{},[3103],{"type":46,"value":3104},"    });\n",{"type":40,"tag":405,"props":3106,"children":3107},{},[],{"type":40,"tag":98,"props":3109,"children":3111},{"id":3110},"migrating-dllimport-to-libraryimport",[3112],{"type":46,"value":3113},"Migrating DllImport to LibraryImport",{"type":40,"tag":49,"props":3115,"children":3116},{},[3117],{"type":46,"value":3118},"For codebases targeting .NET 7+, migrating provides AOT compatibility and trimming safety.",{"type":40,"tag":1342,"props":3120,"children":3121},{},[3122,3141,3157,3172,3189,3202],{"type":40,"tag":109,"props":3123,"children":3124},{},[3125,3127,3133,3135],{"type":46,"value":3126},"Add ",{"type":40,"tag":60,"props":3128,"children":3130},{"className":3129},[],[3131],{"type":46,"value":3132},"partial",{"type":46,"value":3134}," to the containing class and make the method ",{"type":40,"tag":60,"props":3136,"children":3138},{"className":3137},[],[3139],{"type":46,"value":3140},"static partial",{"type":40,"tag":109,"props":3142,"children":3143},{},[3144,3146,3151,3152],{"type":46,"value":3145},"Replace ",{"type":40,"tag":60,"props":3147,"children":3149},{"className":3148},[],[3150],{"type":46,"value":119},{"type":46,"value":2829},{"type":40,"tag":60,"props":3153,"children":3155},{"className":3154},[],[3156],{"type":46,"value":127},{"type":40,"tag":109,"props":3158,"children":3159},{},[3160,3161,3166,3167],{"type":46,"value":3145},{"type":40,"tag":60,"props":3162,"children":3164},{"className":3163},[],[3165],{"type":46,"value":516},{"type":46,"value":2829},{"type":40,"tag":60,"props":3168,"children":3170},{"className":3169},[],[3171],{"type":46,"value":527},{"type":40,"tag":109,"props":3173,"children":3174},{},[3175,3176,3182,3183],{"type":46,"value":3145},{"type":40,"tag":60,"props":3177,"children":3179},{"className":3178},[],[3180],{"type":46,"value":3181},"SetLastError = true",{"type":46,"value":2829},{"type":40,"tag":60,"props":3184,"children":3186},{"className":3185},[],[3187],{"type":46,"value":3188},"SetLastPInvokeError = true",{"type":40,"tag":109,"props":3190,"children":3191},{},[3192,3194,3200],{"type":46,"value":3193},"Remove ",{"type":40,"tag":60,"props":3195,"children":3197},{"className":3196},[],[3198],{"type":46,"value":3199},"CallingConvention",{"type":46,"value":3201}," unless targeting Windows x86",{"type":40,"tag":109,"props":3203,"children":3204},{},[3205,3207,3213,3215,3221],{"type":46,"value":3206},"Build and fix ",{"type":40,"tag":60,"props":3208,"children":3210},{"className":3209},[],[3211],{"type":46,"value":3212},"SYSLIB1054",{"type":46,"value":3214},"–",{"type":40,"tag":60,"props":3216,"children":3218},{"className":3217},[],[3219],{"type":46,"value":3220},"SYSLIB1057",{"type":46,"value":3222}," analyzer warnings",{"type":40,"tag":49,"props":3224,"children":3225},{},[3226],{"type":46,"value":3227},"Enable the interop analyzers:",{"type":40,"tag":1032,"props":3229,"children":3233},{"className":3230,"code":3231,"language":3232,"meta":1036,"style":1036},"language-xml shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u003CPropertyGroup>\n    \u003CEnableTrimAnalyzer>true\u003C\u002FEnableTrimAnalyzer>\n    \u003CEnableAotAnalyzer>true\u003C\u002FEnableAotAnalyzer>\n\u003C\u002FPropertyGroup>\n","xml",[3234],{"type":40,"tag":60,"props":3235,"children":3236},{"__ignoreMap":1036},[3237,3245,3253,3261],{"type":40,"tag":1042,"props":3238,"children":3239},{"class":1044,"line":1045},[3240],{"type":40,"tag":1042,"props":3241,"children":3242},{},[3243],{"type":46,"value":3244},"\u003CPropertyGroup>\n",{"type":40,"tag":1042,"props":3246,"children":3247},{"class":1044,"line":1078},[3248],{"type":40,"tag":1042,"props":3249,"children":3250},{},[3251],{"type":46,"value":3252},"    \u003CEnableTrimAnalyzer>true\u003C\u002FEnableTrimAnalyzer>\n",{"type":40,"tag":1042,"props":3254,"children":3255},{"class":1044,"line":1087},[3256],{"type":40,"tag":1042,"props":3257,"children":3258},{},[3259],{"type":46,"value":3260},"    \u003CEnableAotAnalyzer>true\u003C\u002FEnableAotAnalyzer>\n",{"type":40,"tag":1042,"props":3262,"children":3263},{"class":1044,"line":1213},[3264],{"type":40,"tag":1042,"props":3265,"children":3266},{},[3267],{"type":46,"value":3268},"\u003C\u002FPropertyGroup>\n",{"type":40,"tag":405,"props":3270,"children":3271},{},[],{"type":40,"tag":98,"props":3273,"children":3275},{"id":3274},"tooling",[3276],{"type":46,"value":3277},"Tooling",{"type":40,"tag":415,"props":3279,"children":3281},{"id":3280},"cswin32-win32-apis",[3282],{"type":46,"value":3283},"CsWin32 (Win32 APIs)",{"type":40,"tag":49,"props":3285,"children":3286},{},[3287,3289,3297,3299,3305],{"type":46,"value":3288},"For Win32 P\u002FInvoke, prefer ",{"type":40,"tag":919,"props":3290,"children":3294},{"href":3291,"rel":3292},"https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FCsWin32",[3293],"nofollow",[3295],{"type":46,"value":3296},"Microsoft.Windows.CsWin32",{"type":46,"value":3298}," over hand-written signatures. It source-generates correct declarations from metadata. Add a ",{"type":40,"tag":60,"props":3300,"children":3302},{"className":3301},[],[3303],{"type":46,"value":3304},"NativeMethods.txt",{"type":46,"value":3306}," listing the APIs you need:",{"type":40,"tag":1032,"props":3308,"children":3312},{"className":3309,"code":3310,"language":3311,"meta":1036,"style":1036},"language-bash shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","dotnet add package Microsoft.Windows.CsWin32\n","bash",[3313],{"type":40,"tag":60,"props":3314,"children":3315},{"__ignoreMap":1036},[3316],{"type":40,"tag":1042,"props":3317,"children":3318},{"class":1044,"line":1045},[3319,3324,3330,3335],{"type":40,"tag":1042,"props":3320,"children":3322},{"style":3321},"--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B",[3323],{"type":46,"value":8},{"type":40,"tag":1042,"props":3325,"children":3327},{"style":3326},"--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D",[3328],{"type":46,"value":3329}," add",{"type":40,"tag":1042,"props":3331,"children":3332},{"style":3326},[3333],{"type":46,"value":3334}," package",{"type":40,"tag":1042,"props":3336,"children":3337},{"style":3326},[3338],{"type":46,"value":3339}," Microsoft.Windows.CsWin32\n",{"type":40,"tag":415,"props":3341,"children":3343},{"id":3342},"cswinrt-winrt-apis",[3344],{"type":46,"value":3345},"CsWinRT (WinRT APIs)",{"type":40,"tag":49,"props":3347,"children":3348},{},[3349,3351,3358,3360,3366],{"type":46,"value":3350},"For WinRT interop, use ",{"type":40,"tag":919,"props":3352,"children":3355},{"href":3353,"rel":3354},"https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FCsWinRT",[3293],[3356],{"type":46,"value":3357},"Microsoft.Windows.CsWinRT",{"type":46,"value":3359}," to generate .NET projections from ",{"type":40,"tag":60,"props":3361,"children":3363},{"className":3362},[],[3364],{"type":46,"value":3365},".winmd",{"type":46,"value":3367}," files.",{"type":40,"tag":415,"props":3369,"children":3371},{"id":3370},"objective-sharpie-objective-c-apis",[3372],{"type":46,"value":3373},"Objective Sharpie (Objective-C APIs)",{"type":40,"tag":49,"props":3375,"children":3376},{},[3377,3379,3386],{"type":46,"value":3378},"For binding Objective-C libraries (macOS\u002FiOS), use ",{"type":40,"tag":919,"props":3380,"children":3383},{"href":3381,"rel":3382},"https:\u002F\u002Flearn.microsoft.com\u002Fprevious-versions\u002Fxamarin\u002Fcross-platform\u002Fmacios\u002Fbinding\u002Fobjective-sharpie",[3293],[3384],{"type":46,"value":3385},"Objective Sharpie",{"type":46,"value":3387}," to generate initial P\u002FInvoke and binding definitions from Objective-C headers.",{"type":40,"tag":405,"props":3389,"children":3390},{},[],{"type":40,"tag":98,"props":3392,"children":3394},{"id":3393},"validation",[3395],{"type":46,"value":3396},"Validation",{"type":40,"tag":415,"props":3398,"children":3400},{"id":3399},"review-checklist",[3401],{"type":46,"value":3402},"Review checklist",{"type":40,"tag":105,"props":3404,"children":3407},{"className":3405},[3406],"contains-task-list",[3408,3420,3429,3443,3452,3475,3484,3505,3514,3548,3582],{"type":40,"tag":109,"props":3409,"children":3412},{"className":3410},[3411],"task-list-item",[3413,3418],{"type":40,"tag":3414,"props":3415,"children":3417},"input",{"disabled":1207,"type":3416},"checkbox",[],{"type":46,"value":3419}," Every signature matches the native header exactly (types, sizes)",{"type":40,"tag":109,"props":3421,"children":3423},{"className":3422},[3411],[3424,3427],{"type":40,"tag":3414,"props":3425,"children":3426},{"disabled":1207,"type":3416},[],{"type":46,"value":3428}," Calling convention specified if targeting Windows x86; omitted otherwise",{"type":40,"tag":109,"props":3430,"children":3432},{"className":3431},[3411],[3433,3436,3438],{"type":40,"tag":3414,"props":3434,"children":3435},{"disabled":1207,"type":3416},[],{"type":46,"value":3437}," String encoding is explicit — no reliance on defaults or ",{"type":40,"tag":60,"props":3439,"children":3441},{"className":3440},[],[3442],{"type":46,"value":1406},{"type":40,"tag":109,"props":3444,"children":3446},{"className":3445},[3411],[3447,3450],{"type":40,"tag":3414,"props":3448,"children":3449},{"disabled":1207,"type":3416},[],{"type":46,"value":3451}," Memory ownership is documented and matched (who allocates, who frees, with what)",{"type":40,"tag":109,"props":3453,"children":3455},{"className":3454},[3411],[3456,3459,3461,3466,3468,3473],{"type":40,"tag":3414,"props":3457,"children":3458},{"disabled":1207,"type":3416},[],{"type":46,"value":3460}," ",{"type":40,"tag":60,"props":3462,"children":3464},{"className":3463},[],[3465],{"type":46,"value":808},{"type":46,"value":3467}," used for all native handles (no raw ",{"type":40,"tag":60,"props":3469,"children":3471},{"className":3470},[],[3472],{"type":46,"value":819},{"type":46,"value":3474}," escaping the interop layer)",{"type":40,"tag":109,"props":3476,"children":3478},{"className":3477},[3411],[3479,3482],{"type":40,"tag":3414,"props":3480,"children":3481},{"disabled":1207,"type":3416},[],{"type":46,"value":3483}," Delegates passed as callbacks are rooted to prevent GC collection",{"type":40,"tag":109,"props":3485,"children":3487},{"className":3486},[3411],[3488,3491,3492,3497,3498,3503],{"type":40,"tag":3414,"props":3489,"children":3490},{"disabled":1207,"type":3416},[],{"type":46,"value":3460},{"type":40,"tag":60,"props":3493,"children":3495},{"className":3494},[],[3496],{"type":46,"value":548},{"type":46,"value":1625},{"type":40,"tag":60,"props":3499,"children":3501},{"className":3500},[],[3502],{"type":46,"value":557},{"type":46,"value":3504}," set for APIs that use OS error codes",{"type":40,"tag":109,"props":3506,"children":3508},{"className":3507},[3411],[3509,3512],{"type":40,"tag":3414,"props":3510,"children":3511},{"disabled":1207,"type":3416},[],{"type":46,"value":3513}," Struct layout matches native (packing, alignment, field order)",{"type":40,"tag":109,"props":3515,"children":3517},{"className":3516},[3411],[3518,3521,3522,3527,3528,3533,3535,3540,3541,3546],{"type":40,"tag":3414,"props":3519,"children":3520},{"disabled":1207,"type":3416},[],{"type":46,"value":3460},{"type":40,"tag":60,"props":3523,"children":3525},{"className":3524},[],[3526],{"type":46,"value":639},{"type":46,"value":1625},{"type":40,"tag":60,"props":3529,"children":3531},{"className":3530},[],[3532],{"type":46,"value":2801},{"type":46,"value":3534}," used for C ",{"type":40,"tag":60,"props":3536,"children":3538},{"className":3537},[],[3539],{"type":46,"value":367},{"type":46,"value":1625},{"type":40,"tag":60,"props":3542,"children":3544},{"className":3543},[],[3545],{"type":46,"value":2814},{"type":46,"value":3547}," in cross-platform code",{"type":40,"tag":109,"props":3549,"children":3551},{"className":3550},[3411],[3552,3555,3557,3562,3563,3568,3569,3574,3575,3580],{"type":40,"tag":3414,"props":3553,"children":3554},{"disabled":1207,"type":3416},[],{"type":46,"value":3556}," If using ",{"type":40,"tag":60,"props":3558,"children":3560},{"className":3559},[],[3561],{"type":46,"value":639},{"type":46,"value":1625},{"type":40,"tag":60,"props":3564,"children":3566},{"className":3565},[],[3567],{"type":46,"value":2801},{"type":46,"value":2829},{"type":40,"tag":60,"props":3570,"children":3572},{"className":3571},[],[3573],{"type":46,"value":73},{"type":46,"value":152},{"type":40,"tag":60,"props":3576,"children":3578},{"className":3577},[],[3579],{"type":46,"value":657},{"type":46,"value":3581}," is applied",{"type":40,"tag":109,"props":3583,"children":3585},{"className":3584},[3411],[3586,3589,3591,3596,3598,3603,3605,3611,3613,3619],{"type":40,"tag":3414,"props":3587,"children":3588},{"disabled":1207,"type":3416},[],{"type":46,"value":3590}," No ",{"type":40,"tag":60,"props":3592,"children":3594},{"className":3593},[],[3595],{"type":46,"value":744},{"type":46,"value":3597}," without explicit ",{"type":40,"tag":60,"props":3599,"children":3601},{"className":3600},[],[3602],{"type":46,"value":1017},{"type":46,"value":3604}," — always specify ",{"type":40,"tag":60,"props":3606,"children":3608},{"className":3607},[],[3609],{"type":46,"value":3610},"UnmanagedType.Bool",{"type":46,"value":3612}," (4-byte) or ",{"type":40,"tag":60,"props":3614,"children":3616},{"className":3615},[],[3617],{"type":46,"value":3618},"UnmanagedType.U1",{"type":46,"value":3620}," (1-byte) to ensure normalization across the language boundary.",{"type":40,"tag":415,"props":3622,"children":3624},{"id":3623},"runnable-validation-steps",[3625],{"type":46,"value":3626},"Runnable validation steps",{"type":40,"tag":1342,"props":3628,"children":3629},{},[3630,3676,3700,3710],{"type":40,"tag":109,"props":3631,"children":3632},{},[3633,3638,3640,3645,3646,3651,3653],{"type":40,"tag":198,"props":3634,"children":3635},{},[3636],{"type":46,"value":3637},"Build with interop analyzers enabled",{"type":46,"value":3639}," — confirm zero ",{"type":40,"tag":60,"props":3641,"children":3643},{"className":3642},[],[3644],{"type":46,"value":3212},{"type":46,"value":3214},{"type":40,"tag":60,"props":3647,"children":3649},{"className":3648},[],[3650],{"type":46,"value":3220},{"type":46,"value":3652}," warnings:\n",{"type":40,"tag":1032,"props":3654,"children":3656},{"className":3230,"code":3655,"language":3232,"meta":1036,"style":1036},"\u003CEnableTrimAnalyzer>true\u003C\u002FEnableTrimAnalyzer>\n\u003CEnableAotAnalyzer>true\u003C\u002FEnableAotAnalyzer>\n",[3657],{"type":40,"tag":60,"props":3658,"children":3659},{"__ignoreMap":1036},[3660,3668],{"type":40,"tag":1042,"props":3661,"children":3662},{"class":1044,"line":1045},[3663],{"type":40,"tag":1042,"props":3664,"children":3665},{},[3666],{"type":46,"value":3667},"\u003CEnableTrimAnalyzer>true\u003C\u002FEnableTrimAnalyzer>\n",{"type":40,"tag":1042,"props":3669,"children":3670},{"class":1044,"line":1078},[3671],{"type":40,"tag":1042,"props":3672,"children":3673},{},[3674],{"type":46,"value":3675},"\u003CEnableAotAnalyzer>true\u003C\u002FEnableAotAnalyzer>\n",{"type":40,"tag":109,"props":3677,"children":3678},{},[3679,3684,3686,3692,3694],{"type":40,"tag":198,"props":3680,"children":3681},{},[3682],{"type":46,"value":3683},"Verify struct sizes match",{"type":46,"value":3685}," — for every struct crossing the boundary, assert ",{"type":40,"tag":60,"props":3687,"children":3689},{"className":3688},[],[3690],{"type":46,"value":3691},"Marshal.SizeOf\u003CT>()",{"type":46,"value":3693}," equals the native ",{"type":40,"tag":60,"props":3695,"children":3697},{"className":3696},[],[3698],{"type":46,"value":3699},"sizeof",{"type":40,"tag":109,"props":3701,"children":3702},{},[3703,3708],{"type":40,"tag":198,"props":3704,"children":3705},{},[3706],{"type":46,"value":3707},"Round-trip test",{"type":46,"value":3709}," — call the native function with known inputs and verify expected outputs",{"type":40,"tag":109,"props":3711,"children":3712},{},[3713,3718],{"type":40,"tag":198,"props":3714,"children":3715},{},[3716],{"type":46,"value":3717},"Test with non-ASCII strings",{"type":46,"value":3719}," — pass strings containing characters outside the ASCII range to confirm encoding is correct",{"type":40,"tag":98,"props":3721,"children":3723},{"id":3722},"reference-files",[3724],{"type":46,"value":3725},"Reference Files",{"type":40,"tag":105,"props":3727,"children":3728},{},[3729,3748],{"type":40,"tag":109,"props":3730,"children":3731},{},[3732,3739,3741,3746],{"type":40,"tag":198,"props":3733,"children":3734},{},[3735],{"type":40,"tag":919,"props":3736,"children":3737},{"href":921},[3738],{"type":46,"value":921},{"type":46,"value":3740}," — Complete native-to-.NET type mapping table, struct layout patterns, blittable type rules. ",{"type":40,"tag":198,"props":3742,"children":3743},{},[3744],{"type":46,"value":3745},"Load when",{"type":46,"value":3747}," encountering types not covered in Step 2 above, or when working with struct layout or blittable type questions.",{"type":40,"tag":109,"props":3749,"children":3750},{},[3751,3759,3761,3765],{"type":40,"tag":198,"props":3752,"children":3753},{},[3754],{"type":40,"tag":919,"props":3755,"children":3757},{"href":3756},"references\u002Fdiagnostics.md",[3758],{"type":46,"value":3756},{"type":46,"value":3760}," — Common pitfalls, failure modes and recovery, debugging approach, external resources. ",{"type":40,"tag":198,"props":3762,"children":3763},{},[3764],{"type":46,"value":3745},{"type":46,"value":3766}," debugging an existing P\u002FInvoke failure or reviewing interop code for correctness issues.",{"type":40,"tag":3768,"props":3769,"children":3770},"style",{},[3771],{"type":46,"value":3772},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"items":3774,"total":3881},[3775,3792,3807,3825,3839,3857,3867],{"slug":3776,"name":3776,"fn":3777,"description":3778,"org":3779,"tags":3780,"stars":22,"repoUrl":23,"updatedAt":3791},"analyzing-dotnet-performance","analyze .NET code for performance anti-patterns","Scans .NET code for ~50 performance anti-patterns across async, memory, strings, collections, LINQ, regex, serialization, and I\u002FO with tiered severity classification. Use when analyzing .NET code for optimization opportunities, reviewing hot paths, or auditing allocation-heavy patterns.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3781,3782,3785,3788],{"name":13,"slug":14,"type":15},{"name":3783,"slug":3784,"type":15},"Code Analysis","code-analysis",{"name":3786,"slug":3787,"type":15},"Debugging","debugging",{"name":3789,"slug":3790,"type":15},"Performance","performance","2026-07-12T08:23:25.400375",{"slug":3793,"name":3793,"fn":3794,"description":3795,"org":3796,"tags":3797,"stars":22,"repoUrl":23,"updatedAt":3806},"android-tombstone-symbolication","symbolicate .NET runtime frames in Android tombstones","Symbolicate the .NET runtime frames in an Android tombstone file. Extracts BuildIds and PC offsets from the native backtrace, downloads debug symbols from the Microsoft symbol server, and runs llvm-symbolizer to produce function names with source file and line numbers. USE FOR triaging a .NET MAUI or Mono Android app crash from a tombstone, resolving native backtrace frames in libmonosgen-2.0.so or libcoreclr.so to .NET runtime source code, or investigating SIGABRT, SIGSEGV, or other native signals originating from the .NET runtime on Android. DO NOT USE FOR pure Java\u002FKotlin crashes, managed .NET exceptions that are already captured in logcat, or iOS crash logs. INVOKES Symbolicate-Tombstone.ps1 script, llvm-symbolizer, Microsoft symbol server.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3798,3799,3802,3803],{"name":13,"slug":14,"type":15},{"name":3800,"slug":3801,"type":15},"Android","android",{"name":3786,"slug":3787,"type":15},{"name":3804,"slug":3805,"type":15},"Microsoft","microsoft","2026-07-12T08:23:21.595572",{"slug":3808,"name":3808,"fn":3809,"description":3810,"org":3811,"tags":3812,"stars":22,"repoUrl":23,"updatedAt":3824},"apple-crash-symbolication","symbolicate .NET runtime frames in crash logs","Symbolicate .NET runtime frames in Apple platform .ips crash logs (iOS, tvOS, Mac Catalyst, macOS). Extracts UUIDs and addresses from the native backtrace, locates dSYM debug symbols, and runs atos to produce function names with source file and line numbers. Automatically downloads .dwarf symbols from the Microsoft symbol server using Mach-O UUIDs. USE FOR triaging a .NET MAUI or Mono app crash from an .ips file on any Apple platform, resolving native backtrace frames in libcoreclr or libmonosgen-2.0 to .NET runtime source code, retrieving .ips crash logs from a connected iOS device or iPhone, or investigating EXC_CRASH, EXC_BAD_ACCESS, SIGABRT, or SIGSEGV originating from the .NET runtime. DO NOT USE FOR pure Swift\u002FObjective-C crashes with no .NET components, or Android tombstone files. INVOKES Symbolicate-Crash.ps1 script, atos, dwarfdump, idevicecrashreport.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3813,3814,3815,3818,3821],{"name":13,"slug":14,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3816,"slug":3817,"type":15},"iOS","ios",{"name":3819,"slug":3820,"type":15},"macOS","macos",{"name":3822,"slug":3823,"type":15},"Observability","observability","2026-07-12T08:23:20.369986",{"slug":3826,"name":3826,"fn":3827,"description":3828,"org":3829,"tags":3830,"stars":22,"repoUrl":23,"updatedAt":3838},"assertion-quality","evaluate assertion quality in test suites","Analyzes the variety and depth of assertions across test suites in any language. Use when the user asks to evaluate assertion quality, find shallow tests, identify assertion-free tests (no assertions or only trivial ones like Assert.IsNotNull \u002F toBeTruthy()), flag self-referential or tautological assertions, measure assertion diversity, or audit whether tests verify different facets of behavior. Polyglot: .NET, Python, TS\u002FJS, Java, Go, Ruby, Rust, Swift, Kotlin, PowerShell, C++. DO NOT USE FOR: writing new tests (use code-testing-agent \u002F writing-mstest-tests), mutation reasoning about whether tests would catch a bug (use test-gap-analysis), or a general severity-ranked anti-pattern audit (use test-anti-patterns), fixing or rewriting assertions, or writing, fixing, or modernizing MSTest tests, assertions, or attributes (use writing-mstest-tests).",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3831,3832,3835],{"name":3783,"slug":3784,"type":15},{"name":3833,"slug":3834,"type":15},"QA","qa",{"name":3836,"slug":3837,"type":15},"Testing","testing","2026-07-12T08:23:51.277743",{"slug":3840,"name":3840,"fn":3841,"description":3842,"org":3843,"tags":3844,"stars":22,"repoUrl":23,"updatedAt":3856},"author-component","create and review Blazor components","Create or review Blazor components (.razor files) with correct architecture. USE FOR: writing new Blazor components that do NOT involve JavaScript interop, implementing parameters and EventCallback, RenderFragment slots, component lifecycle (OnInitializedAsync, OnParametersSet), async patterns, IAsyncDisposable, CancellationToken, CSS isolation, code-behind. DO NOT USE FOR: creating new projects (use create-blazor-project), JavaScript interop or calling browser APIs from Blazor (use use-js-interop), forms and validation (use collect-user-input), prerendering issues (use support-prerendering), HTTP data fetching patterns (use fetch-and-send-data), coordinating state between unrelated components (use coordinate-components).\n",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3845,3846,3849,3850,3853],{"name":13,"slug":14,"type":15},{"name":3847,"slug":3848,"type":15},"Blazor","blazor",{"name":17,"slug":1063,"type":15},{"name":3851,"slug":3852,"type":15},"UI Components","ui-components",{"name":3854,"slug":3855,"type":15},"Web Development","web-development","2026-07-15T06:03:29.216359",{"slug":3858,"name":3858,"fn":3859,"description":3860,"org":3861,"tags":3862,"stars":22,"repoUrl":23,"updatedAt":3866},"binlog-failure-analysis","analyze MSBuild binary logs","Analyze MSBuild binary logs to diagnose build failures. USE FOR: build errors that are unclear from console output, diagnosing cascading failures across multi-project builds, tracing MSBuild target execution order, and generally any MSBuild build issues. Requires an existing .binlog file. DO NOT USE FOR: generating binlogs (use binlog-generation), non-MSBuild build systems.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3863,3864,3865],{"name":3783,"slug":3784,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3804,"slug":3805,"type":15},"2026-07-12T08:21:34.637923",{"slug":3868,"name":3868,"fn":3869,"description":3870,"org":3871,"tags":3872,"stars":22,"repoUrl":23,"updatedAt":3880},"binlog-generation","generate MSBuild binary logs for diagnostics","Generate MSBuild binary logs (binlogs) for build diagnostics and analysis. USE FOR: adding \u002Fbl:{} to any dotnet build, test, pack, publish, or restore command to capture a full build execution trace, prerequisite for binlog-failure-analysis and build-perf-diagnostics skills, enabling post-build investigation of errors or performance. Requires MSBuild 17.8+ \u002F .NET 8 SDK+ for {} placeholder; PowerShell needs -bl:{{}}. DO NOT USE FOR: non-MSBuild build systems (npm, Maven, CMake), analyzing an existing binlog (use binlog-failure-analysis instead).",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3873,3876,3877],{"name":3874,"slug":3875,"type":15},"Build","build",{"name":3786,"slug":3787,"type":15},{"name":3878,"slug":3879,"type":15},"Engineering","engineering","2026-07-19T05:38:19.340791",96,{"items":3883,"total":3988},[3884,3896,3903,3910,3918,3924,3932,3938,3944,3954,3967,3978],{"slug":3885,"name":3885,"fn":3886,"description":3887,"org":3888,"tags":3889,"stars":3893,"repoUrl":3894,"updatedAt":3895},"multithreaded-task-migration","migrate MSBuild tasks to multithreaded mode","Guide for migrating MSBuild tasks to multithreaded mode support, including compatibility red-team review. Use this when converting tasks to thread-safe versions, implementing IMultiThreadableTask, adding TaskEnvironment support, or auditing migrations for behavioral compatibility.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3890,3891,3892],{"name":13,"slug":14,"type":15},{"name":3878,"slug":3879,"type":15},{"name":3789,"slug":3790,"type":15},5535,"https:\u002F\u002Fgithub.com\u002Fdotnet\u002Fmsbuild","2026-07-22T05:37:33.965588",{"slug":3776,"name":3776,"fn":3777,"description":3778,"org":3897,"tags":3898,"stars":22,"repoUrl":23,"updatedAt":3791},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3899,3900,3901,3902],{"name":13,"slug":14,"type":15},{"name":3783,"slug":3784,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3789,"slug":3790,"type":15},{"slug":3793,"name":3793,"fn":3794,"description":3795,"org":3904,"tags":3905,"stars":22,"repoUrl":23,"updatedAt":3806},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3906,3907,3908,3909],{"name":13,"slug":14,"type":15},{"name":3800,"slug":3801,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3804,"slug":3805,"type":15},{"slug":3808,"name":3808,"fn":3809,"description":3810,"org":3911,"tags":3912,"stars":22,"repoUrl":23,"updatedAt":3824},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3913,3914,3915,3916,3917],{"name":13,"slug":14,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3816,"slug":3817,"type":15},{"name":3819,"slug":3820,"type":15},{"name":3822,"slug":3823,"type":15},{"slug":3826,"name":3826,"fn":3827,"description":3828,"org":3919,"tags":3920,"stars":22,"repoUrl":23,"updatedAt":3838},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3921,3922,3923],{"name":3783,"slug":3784,"type":15},{"name":3833,"slug":3834,"type":15},{"name":3836,"slug":3837,"type":15},{"slug":3840,"name":3840,"fn":3841,"description":3842,"org":3925,"tags":3926,"stars":22,"repoUrl":23,"updatedAt":3856},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3927,3928,3929,3930,3931],{"name":13,"slug":14,"type":15},{"name":3847,"slug":3848,"type":15},{"name":17,"slug":1063,"type":15},{"name":3851,"slug":3852,"type":15},{"name":3854,"slug":3855,"type":15},{"slug":3858,"name":3858,"fn":3859,"description":3860,"org":3933,"tags":3934,"stars":22,"repoUrl":23,"updatedAt":3866},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3935,3936,3937],{"name":3783,"slug":3784,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3804,"slug":3805,"type":15},{"slug":3868,"name":3868,"fn":3869,"description":3870,"org":3939,"tags":3940,"stars":22,"repoUrl":23,"updatedAt":3880},{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3941,3942,3943],{"name":3874,"slug":3875,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3878,"slug":3879,"type":15},{"slug":3945,"name":3945,"fn":3946,"description":3947,"org":3948,"tags":3949,"stars":22,"repoUrl":23,"updatedAt":3953},"build-parallelism","optimize MSBuild build parallelism","Diagnose and fix under-parallelized MSBuild builds. USE WHEN a multi-project solution build is slower than expected, doesn't speed up when you add cores, pegs a single core while others idle, or you want to know why `-m` isn't helping. Note: `\u002Fmaxcpucount` default is 1 (sequential) — always pass `-m` for parallel builds. Covers finding the critical path (longest serial ProjectReference chain), graph build (`\u002Fgraph`), BuildInParallel, and solution filters (`.slnf`). DO NOT USE FOR: single-project builds, incremental issues (use incremental-build), compilation slowness inside one project (use build-perf-diagnostics), non-MSBuild build systems.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3950,3951,3952],{"name":13,"slug":14,"type":15},{"name":3878,"slug":3879,"type":15},{"name":3789,"slug":3790,"type":15},"2026-07-19T05:38:18.364937",{"slug":3955,"name":3955,"fn":3956,"description":3957,"org":3958,"tags":3959,"stars":22,"repoUrl":23,"updatedAt":3966},"build-perf-baseline","establish and optimize build performance baselines","Establish build performance baselines and apply systematic optimization techniques. USE FOR: diagnosing slow builds, establishing before\u002Fafter measurements (cold, warm, no-op scenarios), applying optimization strategies like MSBuild Server, static graph builds, artifacts output, and dependency graph trimming. Start here before diving into build-perf-diagnostics, incremental-build, or build-parallelism. DO NOT USE FOR: non-MSBuild build systems, detailed bottleneck analysis (use build-perf-diagnostics after baselining).",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3960,3961,3964,3965],{"name":3878,"slug":3879,"type":15},{"name":3962,"slug":3963,"type":15},"Monitoring","monitoring",{"name":3789,"slug":3790,"type":15},{"name":3836,"slug":3837,"type":15},"2026-07-12T08:21:35.865649",{"slug":3968,"name":3968,"fn":3969,"description":3970,"org":3971,"tags":3972,"stars":22,"repoUrl":23,"updatedAt":3977},"build-perf-diagnostics","diagnose MSBuild build performance bottlenecks","Diagnose MSBuild build performance bottlenecks using binary log analysis. USE FOR: identifying why builds are slow by analyzing binlog performance summaries, detecting ResolveAssemblyReference (RAR) taking >5s, Roslyn analyzers consuming >30% of Csc time, single targets dominating >50% of build time, node utilization below 80%, excessive Copy tasks, NuGet restore running every build. Covers timeline analysis, Target\u002FTask Performance Summary interpretation, and 7 common bottleneck categories. Use after build-perf-baseline has established measurements. DO NOT USE FOR: establishing initial baselines (use build-perf-baseline first), fixing incremental build issues (use incremental-build), parallelism tuning (use build-parallelism), non-MSBuild build systems.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3973,3974,3975,3976],{"name":13,"slug":14,"type":15},{"name":3786,"slug":3787,"type":15},{"name":3878,"slug":3879,"type":15},{"name":3789,"slug":3790,"type":15},"2026-07-12T08:21:40.961722",{"slug":3979,"name":3979,"fn":3980,"description":3981,"org":3982,"tags":3983,"stars":22,"repoUrl":23,"updatedAt":3987},"check-bin-obj-clash","detect MSBuild output path conflicts","Detects MSBuild projects with conflicting OutputPath or IntermediateOutputPath. USE FOR: builds failing with 'Cannot create a file when that file already exists', 'The process cannot access the file because it is being used by another process', intermittent build failures that succeed on retry, or missing\u002Foverwritten outputs in multi-project or multi-targeting builds where bin\u002Fobj (or project.assets.json) collide. Common causes: shared OutputPath, missing AppendTargetFrameworkToOutputPath, extra global properties (e.g. PublishReadyToRun), or SetTargetFramework on a ProjectReference to a single-targeting project. DO NOT USE FOR: file access errors unrelated to MSBuild (OS-level locking), single-project single-TFM builds, non-MSBuild build systems.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":8},[3984,3985,3986],{"name":3786,"slug":3787,"type":15},{"name":3878,"slug":3879,"type":15},{"name":3833,"slug":3834,"type":15},"2026-07-19T05:38:14.336279",144]