windows-native-interop

Designs and reviews Win32, COM, and WinRT interop for C#/.NET Windows desktop applications.

3|Updated Aug 8, 2026
One-click install
npx skills add https://github.com/Jose-Polanco-Oxte/Echos-Live-Music-Visualizer --skill windows-native-interop-jose-polanco-oxte
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: windows-native-interop
Source: https://github.com/Jose-Polanco-Oxte/Echos-Live-Music-Visualizer/tree/main/.agents/skills/windows/sub-skills/windows-native-interop
Command: npx skills add https://github.com/Jose-Polanco-Oxte/Echos-Live-Music-Visualizer --skill windows-native-interop-jose-polanco-oxte

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing native Windows interop in C#/.NET is error-prone: incorrect P/Invoke signatures, wrong handle ownership, mismatched calling conventions, and architecture-specific bugs can compile cleanly yet cause memory corruption, leaks, and crashes. This Skill provides structured guidance to design, implement, review, and debug native interop correctly. ## Core Features & Use Cases - Declaration Strategy Selection: Guides choosing between CsWin32, LibraryImport, DllImport, GeneratedComInterface, and WinRT projections based on the API layer and project constraints. - ABI and Lifetime Correctness: Covers typed handles, SafeHandle ownership, structure layout, string encoding, HRESULT vs Win32 errors, COM apartments, and callback lifetimes. - Architecture and Deployment Validation: Addresses x64/ARM64/x86 differences, AnyCPU pitfalls, NativeAOT/trimming compatibility, packaging models, and secure DLL loading. - Use Case: When adding a Win32 call to a WinUI 3 app, use this Skill to generate the correct CsWin32 declaration, wrap it behind a SafeHandle-based service, and validate error handling and architecture behavior. ## Quick Start Ask the AI to review or implement a specific native interop call in your C# project, such as adding a CsWin32-based Win32 API call with correct handle ownership and error handling.

Frequently Asked Questions about windows-native-interop

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call Win32 APIs from C# in .NET?

Use CsWin32 (Microsoft.Windows.CsWin32) with a minimal NativeMethods.txt listing only the APIs you need. It generates type-safe declarations, structures, and SafeHandle-aware overloads from Windows SDK metadata, which is preferred over handwritten DllImport signatures.

Should I use LibraryImport or DllImport for P/Invoke?

Use LibraryImport for custom native C ABI functions on .NET 7 or later, since it provides source-generated marshalling. Use CsWin32 for Windows SDK Win32 APIs, and keep existing correct DllImport declarations unless migration provides a real benefit.

How do I get the HWND of a WinUI 3 window in C#?

Use WinRT.Interop.WindowNative.GetWindowHandle(window), the supported helper for retrieving a WinUI 3 window handle. Do not discover the HWND through window titles, enumeration, or process IDs, and never destroy a framework-owned HWND.

Does CsWin32 work with NativeAOT and trimming?

CsWin32's default mode uses runtime marshalling and is not automatically NativeAOT-compatible; Microsoft documents additional AOT configuration. Prefer source-generated mechanisms like LibraryImport and GeneratedComInterface where they fit the ABI.

Why does my P/Invoke code work on x64 but crash on x86?

Calling convention mismatches cause stack corruption on x86, which distinguishes stdcall from cdecl, while x64 and ARM64 use a single platform ABI. Also verify pointer-sized types use nint/nuint rather than int, and match structure layout per architecture.

When should I use SafeHandle instead of IntPtr for native handles?

Use SafeHandle for owned unmanaged handles whenever practical, as .NET guidance recommends it over finalizers. Each handle type needs its correct release function, and borrowed or pseudo-handles must never be wrapped in a releasing SafeHandle.