Language: EN

vanara

How to use the Windows API with C# Vanara

Vanara is a C# library that allows .NET developers to easily exceed the functions available in the Windows API.

Accessing the Windows API allows us to perform a wide range of options that we could not do otherwise. But in general, its use is complex and error-prone.

Vanara is designed to facilitate this interop work, providing a safe and easy way, without the need to use unsafe code.

For example, Vanara provides functions for working with the system’s process list, handling files and directories, working with the network, among others. It also provides a series of security classes for working with digital certificates, cryptography, and authentication.

If we look at the number of assemblies and functions that Vanara provides, it’s just insane. Fortunately, the project documentation is very good. And, still, it’s difficult to get an idea of the number of functions that are there.

How to use Vanara

The amount of existing functions in Vanara is so large that, according to the library’s own documentation, the recommended way to use it is as follows,

  • First, we look for the function we need in the Microsoft documentation, and we note in which library or DLL it is located.
  • We check that the equivalent of this library exists in Vanara, and has the function we need. To do this, we review the table of libraries on the project’s website.
  • With that, we would get the name of the Nuget we need. We download it and add it to the solution
  • We can now use the function with Vanara.

For example, if we need the GetComputerName function, we look in the Microsoft documentation and, at the end of the page, it indicates that it is part of Kernel32.

Now we go to the documentation of Vanara.Kernel32 and check that the function exists.

We add the corresponding Nuget package,

Install-Package Vanara.PInvoke.Kernel32

And now we can use the function in our code.

using static Vanara.PInvoke.Kernel32;

var sbSz = 0;
var sb = new StringBuilder(sbSz);

// Get the computer name and display it in the console
if (Kernel32.GetComputerName(sb, ref sbSz))
{
    Console.WriteLine($"Computer name: {sb}");
}
else
{
    Console.WriteLine($"Error getting the computer name: {Win32Error.GetLastError()}");
}

If, for example, we need the FindWindowA function, we go to the Microsoft documentation and see that it is part of User32.dll.

We add the corresponding Nuget package,

Install-Package Vanara.PInvoke.User32

And now we can use the function in our code.

string windowTitle = "My Window"; // The title of the window we want to find

var hWnd = FindWindow(null, windowTitle);
if (hWnd != default)
{
	Console.WriteLine($"The window '{windowTitle}' was found with the handle: {hWnd}");
}
else
{
	Console.WriteLine($"The window '{windowTitle}' was not found.");
}

Obviously, it’s not the easiest library to use. But, dealing with Interop actions, and especially with the Windows API, is never entirely easy.

With Vanara, we have almost all the functions of the Windows API in C# functions. This allows us to do many functions that are not possible otherwise, and it is much easier and safer than using the DLLs directly.

Vanara is Open Source, and all the code and documentation is available in the project repository at https://github.com/dahall/Vanara