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.
- 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 the 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);
- Call it directly
- 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.
- 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 fromgdi32.dll
in the system directory.)
- (e.g. The assembly
- Any structure or macro or enumeration (no function) that is used by many libraries is put into either
Vanara.Core
orVanara.PInvoke.Shared
.- (e.g. The macro
HIWORD
and the structureSIZE
are both inVanara.PInvoke.Shared
and classes to simplify interop calls and native memory management are inVanara.Core
.)
- (e.g. The macro
- 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.)
- (e.g. In the
- 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 derivedSafeHANDLE
exists that performs that function on disposal.- e.g.
HTOKEN
is defined.SafeHTOKEN
builds upon that handle with an automated release callingCloseHandle
.
- e.g.
- 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 theref
keyword.- Windows API:
BOOL MapDialogRect(HWND hDlg, LPRECT lpRect)
- Vanara:
bool MapDialogRect(HWND hDlg, in RECT lpRect);
- Windows API:
- 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.
Assembly | NuGet Link | Description |
---|---|---|
Vanara.BITS | 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 | 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:
|
|
Vanara.DirectoryServices | 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 | Extensions and helper classes for System.Management. | |
Vanara.Net | 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 | Shared methods, structures and constants for use throughout the Vanara.PInvoke assemblies. Includes:
|
|
Vanara.PInvoke.SpellCheckingApi | PInvoke API (methods, structures and constants) imported from Windows Spell Checking API. | |
Vanara.Security | 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 | 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 | .NET classes to manage Windows Virtual Storage (VHD and VHDX) using P/Invoke functions from VirtDisk.dll. | |
Vanara.Windows.Extensions | Extension methods and conversions from Vanara P/Invoke types and methods to Windows Forms types and methods. | |
Vanara.Windows.Forms | 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 | Common classes for Windows Shell items derived from the Vanara PInvoke libraries. Includes shell items, files, icons, links, and taskbar lists. | |
Vanara.Windows.Shell | Classes for Windows Shell items derived from the Vanara PInvoke libraries. Includes shell items, files, icons, links, and taskbar lists. | |
Vanara.WinUI.Extensions | Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods. | |
Vanara.WPF.Extensions | Extension methods and conversions from Vanara P/Invoke types and methods to WPF types and methods. |
There are numerous examples in the UnitTest folder and in the WinClassicSamplesCS project that recreates the Windows Samples in C# using Vanara.