Hands up if you remember playing with the Win32 APIs! I am sure most of you reading this did. If not, I would suggest reading up on the powerful Win32 APIs.

I remember when I first started with programming (many eons ago), the Win32 API always intrigued me. Doing things seemingly impossible – such as keeping track of the mouse position outside of a Windows Form, obtaining a list of open windows and processes, and changing the Taskbar options through your own application – was a rewarding challenge. I am going to go a bit into history here so that you can see how far the Win32 API has come.

In Visual Basic 6 (yes, you read that right)

To include a Windows API function in your VB6 programs, use the Declare statement to "declare" the function to be part of your program. The Declare statement is added to the General Declarations section of either a standard module or a form. If the Declare statement is added to a standard module, the function is considered Public and can be called from anywhere in your application. If the Declare statement is added to the General Declarations section of a form, the function is local to that form and can be called only from within that form. In the latter case, you need to include the Private keyword.

Syntax

The syntax of the Declare statement depends on whether the procedure you call returns a value. If the procedure does return a value, you use the Function form of the Declare statement:

[Public | Private] Declare Function publicname Lib "libname" _
[Alias "alias"] [([[ByVal | ByRef] argument [As Type] _
[, [Byval | ByRef] argument [As Type]] ...])] [As Type]

Example

Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long

You could use the above Win 32 API to copy a file for example. Let's have a look at Visual C++ 6 next.

VC++ 6

What I have always liked about Visual C++ is the fact that it was easier to include Win 32 API functions as it was built-in – not saying VB 6 could not do this, but it was a tad more difficult. You could also create specific apps for Win32, these included:

  1. Win32 Application

  2. Win32 Console Application

  3. Win32 Dynamic-Link Library

  4. Win32 Static Library

These are shown in the image below.

Graphical user interface, text, application

Description automatically generated

Figure 1 - Visual C++ 6 New Project

API Viewers & Tools

While I am on the topic of APIs on older frameworks, the following two websites deserve special mention. If you did not know the name of a certain Win32 API, or didn't know how to implement it, these two sites were very helpful in the names and implementations of Win32 APIs:

AllAPI Network

ApiViewer

.NET workarounds - P/Invoke

With .NET you could also make use of the Win32 API at any time. This is possible with the help of wrappers and bindings such as the P/Invoke (Platform Invoke) scheme. P/Invoke enables you to access structs, callbacks, and functions in unmanaged libraries from managed (.NET) code. By using the System and System.Runtime.InteropServices you can communicate with native components from your managed code.

Syntax

An example of the of a Platform Invoke statement looks like the following:

using System;
using System.Runtime.InteropServices;
    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);

This imports the MessageBox function inside the User32.DLL into your application.

C# and RUST

Microsoft recently introduced the win32metadata project. This eases the creation of language projections by providing a complete description of the Win32 API surface so it can be automatically projected to any language, thus improving correctness and minimizing maintenance. Developers will use the language projections that consume the metadata to project the APIs into the natural patterns of specific languages.

C# / Win32 language projection

Rust language projection

Modern C++ projection (to follow soon)






This article was originally published on February 25, 2021