$ show kant2002/WinFormsComInterop
WinFormsComInterop is a .NET library that provides a custom ComWrappers implementation for Windows Forms (and WebView2) to enable NativeAOT compilation and trimming. It includes a source generator that automatically produces Runtime Callable Wrappers (RCW) and COM Callable Wrappers (CCW) for arbitrary COM interfaces, reducing manual interop code. The repository also contains facade assemblies that stub out dependencies on System.Windows.Forms, WindowsBase, PresentationCore, and Microsoft.Web.WebView2.Core, allowing AOT-friendly builds without full desktop framework references. Multiple sample applications demonstrate usage with WinForms, Blazor Hybrid, hosted WinForms in native DLLs, and trimming. The project is actively maintained and targets .NET 6 through .NET 10, but relies on pre-release SDK versions and has some experimental components (WPF, WinUI) that are not production-ready.
The LLM identified 3 files worth highlighting for reuse:
The main entry point for the source generator that produces COM wrapper classes. It processes custom attributes (RuntimeCallableWrapper, ComCallableWrapper) and generates RCW/CCW proxies with marshaling logic. This component is self-contained and can be reused in any .NET project that needs to generate COM interop wrappers for NativeAOT scenarios.
A custom marshaller for strings in COM interop, handling conversion between managed strings and native BSTR or LPWSTR. It demonstrates a clean, isolated pattern for implementing custom marshaling that can be extracted and applied to other COM interop projects.
The core ComWrappers implementation that manages COM object creation and vtable generation for WinForms interfaces (e.g., IDropTarget, IWebBrowser2, IAccessible). While tightly coupled to WinForms, the pattern of subclassing ComWrappers and providing custom vtable definitions is a reusable architectural pattern for any COM interop library targeting NativeAOT.