NET class library "Vanara": An easy-to-use Windows API encapsulation library

NET class library "Vanara": An easy-to-use Windows API encapsulation library

A series of very easy-to-use. NET class libraries that have excellent encapsulation of the Windows API make it almost unnecessary to write cumbersome Windows API conversion functions.

最后更新 6/28/2021 10:53 PM
沙漠尽头的狼
预计阅读 5 分钟
分类
.NET
专题
C#Open Source Project
标签
.NET C# Windows API

Warehouse address: github.com/dahall/Vanara

Translation: Waves at the end of the desert (the level is limited, if you have any conditions, please try to check the warehouse address and read it)

A series of very easy-to-use. NET class libraries that have excellent encapsulation of the Windows API make it almost unnecessary to write cumbersome Windows API conversion functions.

This project contains various. NET assemblies that contain P/Invoke functions, interfaces, enumerations, and constructs from the Windows library. Each assembly is associated with one or more closely related libraries. For example, Shlwapi.dll contains all functions exported from Shlwapi.lib;Kernel32.dll contains all of Kernel32.lib and Kernelbase.lib.

所有程序集都可通过 NuGet 获得,并提供针对.NET 2.0、3.5、4.0、4.5、Core 3.0、Core 3.1 和.NET 5.0(v3.2.20 中新增)的版本,并支持SourceLink。在依赖项不允许的所有情况下,.NET Standard 2.0、.NET Core 2.0 和 2.1 版本也包含在 UWP 和其他.NET Core 及标准项目中。

在充分测试之后,这个项目每隔几周发布一次新版本。新的版本和发行说明一起被编目在Releases部分,所有 NuGet 包都发布到nuget.org。每个 GitHub 推送都会触发AppVeyor构建。所有者感谢他们的免费开源账户!文章开头显示了项目构建状态信息。AppVeyor 源用于构建 NuGet 包。

How to use it?

  1. Find the function you want in the Microsoft documentation. Notice which library or DLL the function is located in.
  2. Check the supporting library table below to confirm that Vanara is in stock and has the functions you need (Windows API). Clicking the assembly link will take you deeper into the coverage of the assembly. Find your function and if there is a matching implementation, it will appear on the right. You can also use GitHub's Project Search (upper left corner of the page) to search for functions, methods, or constants. Make sure to select "In this repository".
  3. Add assemblies to your project through NuGet.
  4. To use this feature, you can:
    1. 直接调用var bret = Vanara.PInvoke.Kernel32.GetComputerName(sb, ref sbSz);
    1. Under C#6.0 and later, use the static using directive and call it:
using static Vanara.PInvoke.Kernel32;

var bret = GetComputerName(sb, ref sbSz);
  1. In some cases, one of the supported assemblies has a corresponding helper/wrapper class, especially for security, system services, forms, and shells. Go to their libraries page (click the link in the section) and browse the classes contained in each library.

design concept

  • All functions imported from a single DLL should be placed in a single assembly named after the DLL.
    • (例如,程序集Vanara.PInvoke.Gdi32.dll承载系统目录中从gdi32.dll导出的所有函数和支持的枚举、常量和结构。)
  • 任何由许多库使用的结构、宏或枚举(非函数)都会放入Vanara.Core或'Vanara.PInvoke.Shared`库中。
    • (例如,宏HIWORD'和结构SIZE都在Vanara.PInvoke.Shared中,简化互操作调用和本机内存管理的类都在'Vanara.Core中)
  • In the project, all constructs are contained in a file named after the header file (*.h), where these constructs are defined in the Windows API.
    • (例如,在Vanara.PInvoke.Kernel32项目目录中,您将分别找到一个 FileApi.cs、WinBase.cs 和一个 WinNT.cs 文件,分别表示 FileApi.h、WinBase.h 和 WinNT.h)
  • If directly interpreting a structure or function would lead to memory leaks or misuse, I try to simplify its use.
  • 在结构体总是通过引用传递,并且在需要清理内存分配的地方,我将结构体更改为实现IDispoable的类。
  • 尽可能,所有句柄都已转换为以 Windows API 句柄命名的SafeHandle派生工具。如果这些句柄需要调用函数以释放/关闭/销毁,则存在一个派生的SafeHANDLE,该函数将在 disposal 时执行该函数。
    • 例如,定义了HTOKENSafeHTOKEN在该句柄上调用CloseHandle自动释放。
  • Whenever possible, all functions that allocate memory freed by the caller use safe memory handles.
  • 程序集中所有 PInvoke 调用都以'Vanara.PInvoke`为前缀。
  • 如果要将结构体作为常量传递到函数中,则使用in语句封装该结构体,该语句将通过引用传递结构体,而不需要ref关键字。
    • Windows API:BOOL MapDialogRect(HWND hDlg, LPRECT LPRECT)
    • Vanara:bool MapDialogRect(HWND hDlg, in RECT lpRect);
  • If there are classes or extensions that use PInvoke calls, they are in the wrapper set prefixed with 'Vanara' followed by the logical name of the feature. Today, they are Core, Security, SystemServices, Windows.Forms, and Windows.Shell.

Supported libraries

Supported assemblies

sample code

There are numerous examples in the UnitTest folder and in the WinClassicSamplesCS project that recreates the Windows Samples in C# using Vanara.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读