cswin32-interop

Installation
SKILL.md

CsWin32 Interop Guide

CsWin32 replaces [DllImport] with source-generated PInvoke.* calls. FEATURE_WINDOWSINTEROP is the compile-time gate; source builds disable it.

Paired skill: cswin32-com covers struct-based COM interop on top of CsWin32 (ComScope<T>, AgileComPointer<T>, delegate* unmanaged vtables, IComIID, CoCreateInstance, manual COM structs not in Win32 metadata). This file covers only the general P/Invoke layer; the COM skill builds on its blittable-signature rules.

Rules

  1. Replace [DllImport] with PInvoke.*. Delete old declarations and hand-written structs/enums/constants.
  2. Gate with #if FEATURE_WINDOWSINTEROP, add runtime IsWindows check inside. Both required.
  3. Use CsWin32 types directly (HANDLE, HMODULE, HRESULT.S_OK, FILE_FLAGS_AND_ATTRIBUTES, etc.).
  4. Call PInvoke.* directly — no wrappers. Types flow via InternalsVisibleTo.
  5. Prefer CsWin32 for Windows APIs. Use [LibraryImport] only for non-Windows native calls (e.g. libc), guarded with #if NET.
  6. Preserve the old error-handling contract. Check the original [DllImport] for PreserveSig / SetLastError / BOOL / HRESULT semantics and reproduce them: PreserveSig=false.ThrowOnFailure(); SetLastError=true + failed BOOLthrow new Win32Exception(). Silently returning where the old code threw is a behavior change. See cswin32-com's parity table for the COM-side equivalent.

Blittable signatures

Installs
1
Repository
dotnet/msbuild
GitHub Stars
5.5K
First Seen
Jun 4, 2026
cswin32-interop — dotnet/msbuild