Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI
Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI
Direct Interop code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI, CodecAPI, GDI, Spatial Audio, DVD, Windows Media Player, UWP DXInterop, WinUI3, etc.
The easiest way to use this is reference the nuget package: https://www.nuget.org/packages/DirectN/ (.NET Framework) or https://www.nuget.org/packages/DirectNCore/ (.NET Core and .NET 5, 6, 7, etc.) or https://www.nuget.org/packages/DirectNStandard/ (.NET Standard).
Or you can pick the C# source files you want (among the 10000 .cs provided ...). It can be sometimes difficult because the dependencies between all these technologies can make extracting a small portion quite difficult (and they are one of the reasons why DirectN exists...). However this is still possible.
[!WARNING]
As of 2025, DirectN evolutions will now happen in the DirectNAot project which builds AOT-compatible DirectN code project (see below) with the main benefit of being able to ship single-file binaries. DirectN still exists, as is, mostly for .NET Framework support. We recommend you start migrating your .NET 9+ projects to DirectNAot as soon as possible. Don't hesitate to create issues in DirectNAot repository for migration issues.It's time to move forward!
Since DirectN is compatible with .NET Framework and older C# versions and constructs, marshalling is provided by the CLR (it's "built-in"), so it's not compatible with Native AOT Deployment.
However, a .NET 9+ (.NET 8 has a dreadful COM support bug so it's not supported) project here DirectNAot which builds AOT-compatible DirectN code. This code is not 100% source compatible with DirectN but it's very similar, and its code generator named Win32InteropBuilder is now provided as open source and is based on Microsoft-endorsed win32metadata project, which ensures better future enhancements.
Note the fact DirectNAOT is AOT-compatible doesn't prevent you from using it for non-AOT projects.
DirectN allows you to port C/C++ code to C#, or to write C# code from scratch, probably more easily than with other existing interop libraries in this domain because one of its main objective is to use exactly the same names and types than the native concepts (interfaces, enums, structures, constants, methods, arguments, guids, etc.) . So you can read the official documentation, use existing C/C++ samples, and start coding with .NET right away.
By design, everything is in the same namespace (and in the same assembly if you use the whole package) so you don't need to know where is defined this or that interface, constants, etc.
All native COM interfaces are generated as .NET (COM) interfaces, not classes or fancy/complex/unsafe structs. COM utility wrappers are also provided (ComObject, ComMemory, etc.), and many extension methods are also provided for some COM interfaces (the principle used can be extended to any COM method, but this part is not generated). They allow easier .NET programming, but they are not strictly needed. Most of this is possible because DirectN represent COM inheritance by .NET inheritance (so, DirectN.IWICImagingFactory2 derives from DirectN.IWICImagingFactory for example).
And last but not least, marking code using DirectN as unsafe is not required.
For example, the exact same C# version of the "Capture screen using DirectX" code here: https://stackoverflow.com/questions/30021274/capture-screen-using-directx is this:
…WicNet is a .NET standard project that is a full wrapper on WIC (Windows Imaging Component) components. It includes WIC connections with Direct2D (effects), and DirectWrite (text rendering) and is based on DirectN
Wice "Windows Interface Composition Engine" also uses DirectN and is a bloat-free .NET UI engine (no releation with Winforms, WPF, UWP nor WinUI3) for creating Windows (as in Win32/WinRT) desktop applications.
The DirectN.MinimalD3D11 sample here https://github.com/smourier/DirectN/tree/master/DirectN/DirectN.MinimalD3D11 has been ported to C# from here: https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052 which features "a minimal Direct3D11 "uncluttered direct3d 11 setup and rendering primer for newcomers to the api" sample.
It's dependent on DirectN, .NET 10, Windows Forms and ... that's it. Here is the output (believe me, it rotates):
Full credits go to d7Samurai: https://gist.github.com/d7samurai
Note: the code also works with .NET Core 3.1 to 5, it's just a project configuration matter.
The DirectN.WinUI3.MinimalD3D11 sample here https://github.com/smourier/DirectN/tree/master/DirectN/DirectN.WinUI3.MinimalD3D11 has been ported to C# from here: https://gist.github.com/d7samurai/abab8a580d0298cb2f34a44eec41d39d which features a minimal Direct3D11 "'API familiarizer' - an uncluttered Direct3D 11 setup & basic rendering reference implementation, in the form of a complete, runnable Windows application contained in a single function and laid out in a linear, step-by-step fashion" sample.
It's dependent on DirectN, .NET 10, WinUI3 (using SwapChainPanel https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.swapchainpanel), and ... that's it. Here is the output (believe me, it rotates):
Full credits go to d7Samurai: https://gist.github.com/d7samurai
No open issues yet, or sync has not completed.