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
- Replace
[DllImport]withPInvoke.*. Delete old declarations and hand-written structs/enums/constants. - Gate with
#if FEATURE_WINDOWSINTEROP, add runtimeIsWindowscheck inside. Both required. - Use CsWin32 types directly (
HANDLE,HMODULE,HRESULT.S_OK,FILE_FLAGS_AND_ATTRIBUTES, etc.). - Call
PInvoke.*directly — no wrappers. Types flow viaInternalsVisibleTo. - Prefer CsWin32 for Windows APIs. Use
[LibraryImport]only for non-Windows native calls (e.g.libc), guarded with#if NET. - Preserve the old error-handling contract. Check the original
[DllImport]forPreserveSig/SetLastError/BOOL/HRESULTsemantics and reproduce them:PreserveSig=false→.ThrowOnFailure();SetLastError=true+ failedBOOL→throw new Win32Exception(). Silently returning where the old code threw is a behavior change. See cswin32-com's parity table for the COM-side equivalent.