Awesome Open Source
Awesome Open Source

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 2.0, 3.5, 4.0, 4.5, Core 3.0, Core 3.1 and .NET 5.0 (new in v3.2.20) and support SourceLink. In all cases where a dependency doesn't disallow it, .NET Standard 2.0, .NET Core 2.0, and 2.1 builds are also included for use with UWP and other .NET Core and Standard projects.

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/DLL Assembly Coverage NuGet Link       
aclui.dll Vanara.PInvoke.AclUI Coverage NugetNuget
advapi32.dll, secur32.dll, authz.dll, sspicli.dll, schannel.dll Vanara.PInvoke.Security Coverage NugetNuget
cabinet.dll Vanara.PInvoke.Cabinet Coverage NugetNuget
CfgMgr32.dll Vanara.PInvoke.CfgMgr32 Coverage NugetNuget
CldApi.dll Vanara.PInvoke.CldApi Coverage NugetNuget
comctl32.dll Vanara.PInvoke.ComCtl32 Coverage NugetNuget
ComDlg32.dll Vanara.PInvoke.ComDlg32 Coverage NugetNuget
credui.dll Vanara.PInvoke.CredUI Coverage NugetNuget
crypt32.dll, bcrypt.dll, ncrypt.dll, tokenbinding.dll, cryptui.dll, cryptnet.dll, cryptdlg.dll Vanara.PInvoke.Cryptography Coverage NugetNuget
d2d1.dll, dxgi.dll, dwrite.dll, windowscodecs.dll Vanara.PInvoke.Graphics Coverage NugetNuget
DbgHelp.dll, ImageHlp.dll Vanara.PInvoke.DbgHelp Coverage NugetNuget
Dhcpcsvc6.dll, Dhcpcsvc.dll Vanara.PInvoke.Dhcp Coverage NugetNuget
DnsApi.dll Vanara.PInvoke.DnsApi Coverage NugetNuget
Drt.dll Vanara.PInvoke.Drt Coverage NugetNuget
dwmapi.dll Vanara.PInvoke.DwmApi Coverage NugetNuget
FirewallApi.dll Vanara.PInvoke.FirewallApi Coverage NugetNuget
FunDisc.dll Vanara.PInvoke.FunDisc Coverage NugetNuget
gdi32.dll Vanara.PInvoke.Gdi32 Coverage NugetNuget
imapi2.dll, imapi2fs.dll Vanara.PInvoke.IMAPI Coverage NugetNuget
iphlpapi.dll Vanara.PInvoke.IpHlpApi Coverage NugetNuget
IScsiDsc.dll Vanara.PInvoke.IScsiDsc Coverage NugetNuget
kernel32.dll, kernelbase.dll, normaliz.dll, vertdll.dll Vanara.PInvoke.Kernel32 Coverage NugetNuget
ktmw32.dll Vanara.PInvoke.KtmW32 Coverage NugetNuget
Lz32.dll Vanara.PInvoke.Lz32 Coverage NugetNuget
magnification.dll Vanara.PInvoke.Magnification Coverage NugetNuget
mpr.dll Vanara.PInvoke.Mpr Coverage NugetNuget
msctf.dll, input.dll, msimtf.dll Vanara.PInvoke.TextServicesFramework Coverage NugetNuget
Msi.dll Vanara.PInvoke.Msi Coverage NugetNuget
netapi32.dll Vanara.PInvoke.NetApi32 Coverage NugetNuget
netprofm.dll Vanara.PInvoke.NetListMgr Coverage NugetNuget
NewDev.dll Vanara.PInvoke.NewDev Coverage NugetNuget
ntdll.dll Vanara.PInvoke.NTDll Coverage NugetNuget
ntdsapi.dll Vanara.PInvoke.NTDSApi Coverage NugetNuget
ole32.dll, oleaut32.dll, propsys.dll Vanara.PInvoke.Ole Coverage NugetNuget
oleacc.dll Vanara.PInvoke.Accessibility Coverage NugetNuget
OleDlg.dll Vanara.PInvoke.OleDlg Coverage NugetNuget
opcservices.dll Vanara.PInvoke.Opc Coverage NugetNuget
P2P.dll Vanara.PInvoke.P2P Coverage NugetNuget
pdh.dll Vanara.PInvoke.Pdh Coverage NugetNuget
PeerDist.dll Vanara.PInvoke.PeerDist Coverage NugetNuget
powrprof.dll Vanara.PInvoke.PowrProf Coverage NugetNuget
ProjectedFSLib.dll Vanara.PInvoke.ProjectedFSLib Coverage NugetNuget
qmgr.dll Vanara.PInvoke.BITS Coverage NugetNuget
rpcrt4.dll Vanara.PInvoke.Rpc Coverage NugetNuget
RstrtMgr.dll Vanara.PInvoke.RstrtMgr Coverage NugetNuget
SearchApi Vanara.PInvoke.SearchApi Coverage NugetNuget
SetupAPI.dll Vanara.PInvoke.SetupAPI Coverage NugetNuget
SHCore.dll Vanara.PInvoke.SHCore Coverage NugetNuget
shell32.dll, url.dll Vanara.PInvoke.Shell32 Coverage NugetNuget
shlwapi.dll Vanara.PInvoke.ShlwApi Coverage NugetNuget
taskschd.dll, mstask.dll Vanara.PInvoke.TaskSchd Coverage NugetNuget
UrlMon.dll Vanara.PInvoke.UrlMon Coverage NugetNuget
user32.dll Vanara.PInvoke.User32 Coverage NugetNuget
UserEnv.dll Vanara.PInvoke.UserEnv Coverage NugetNuget
uxtheme.dll Vanara.PInvoke.UxTheme Coverage NugetNuget
Version.dll Vanara.PInvoke.Version Coverage NugetNuget
virtdisk.dll Vanara.PInvoke.VirtDisk Coverage NugetNuget
WcmApi.dll Vanara.PInvoke.WcmApi Coverage NugetNuget
WcnApi.dll Vanara.PInvoke.WcnApi Coverage NugetNuget
wer.dll Vanara.PInvoke.Wer Coverage NugetNuget
wininet.dll Vanara.PInvoke.WinINet Coverage NugetNuget
winmm.dll, msacm32.dll, avifil32.dll Vanara.PInvoke.Multimedia Coverage NugetNuget
winspool.drv, prntvpt.dll Vanara.PInvoke.Printing Coverage NugetNuget
wintrust.dll Vanara.PInvoke.WinTrust Coverage NugetNuget
WlanApi.dll, Wlanui.dll Vanara.PInvoke.WlanApi Coverage NugetNuget
ws2_32.dll Vanara.PInvoke.Ws2_32 Coverage NugetNuget
WsmSvc.dll Vanara.PInvoke.WsmSvc Coverage NugetNuget
WTSApi32.dll Vanara.PInvoke.WTSApi32 Coverage NugetNuget

Supporting Assemblies

Assembly    NuGet Link    Description
Vanara.BITS NugetNuget .NET classes to access Background Intelligent Transfer Service (BITS) functionality. Intelligently uses most recent library functions and gracefully fails when new features are not available on older OS versions.
Vanara.Core NugetNuget This library includes shared methods, structures and constants for use throughout the Vanara assemblies. Think of it as windows.h with some useful extensions. It includes:
* Extension methods for working with enumerated types (enum), FILETIME, and method and property extractions via reflection
* Extension and helper methods to marshaling structures arrays and strings
* SafeHandle based classes for working with memory allocated via CoTaskMem, HGlobal, or Local calls that handles packing and extracting arrays, structures and raw memory
* Safe pinning of objects in memory
* Memory stream based on marshaled memory
Vanara.PInvoke.Shared NugetNuget Shared methods, structures and constants for use throughout the Vanara.PInvoke assemblies. Includes:
* IEnumerable helpers for COM enumerations
* Custom marshaler for CoTaskMem pointers
* Enhanced error results classes for HRESULT, Win32Error and NTStatus
* Standard windows.h macros (e.g. HIWORD, MAKELONG, etc.)
* Overlapped method wrapper
* Resource ID holder
* Shared structures and enums (see release notes)
Vanara.Security NugetNuget Classes for security related items derived from the Vanara PInvoke libraries. Includes extension methods for Active Directory and access control classes, methods for working with accounts, UAC, privileges, system access, impersonation and SIDs, and a full LSA wrapper.
Vanara.SystemServices NugetNuget Classes for system related items derived from the Vanara PInvoke libraries. Includes extensions for Process (privileges and elavation), FileInfo (compression info), Shared Network Drives and Devices, and ServiceController (SetStartType) that pull extended information through native API calls.
Vanara.VirtualDisk NugetNuget .NET classes to manage Windows Virtual Storage (VHD and VHDX) using P/Invoke functions from VirtDisk.dll.
Vanara.Windows.Forms NugetNuget Classes for user interface related items derived from the Vanara PInvoke libraries. Includes extensions for almost all common controls to give post Vista capabilities, WinForms controls (panel, commandlink, enhanced combo boxes, IPAddress, split button, trackbar and themed controls), shutdown/restart/lock control, buffered painting, resource files, access control editor, simplifed designer framework for Windows.Forms.
Vanara.Windows.Shell NugetNuget Classes for Windows Shell items derived from the Vanara PInvoke libraries. Includes shell items, files, icons, links, and taskbar lists.

Quick Links

Sample Code

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


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
c-sharp (12,444
shell (10,397
windows (1,460
dotnet (895
nuget (156
dotnet-standard (75
extensions (72
interop (26
controls (20
winapi (18