A simple, direct, ultra-thin CLR library for high-performance Win32 Native Interop
A simple, direct, ultra-thin CLR library for high-performance Win32 Native Interop
static int Main(string[] args) { using (var win = Window.Create(text: "Hello")) { win.Show(); return new EventLoop().Run(win); } }
Nuget:
Install-Package WinApi
Fully supports the CoreCLR. Uses C# 7 features like
ref returnsto achieve performance without losing semantic value.
Direct message loop performance: 20-35% faster. Heap allocation: 0MB vs. roughly, 0.75GB / 100k messages. Memory page faults (Soft): 0.005% - A mere 5k vs. roughly 1 million faults/100k messages)
WinApi- The core package that contains all the methods, helpers, and the tiny
WinApi.Windowsnamespace.
WinApi.Desktop- Desktop-only helpers.
WinApi.Utils- Provides utilities like
NativePixelBuffer,
DwmWindowetc.
WinApi.DxUtils- Provides DirectX utilities that ease the version management of SharpDX factories, and provides cohesive automatic device management to write DirectX application with just a few lines of code automatically managing device loss, device errors, etc.
WinApi.Windows.Controls[Incomplete] - A small library that implements the
EventedWindowCorefor standard classes like
Static,
Editand also provides
Window, which is just a helper to ease direct derivation of EventedWindowCore. This library is currently incomplete and just provides the implementations to serve as an example.
Note: - Starting from v4, all packages are of minimum
netstandard 1.4, and
Sourcenuget packages are no more.
Desktoppackage is
netstandard 2.0.
Zero GC allocationson during window messages, and event loop cycles.
Fundamental concepts similar to ATL/WTL, but in a C# idiomatic way.
can work with CoreCLR.
pay-only-for-what-you-use model.
RealtimeEventLoopfor games while the simple
EventLoopis ideal for normal applications).
hand-written from a combination of auto-generation from Windows SDK headers and MSDN, and tested for correctness.
windows.hheader file for the CLR. Other Windows SDK wrappers may, or may not be in fragmented into separate packages.
Examples of usage with Direct2D, 3D, Skia, OpenGL are all in the samples.
WS_OVERLAPPEDWINDOW, will never be changed to
OverlappedWindowto look more like C#. The only exceptions:
WMand
VirtualKey- the message id, and virtual key constants for simpler usability).
WinApi.Windows- See below.
public enum WindowStyles { .. WS_OVERLAPPEDWINDOW = 0x00. }). Never WINDOWSTYLE, or MARGINS or RECT. Always
Margin,
Rectangle, etc. (It actually is surprisingly clean once drop the usual depencendies like WinForms, or WPF which always provide alternative forms).
intfor Windows types like
BOOL- to ensure minimum Marashalling impact when inside a structure. Using
boolrequires another copy, since bool in CLR is 1 byte, but the unmanaged variant could be 1, 2 or 4 bytes, depending on the context. However, when it comes to functions
boolis used directly, since int conversion there is not only tedious but is bound to loose semantic value.
IntelliSense capable. No MSDN round-trips, while doing low level programming with CLR.
Methodssuffix. (
User32Methods,
Kernel32Methods,
DwmApiMethods, etc). Prefered to use
int,
uintetc inside the
*Methodsclass to ensure parity with native APIs. Enums can be used for flags only if the value is a strictly well defined constant set. Otherwise prefer int, uint, etc. However, type safe wrappers can be supplemented in the
Helpers.
Helpers(
User32Helpers,
Kernel32Helpers, etc).
Experimentalnamespace similarly.
While there aren't many well defined reliable wrappers, there are a few - my favorite being Pinvoke (https://github.com/AArnott/pinvoke). While
Goalsabove, should explain the reasons for re-inventing the wheel, it's also mostly a matter for coding style, and about having the ability to micro-optimize when you really need to.
--- LibraryName -- Types.cs (Structs, enums and other constants) -- Methods.cs (All direct native methods) -- Helpers.cs (All the helper methods with type safety wrappers) ## Constants.cs (Optionally, if there are too many types, split constants (enums) from pure structs)
WinApi:
WinApi.Windows- https://github.com/prasannavl/WinApi/blob/master/Samples/Sample.SimpleWindow/Program.cs
WinApi.DxUtils- https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.DirectX
SendInputhelpers - https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.SimulateInput
SkiaSharp- https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.Skia
Methods.csor
Constants.cs)
Helpers.cs, and/or any other library features require a separate PR as well. When in doubt, separate it out.
WinApi
(This section is community editable. Please help yourself)
Thanks to JetBrains for the OSS license of Resharper Ultimate.
Proudly developed using:
This project is licensed under either of the following, at your choice:
Contribution to the LiquidState project is organized under the terms of the Contributor Covenant, and as such the maintainer @prasannavl promises to intervene to uphold that code of conduct.