WIN32 APIs
The Windows operating system, and its various applications are written in a variety of programming languages ranging from assembly to C# but many of those make use of the Windows-provided built-in application programming interfaces (or APIs).
These interfaces, known as the Win32 API, offer developers pre-built functionality. The APIs themselves are designed to be invoked from C and are documented with C-style data types but as we will discover throughout this course, they can be used with multiple other languages.
Many of the Win32 APIs are documented by Microsoft. One simple example is the GetUserNameA
API exported by Advapi32.dll
which retrieves the name of the user executing the function.
The syntax section of the documentation shows the function prototype that details the number and type of arguments along with the return type:
1
2
3
4
BOOL GetUserNameA(
LPSTR lpBuffer,
LPDWORD pcbBuffer
);
In this example, the API requires two arguments. The first is an output buffer of type LPSTR
which is the Microsoft term for a character array. The second argument is a pointer to a DWORD which is a 32-bit unsigned integer. The return value from the API is a boolean.
As we use these APIs we must keep in mind two details. First, we must determine if the process is 32-bit or 64-bit since some arguments and their size depend on the bitness. Second, we must distinguish between the use of ASCII and Unicode (which Microsoft sometimes refers to as UTF-16). Since ASCII characters use one byte and Unicode uses at least two, many of the Win32 APIs are available in two distinct versions.
The code above shows the prototype for GetUserNameA
, where the suffix “A” indicates the ASCII version of the API. The code below shows the prototype for GetUserNameW
, in which the “W” suffix (for “wide char”) indicates Unicode:
c BOOL GetUserNameW( LPWSTR lpBuffer, LPDWORD pcbBuffer );
The first argument type is now of type LPWSTR which is a UNICODE character array.