Project Icon

Vanara

强大的Windows API封装库,简化.NET开发

Vanara是一个功能全面的.NET开源库,封装了Windows API的P/Invoke调用。该库涵盖多个Windows库的函数、接口、枚举和结构,兼容多个.NET版本,并为WinForms、WPF和UWP提供扩展。Vanara使开发者能够在.NET项目中便捷地调用Windows原生功能,从而提升开发效率。

Vanara

Version Downloads Build status

This project contains various .NET assemblies that contain P/Invoke functions, interfaces, enums and structures from Windows libraries. Each assembly is associated with one or a few tightly related libraries. For example, Shlwapi.dll has all the exported functions from shlwapi.lib; Kernel32.dll has all for both kernel32.lib and kernelbase.lib.

All assemblies are available via NuGet and provide builds against .NET 4.5, 4.8, 5.0, 6.0, .NET Standard 2.0, Core 3.1 and support SourceLink. Extensions are available for WinForms, WPF, and UWP projects. If you need support for other .NET versions, look to versions 3.3.15 and earlier.

This project releases new versions every few weeks after sufficient testing. New releases are cataloged, along with release notes, in the Releases section and all NuGet packages are published to nuget.org. Each GitHub push triggers an AppVeyor build. The owners thank them for the free Open-Source account! The status of that build is in the header of this page. The NuGet packages from those builds are available for test purposes on AppVeyor's project NuGet source at https://ci.appveyor.com/nuget/vanara-prerelease.

Use

  1. Look for the function you need in Microsoft documentation. Note which library or DLL the function is in.
  2. Confirm the Vanara library exists and has your function by looking at the Supported Libraries table below. Clicking on the Assembly link will take you to a drill down of that assembly's coverage. Find your function and if there is a matching implementation it will appear to the right. You can also use GitHub's project search (upper left of page) to search for your function, method or constant. Make sure to select "In this repository".
  3. Add the assembly to your project via NuGet.
  4. To use the function, you can:
    1. Call it directly var bret = Vanara.PInvoke.Kernel32.GetComputerName(sb, ref sbSz);
    2. Under C# 6.0 and later, use a static using directive and call it:
    using static Vanara.PInvoke.Kernel32;
    
    var bret = GetComputerName(sb, ref sbSz);
    
  5. In some cases there is a corresponding helper/wrapper class in one of the Supporting Assemblies, especially for Security, System Services, Forms and Shell. Go to their library page (click on link in section) and look through the classes included in each library.

Design Concepts

  • All functions that are imported from a single DLL should be placed into a single assembly that is named after the DLL.
    • (e.g. The assembly Vanara.PInvoke.Gdi32.dll hosts all functions and supporting enumerations, constants and structures that are exported from gdi32.dll in the system directory.)
  • Any structure or macro or enumeration (no function) that is used by many libraries is put into either Vanara.Core or Vanara.PInvoke.Shared.
    • (e.g. The macro HIWORD and the structure SIZE are both in Vanara.PInvoke.Shared and classes to simplify interop calls and native memory management are in Vanara.Core.)
  • Inside a project, all constructs are contained in a file named after the header file (*.h) in which they are defined in the Windows API.
    • (e.g. In the Vanara.PInvoke.Kernel32 project directory, you'll find a FileApi.cs, a WinBase.cs and a WinNT.cs file representing fileapi.h, winbase.h and winnt.h respectively.)
  • Where the direct interpretation of a structure or function leads to memory leaks or misuse, I have tried to simplify its use.
  • Where a structure is always passed by reference and where that structure needs to clean up memory allocations, I have changed the structure to a class implementing IDisposable.
  • Wherever possible, all handles have been turned into SafeHandle derivatives named after the Windows API handle. If those handles require a call to a function to release/close/destroy, a derived SafeHANDLE exists that performs that function on disposal.
    • e.g. HTOKEN is defined. SafeHTOKEN builds upon that handle with an automated release calling CloseHandle.
  • Wherever possible, all functions that allocate memory that is to be freed by the caller use a safe memory handle.
  • All PInvoke calls are in assemblies prefixed by Vanara.PInvoke.
  • If a structure is to passed into a function as a constant, that structure is marshaled using the in statement which will pass the structure by reference without requiring the ref keyword.
    • Windows API: BOOL MapDialogRect(HWND hDlg, LPRECT lpRect)
    • Vanara: bool MapDialogRect(HWND hDlg, in RECT lpRect);
  • If there are classes or extensions that make use of the PInvoke calls, they are in wrapper assemblies prefixed by Vanara and then followed by a logical name for the functionality. Today, those are Core, Security, SystemServices, Windows.Forms and Windows.Shell.

Supported Libraries

Library/DLLAssemblyCoverageNuGet Link       
aclui.dllVanara.PInvoke.AclUICoverageNugetNuget
advapi32.dll, secur32.dll, authz.dll, sspicli.dll, schannel.dll, tdh.dllVanara.PInvoke.SecurityCoverageNugetNuget
amsi.dllVanara.PInvoke.AMSICoverageNugetNuget
avicap32.dll, avifil32.dll, msacm32.dll, msvfw32.dll, winmm.dllVanara.PInvoke.MultimediaCoverageNugetNuget
avrt.dllVanara.PInvoke.AvrtCoverageNugetNuget
cabinet.dllVanara.PInvoke.CabinetCoverageNugetNuget
CfgMgr32.dllVanara.PInvoke.CfgMgr32CoverageNugetNuget
cimfs.dllVanara.PInvoke.CimFsCoverageNugetNuget
CldApi.dllVanara.PInvoke.CldApiCoverageNugetNuget
clfsw32.dllVanara.PInvoke.ClfsW32CoverageNugetNuget
comctl32.dllVanara.PInvoke.ComCtl32CoverageNugetNuget
ComDlg32.dllVanara.PInvoke.ComDlg32CoverageNugetNuget
credui.dllVanara.PInvoke.CredUICoverageNugetNuget
crypt32.dll, bcrypt.dll, ncrypt.dll, tokenbinding.dll, cryptui.dll, cryptnet.dll, cryptdlg.dllVanara.PInvoke.CryptographyCoverageNugetNuget
d2d1.dll, dxgi.dll, dwrite.dll, windowscodecs.dllVanara.PInvoke.GraphicsCoverageNugetNuget
davclnt.dllVanara.PInvoke.DavClntCoverageNugetNuget
DbgHelp.dll, ImageHlp.dllVanara.PInvoke.DbgHelpCoverageNugetNuget
Dhcpcsvc6.dll, Dhcpcsvc.dllVanara.PInvoke.Dhcp
项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

白日梦AI

白日梦AI提供专注于AI视频生成的多样化功能,包括文生视频、动态画面和形象生成等,帮助用户快速上手,创造专业级内容。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

讯飞绘镜

讯飞绘镜是一个支持从创意到完整视频创作的智能平台,用户可以快速生成视频素材并创作独特的音乐视频和故事。平台提供多样化的主题和精选作品,帮助用户探索创意灵感。

Project Cover

讯飞文书

讯飞文书依托讯飞星火大模型,为文书写作者提供从素材筹备到稿件撰写及审稿的全程支持。通过录音智记和以稿写稿等功能,满足事务性工作的高频需求,帮助撰稿人节省精力,提高效率,优化工作与生活。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号