{"id":15039210,"url":"https://github.com/prasannavl/winapi","last_synced_at":"2025-05-15T08:05:29.543Z","repository":{"id":10766141,"uuid":"66861834","full_name":"prasannavl/WinApi","owner":"prasannavl","description":"A simple, direct, ultra-thin CLR library for high-performance Win32 Native Interop","archived":false,"fork":false,"pushed_at":"2022-12-07T19:33:54.000Z","size":720,"stargazers_count":839,"open_issues_count":28,"forks_count":102,"subscribers_count":42,"default_branch":"master","last_synced_at":"2025-05-15T08:05:24.522Z","etag":null,"topics":["directx","dotnet","dotnetcore","winapi"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prasannavl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-29T16:45:18.000Z","updated_at":"2025-04-28T14:06:28.000Z","dependencies_parsed_at":"2023-01-13T16:08:32.181Z","dependency_job_id":null,"html_url":"https://github.com/prasannavl/WinApi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FWinApi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FWinApi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FWinApi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasannavl%2FWinApi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prasannavl","download_url":"https://codeload.github.com/prasannavl/WinApi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254301431,"owners_count":22047904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directx","dotnet","dotnetcore","winapi"],"created_at":"2024-09-24T20:41:56.093Z","updated_at":"2025-05-15T08:05:29.503Z","avatar_url":"https://github.com/prasannavl.png","language":"C#","readme":"﻿# WinApi\n\nA simple, direct, ultra-thin CLR library for high-performance Win32 Native Interop\n\n[![NuGet badge](https://buildstats.info/nuget/WinApi)](https://www.nuget.org/packages/WinApi)\n[![NuGet badge](https://buildstats.info/nuget/WinApi.Desktop)](https://www.nuget.org/packages/WinApi.Desktop)\n[![NuGet badge](https://buildstats.info/nuget/WinApi.Utils)](https://www.nuget.org/packages/WinApi.Utils)\n[![NuGet badge](https://buildstats.info/nuget/WinApi.DxUtils)](https://www.nuget.org/packages/WinApi.DxUtils)\n[![NuGet badge](https://buildstats.info/nuget/WinApi.Windows.Controls)](https://www.nuget.org/packages/WinApi.Windows.Controls)\n\n```c#\nstatic int Main(string[] args)\n{\n    using (var win = Window.Create(text: \"Hello\"))\n    {\n        win.Show();\n        return new EventLoop().Run(win);\n    }\n}\n```\n\n**Nuget:**\n\u003e Install-Package WinApi\n\nFully supports the **CoreCLR.** Uses C# 7 features like `ref returns` to achieve performance without losing semantic value.\n\n### Articles\n- \u003ca href=\"https://www.prasannavl.com/2016/10/introducing-winapi-the-evolution/\"\u003eIntroducing WinApi: The Evolution\u003c/a\u003e\n- \u003ca href=\"https://www.prasannavl.com/2016/10/introducing-winapi-basics/\"\u003eIntroducing WinApi: Basics\u003c/a\u003e\n- \u003ca href=\"https://www.prasannavl.com/2016/10/introducing-winapi-graphics-with-direct3d-d2d1-gdi-opengl-and-skia\"\u003eIntroducing WinApi: Graphics with Direct3D, D2D1, GDI, OpenGL and Skia\u003c/a\u003e\n- \u003ca href=\"https://www.prasannavl.com/2016/10/introducing-winapi-comparing-gc-pressure-and-performance-with-winforms\"\u003eIntroducing WinApi: Comparing GC pressure and performance with WinForms\u003c/a\u003e\n\n#### TL;DR: WinForms Comparison\n```\nDirect message loop performance: 20-35% faster.\nHeap allocation: 0MB vs. roughly, 0.75GB / 100k messages.\nMemory page faults (Soft): 0.005% - A mere 5k vs. roughly 1 million faults/100k messages)\n```\n\n### Packages\n- **`WinApi`** - The core package that contains all the methods, helpers, and the tiny `WinApi.Windows` namespace.\n- **`WinApi.Desktop`** - Desktop-only helpers.\n- **`WinApi.Utils`** - Provides utilities like `NativePixelBuffer`, `DwmWindow` etc.\n- **`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.\n- **`WinApi.Windows.Controls`** [Incomplete] - A small library that implements the `EventedWindowCore` for standard classes like `Static`, `Edit` and 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.\n\n**Note:** - Starting from v4, all packages are of minimum `netstandard 1.4`, and `Source` nuget packages are no more. `Desktop` package is `netstandard 2.0`.\n\n### WinApi.Windows\n\n- Ultra-light weight, extremely simple and tiny wrappers that can be used to create, manipulate or use windows extensively.\n- `Zero GC allocations` on during window messages, and event loop cycles.\n- `Fundamental concepts similar to ATL/WTL`, but in a C# idiomatic way.\n- NativeWindow class is a very thin Window class that processes no messages, and provides no extra functionality. Great for using with custom GUI toolkits, DirectX, OpenGL games.\n- NativeWindow can also be extended to work with any subclasses like Button, ComboBox, etc, with the same principles.\n- A GUI wrapper for Win32 that `can work with CoreCLR`.\n- Can be wrapped over any existing windows, just by using the handle.\n- Strict `pay-only-for-what-you-use model`.\n- Several different event loops depending on the need (For example, `RealtimeEventLoop` for games while the simple `EventLoop` is ideal for normal applications).\n\n### Goals\n\n- Every single method is `hand-written from a combination of auto-generation from Windows SDK headers and MSDN`, and tested for correctness.\n- Provide both safe (through helpers, and safety wrappers like SafeHandles, CriticalHandles), and unsafe wrappers (pure with minimal performance impact), in a clean way supplemented with inline documentation.\n- Provide a single DLL that can over time, be a direct equivalent of C/C++ `windows.h` header file for the CLR. Other Windows SDK wrappers may, or may not be in fragmented into separate packages.\n- Sufficient base to be able to write custom toolkits over Win32 based on Direct2D, Direct3D or even an external graphics library like Skia, without depending on WPF or WinForms - `Examples of usage with Direct2D, 3D, Skia, OpenGL are all in the samples`.\n- Always retain parity with the native API when it comes to constants (Eg: `WS_OVERLAPPEDWINDOW`, will never be changed to `OverlappedWindow` to look more like C#. The only exceptions: `WM` and `VirtualKey` - the message id, and virtual key constants for simpler usability).\n- `WinApi.Windows` - See below.\n- All structs, flags, should always have the names in the idiomatic C# style. (Eg: `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).\n- Use variants such as `int` for Windows types like `BOOL` - to ensure minimum Marashalling impact when inside a structure. Using `bool` requires 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 `bool` is used directly, since int conversion there is not only tedious but is bound to loose semantic value.\n\n### Secondary goals\n\n- Provide fully documented API (both from headers and MSDN, where-ever applicable) in the releases. Everything should be `IntelliSense capable`. No MSDN round-trips, while doing low level programming with CLR.\n\n\n### Notes\n\n- All methods in its minimal interop form (no SafeHandles, CriticalHandles, etc) unless absolutely required, for maximum micro-optimization of interop scenarios in the class with `Methods` suffix. (`User32Methods`, `Kernel32Methods`, `DwmApiMethods`, etc). Prefered to use `int`, `uint` etc inside the `*Methods` class 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`.\n- All methods with handles, enums and other supplemented types go into `Helpers` (`User32Helpers`, `Kernel32Helpers`, etc).\n- Everything that uses undocumented APIs is maintained in a separate `Experimental` namespace similarly.\n\n### Why re-invent the wheel?\n\nWhile there aren't many well defined reliable wrappers, there are a few - my favorite being Pinvoke (https://github.com/AArnott/pinvoke). While `Goals` above, 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.\n\n### Filesystem structure\n```\n--- LibraryName\n    --  Types.cs (Structs, enums and other constants)\n    --  Methods.cs (All direct native methods)\n    --  Helpers.cs (All the helper methods with type safety wrappers)\n    ##  Constants.cs (Optionally, if there are too many types, split constants (enums) from pure structs)\n```\n\n## Samples\n\n\n#### C/C++ Samples to serve as comparison standard:\n- Win32 C - https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.Native.Win32/main.cpp\n- ATL/C++ - https://github.com/prasannavl/WinApi/blob/master/Samples/Sample.Native.Atl/CAppWindow.cpp\n\n#### C# Samples using `WinApi`:\n\n- Raw equivalent of C Sample: https://github.com/prasannavl/WinApi/blob/master/Samples/Sample.Win32/Program.cs\n- Equivalent of C and ATL/C++ using `WinApi.Windows` - https://github.com/prasannavl/WinApi/blob/master/Samples/Sample.SimpleWindow/Program.cs\n- DirectX using `WinApi.DxUtils` - https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.DirectX\n- OpenGL - https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.OpenGL\n- Keyboard Input Simulation with `SendInput` helpers - https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.SimulateInput\n- Using Skia as the primary 2D drawing backend with `SkiaSharp` - https://github.com/prasannavl/WinApi/tree/master/Samples/Sample.Skia\n\nContributions\n---\n- Please follow the file structure detailed. \n- Please avoid batching up commits in your PRs. Keep pure Win32 methods, and constants in a separate one so they can easily be merged. (Anything that usually belongs in `Methods.cs` or `Constants.cs`)\n- Use your discretion to decide whether `Helpers.cs`, and/or any other library features require a separate PR as well. When in doubt, separate it out.\n- Beyond that feel free to follow your usual standards - feature/bugfix, etc based batching.\n\nCommunity projects using `WinApi`\n---\n(This section is community editable. Please help yourself)\n\n- [Chromely](https://github.com/mattkol/Chromely) : Build .NET/.NET Core HTML5 desktop apps using cross-platform native GUI API.\n\nCredits\n---\nThanks to [JetBrains](https://www.jetbrains.com) for the OSS license of Resharper Ultimate.\n\nProudly developed using:\n\n\u003ca href=\"https://www.jetbrains.com/resharper/\n\"\u003e\u003cimg src=\"https://blog.jetbrains.com/wp-content/uploads/2014/04/logo_resharper.gif\" alt=\"Resharper logo\" width=\"100\" /\u003e\u003c/a\u003e\n\nLicense\n---\n\nThis project is licensed under either of the following, at your choice:\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0))\n* GPL 3.0 license ([LICENSE-GPL](LICENSE-GPL) or [https://opensource.org/licenses/GPL-3.0](https://opensource.org/licenses/GPL-3.0))\n\nCode of Conduct\n---\n\nContribution to the LiquidState project is organized under the terms of the Contributor Covenant, and as such the maintainer [@prasannavl](https://github.com/prasannavl) promises to intervene to uphold that code of conduct.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprasannavl%2Fwinapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprasannavl%2Fwinapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprasannavl%2Fwinapi/lists"}