A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
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.8, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, .NET Standard 2.0 & 2.1, 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 commit triggers a GitHub Actions build. The status of that build is in the header of this page. The NuGet packages from those pre-release builds are available for test purposes on MyGet's project NuGet source at https://www.myget.org/feed/Packages/vanara. There are details at that site for adding it as a NuGet Source.
Use
- Just include the Vanara.Library package which includes all the Vanara assembilies. Overkill, but easy.
-OR-
- Grab each library as needed:
- Look for the function you need in Microsoft documentation. Note which library or DLL the function is in.
- 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".
- Add the assembly to your project via NuGet.
- To use a function, you can:
- Call it directly
var bret = Vanara.PInvoke.Kernel32.GetComputerName(sb, ref sbSz);
- 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);
- 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, try to simplify its use.
- Where a structure is always passed by reference and where that structure needs to clean up memory allocations, change the structure to a class implementing
IDisposable.
- Wherever possible, turn all handles 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.
- Functions use overloads, and often generated overloads, to simplify the call (allocating and releasing memory, handling pointers, templating complex patterns, etc.). When calling a method, check to see if there is an overload first.
Supported Libraries
Library/DLL
Assembly
Coverage
NuGet Link
aclui.dll
Vanara.PInvoke.AclUI
activeds.dll
Vanara.PInvoke.ActiveDS
advapi32.dll, secur32.dll, authz.dll, sspicli.dll, schannel.dll, tdh.dll
Vanara.PInvoke.Security
amsi.dll
Vanara.PInvoke.AMSI
avicap32.dll, avifil32.dll, msacm32.dll, msvfw32.dll, winmm.dll
Vanara.PInvoke.Multimedia
avrt.dll
Vanara.PInvoke.Avrt
cabinet.dll
Vanara.PInvoke.Cabinet
CfgMgr32.dll
Vanara.PInvoke.CfgMgr32
cimfs.dll
Vanara.PInvoke.CimFs
CldApi.dll
Vanara.PInvoke.CldApi
clfsw32.dll
Vanara.PInvoke.ClfsW32
comctl32.dll
Vanara.PInvoke.ComCtl32
ComDlg32.dll
Vanara.PInvoke.ComDlg32
credui.dll
Vanara.PInvoke.CredUI
crypt32.dll, bcrypt.dll, ncrypt.dll, tokenbinding.dll, cryptui.dll, cryptnet.dll, cryptdlg.dll
Vanara.PInvoke.Cryptography
d2d1.dll
Vanara.PInvoke.Direct2D
d3d11.dll, d3dcsx.dll, d3d11sdklayers.dll
Vanara.PInvoke.Direct3D11
d3d12.dll
Vanara.PInvoke.Direct3D12
d3dcompiler_47.dll, dxilconv.dll
Vanara.PInvoke.D3DCompiler
davclnt.dll
Vanara.PInvoke.DavClnt
DbgHelp.dll, ImageHlp.dll
Vanara.PInvoke.DbgHelp
Dcomp.dll
Vanara.PInvoke.Dcomp
Dhcpcsvc6.dll, Dhcpcsvc.dll
Vanara.PInvoke.Dhcp
DnsApi.dll
Vanara.PInvoke.DnsApi
dosvc.dll
Vanara.PInvoke.DOSvc
drt.dll, drtprov.dll, drttransport.dll
Vanara.PInvoke.Drt
dstorage.dll
Vanara.PInvoke.DStorage
dwmapi.dll
Vanara.PInvoke.DwmApi
dwrite.dll
Vanara.PInvoke.DWrite
dxcore.dll
Vanara.PInvoke.DXCore
dxgi.dll
Vanara.PInvoke.DXGI
ehstorapi.dll
Vanara.PInvoke.EnhancedStorage
elscore.dll
Vanara.PInvoke.ElsCore
fhsvcctl.dll
Vanara.PInvoke.FhSvcCtl
FirewallApi.dll
Vanara.PInvoke.FirewallApi
[](