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 and 4.5. In all cases where a dependency doesn't disallow it, a .NET Standard 2.0 build is also included for use with UWP and other .NET Core and Standard projects.
- 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.
- 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
I have tried to follow the concepts below in laying out the libraries.
- 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 simplfy 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.)
- Where the direct interpretation of a structure leads to memory leaks or misuse, I have tried to simplify their use
- Where structures are 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. - 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 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.
Library/DLL | Assembly | NuGet Link |
---|---|---|
AclUI.dll | Vanara.PInvoke.AclUI |
|
qmgr.dll (BITS) | Vanara.PInvoke.BITS |
|
ComCtl32.dll | Vanara.PInvoke.ComCtl32 |
|
CredUI.dll | Vanara.PInvoke.CredUI |
|
Crypt32.dll | Vanara.PInvoke.Crypt32 |
|
DwmApi.dll | Vanara.PInvoke.DwmApi |
|
Gdi32.dll | Vanara.PInvoke.Gdi32 |
|
IpHlpApi.dll | Vanara.PInvoke.IpHlpApi |
|
Kernel32.dll | Vanara.PInvoke.Kernel32 |
|
Mpr.dll | Vanara.PInvoke.Mpr |
|
NetApi32.dll | Vanara.PInvoke.NetApi32 |
|
NetListMgr.dll | Vanara.PInvoke.NetListMgr |
|
NTDSApi.dll | Vanara.PInvoke.NTDSApi |
|
NTDll.dll | Vanara.PInvoke.NTDll |
|
Ole32.dll, OleAut32 and PropSys.dll | Vanara.PInvoke.Ole |
|
AdvApi32.dll, Authz.dll and Secur32.dll | Vanara.PInvoke.Security |
|
Shell32.dll | Vanara.PInvoke.Shell32 |
|
ShlwApi.dll | Vanara.PInvoke.ShlwApi |
|
TaskSchd.dll | Vanara.PInvoke.TaskSchd |
|
User32.dll | Vanara.PInvoke.User32 |
|
User32.dll that have GDI references | Vanara.PInvoke.User32.Gdi |
|
UxTheme.dll | Vanara.PInvoke.UxTheme |
|
VirtDisk.dll | Vanara.PInvoke.VirtDisk |
|
WinINet.dll | Vanara.PInvoke.WinINet |
Assembly | NuGet Link | Description |
---|---|---|
Vanara.Core | Shared methods, structures and constants for use throughout the Vanara assemblies. Think of it as windows.h with some useful extensions. | |
Vanara.PInvoke.Shared | Shared methods, structures and constants for use throughout the Vanara.PInvoke assemblies | |
Vanara.Security | Wrapper classes for security related items in the PInvoke libraries | |
Vanara.SystemServices | Wrapper classes for system related items in the PInvoke libraries | |
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, simplified designer framework for Windows.Forms. | |
Vanara.Windows.Shell | Classes for Windows Shell items derived from the Vanara PInvoke libraries. Includes shell items, files, icons, links, and taskbar lists. |
There are numerous examples in the UnitTest folder.