Skip to content
/ Vanara Public

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.

License

Notifications You must be signed in to change notification settings

dahall/Vanara

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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/DLL Assembly Coverage NuGet Link       
aclui.dll Vanara.PInvoke.AclUI Coverage NugetNuget
activeds.dll Vanara.PInvoke.ActiveDS Coverage NugetNuget
advapi32.dll, secur32.dll, authz.dll, sspicli.dll, schannel.dll, tdh.dll Vanara.PInvoke.Security Coverage NugetNuget
amsi.dll Vanara.PInvoke.AMSI Coverage NugetNuget
avicap32.dll, avifil32.dll, msacm32.dll, msvfw32.dll, winmm.dll Vanara.PInvoke.Multimedia Coverage NugetNuget
avrt.dll Vanara.PInvoke.Avrt Coverage NugetNuget
cabinet.dll Vanara.PInvoke.Cabinet Coverage NugetNuget
CfgMgr32.dll Vanara.PInvoke.CfgMgr32 Coverage NugetNuget
cimfs.dll Vanara.PInvoke.CimFs Coverage NugetNuget
CldApi.dll Vanara.PInvoke.CldApi Coverage NugetNuget
clfsw32.dll Vanara.PInvoke.ClfsW32 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
davclnt.dll Vanara.PInvoke.DavClnt 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
dosvc.dll Vanara.PInvoke.DOSvc Coverage NugetNuget
drt.dll, drtprov.dll, drttransport.dll Vanara.PInvoke.Drt Coverage NugetNuget
dwmapi.dll Vanara.PInvoke.DwmApi Coverage NugetNuget
ehstorapi.dll Vanara.PInvoke.EnhancedStorage Coverage NugetNuget
elscore.dll Vanara.PInvoke.ElsCore Coverage NugetNuget
fhsvcctl.dll Vanara.PInvoke.FhSvcCtl Coverage NugetNuget
FirewallApi.dll Vanara.PInvoke.FirewallApi Coverage NugetNuget
FunDisc.dll Vanara.PInvoke.FunDisc Coverage NugetNuget
fwpuclnt.dll Vanara.PInvoke.FwpUClnt Coverage NugetNuget
gdi32.dll Vanara.PInvoke.Gdi32 Coverage NugetNuget
httpapi.dll Vanara.PInvoke.HttpApi Coverage NugetNuget
imapi2.dll, imapi2fs.dll Vanara.PInvoke.IMAPI Coverage NugetNuget
imm32.dll Vanara.PInvoke.Imm32 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
mmdevapi.dll Vanara.PInvoke.CoreAudio Coverage NugetNuget
mpr.dll Vanara.PInvoke.Mpr Coverage NugetNuget
msctf.dll, input.dll, msimtf.dll Vanara.PInvoke.TextServicesFramework Coverage NugetNuget
msftedit.dll,riched20.dll Vanara.PInvoke.MsftEdit Coverage NugetNuget
Msi.dll Vanara.PInvoke.Msi Coverage NugetNuget
MsRdc.dll Vanara.PInvoke.MsRdc Coverage NugetNuget
ndfapi.dll Vanara.PInvoke.NdfApi 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
odbc32.dll Vanara.PInvoke.Odbc32 Coverage NugetNuget
ole32.dll, oleaut32.dll, propsys.dll Vanara.PInvoke.Ole Coverage NugetNuget
oleacc.dll Vanara.PInvoke.Accessibility Coverage NugetNuget
oledb.dll Vanara.PInvoke.OleDb 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
photoacq.dll Vanara.PInvoke.PhotoAcquire Coverage NugetNuget
portabledeviceapi.dll, portabledeviceconnectapi.dll, portabledevicetypes.dll Vanara.PInvoke.PortableDeviceApi Coverage NugetNuget
powrprof.dll Vanara.PInvoke.PowrProf Coverage NugetNuget
ProjectedFSLib.dll Vanara.PInvoke.ProjectedFSLib Coverage NugetNuget
qmgr.dll Vanara.PInvoke.BITS Coverage NugetNuget
qwave.dll, traffic.dll Vanara.PInvoke.QoS Coverage NugetNuget
rpcrt4.dll Vanara.PInvoke.Rpc Coverage NugetNuget
RstrtMgr.dll Vanara.PInvoke.RstrtMgr Coverage NugetNuget
SearchApi Vanara.PInvoke.SearchApi Coverage NugetNuget
sensorsapi.dll Vanara.PInvoke.SensorsApi 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
uianimation.dll Vanara.PInvoke.UIAnimation Coverage NugetNuget
uiautomationcore.dll Vanara.PInvoke.UIAutomation Coverage NugetNuget
UrlMon.dll Vanara.PInvoke.UrlMon Coverage NugetNuget
user32.dll Vanara.PInvoke.User32 Coverage NugetNuget
UserEnv.dll Vanara.PInvoke.UserEnv Coverage NugetNuget
usp10.dll Vanara.PInvoke.Usp10 Coverage NugetNuget
uxtheme.dll Vanara.PInvoke.UxTheme Coverage NugetNuget
Version.dll Vanara.PInvoke.Version Coverage NugetNuget
virtdisk.dll Vanara.PInvoke.VirtDisk Coverage NugetNuget
vssapi.dll Vanara.PInvoke.VssApi Coverage NugetNuget
WcmApi.dll Vanara.PInvoke.WcmApi Coverage NugetNuget
WcnApi.dll Vanara.PInvoke.WcnApi Coverage NugetNuget
websocket.dll Vanara.PInvoke.WebSocket Coverage NugetNuget
wer.dll Vanara.PInvoke.Wer Coverage NugetNuget
WinBio.dll Vanara.PInvoke.WinBio Coverage NugetNuget
winhttp.dll Vanara.PInvoke.WinHTTP Coverage NugetNuget
wininet.dll Vanara.PInvoke.WinINet Coverage NugetNuget
winscard.dll Vanara.PInvoke.WinSCard 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
wscapi.dll Vanara.PInvoke.WscApi Coverage NugetNuget
wslapi.dll Vanara.PInvoke.WslApi Coverage NugetNuget
WsmSvc.dll Vanara.PInvoke.WsmSvc Coverage NugetNuget
WTSApi32.dll Vanara.PInvoke.WTSApi32 Coverage NugetNuget
wuapi.dll Vanara.PInvoke.WUApi Coverage NugetNuget

Supporting Assemblies

Assembly    NuGet Link    Description
Vanara.BITS NugetNuget Complete .NET coverage of Windows BITS (Background Intelligent Transfer Service) functionality. Provides access to all library functions through Windows 11 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.DirectoryServices NugetNuget Wrapper classes around Win32 ADs methods and interfaces to provide simplified and object-oriented access to Active Directory and other directory service calls.
Vanara.Management NugetNuget Extensions and helper classes for System.Management.
Vanara.Net NugetNuget Abstracted classes around Win32 networking functions to provide simplified and object-oriented access to key networking capabilities like DNS, DHCP, filtering, access, and discovery.
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.PInvoke.SpellCheckingApi NugetNuget PInvoke API (methods, structures and constants) imported from Windows Spell Checking API.
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.Extensions NugetNuget Extension methods and conversions from Vanara P/Invoke types and methods to Windows Forms types and methods.
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.Common NugetNuget Common classes for Windows Shell items derived from the Vanara PInvoke libraries. Includes shell items, files, icons, links, and taskbar lists.
Vanara.Windows.Shell NugetNuget Classes for Windows Shell items derived from the Vanara PInvoke libraries. Includes shell items, files, icons, links, and taskbar lists.
Vanara.WinUI.Extensions NugetNuget Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods.
Vanara.WPF.Extensions NugetNuget Extension methods and conversions from Vanara P/Invoke types and methods to WPF types and methods.

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.