Common Types
This section covers fundamental data types and concepts frequently used across the Windows API. Understanding these types is crucial for effective interaction with the operating system.
Error Codes
Error codes are integral to the Windows API, providing detailed information about the success or failure of an operation. They are typically returned as values of type DWORD.
System Error Codes
The Windows operating system defines a wide range of system-wide error codes. These are often returned by functions when an error occurs. The most common way to retrieve more descriptive information about an error code is by using the FormatMessage API.
typedef unsigned long DWORD;
Commonly Encountered Error Codes
| Error Code | Symbolic Name | Description |
|---|---|---|
0x00000000 |
ERROR_SUCCESS |
The operation completed successfully. |
0x00000002 |
ERROR_FILE_NOT_FOUND |
The system cannot find the file specified. |
0x00000005 |
ERROR_ACCESS_DENIED |
Access is denied. |
0x00000011 |
ERROR_INVALID_FUNCTION |
The requested operation is not valid for this object. |
0x00000057 |
ERROR_INVALID_PARAMETER |
The parameter is incorrect. |
0x0000007B |
ERROR_INVALID_NAME |
The filename or extension is too long. |
0x00000087 |
ERROR_INVALID_PARAMETER |
The parameter is incorrect. (Often overlaps with 0x57) |
0x000000EF |
ERROR_ALREADY_EXISTS |
The file or directory specified already exists. |
0x000000F0 |
ERROR_NOT_ENOUGH_MEMORY |
Not enough memory is available to complete this operation. |
0x80070005 |
E_ACCESSDENIED |
Access is denied. (HRESULT equivalent) |
Retrieving Error Messages
The FormatMessage function is essential for translating numerical error codes into human-readable strings. This function takes the error code and retrieves a formatted message string from the system's message table.
// Example usage in C++ (simplified)DWORD error_code = GetLastError();LPSTR message_buffer = (LPSTR) malloc(sizeof(char) * MAX_PATH);DWORD length = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULTY),message_buffer,
MAX_PATH,NULL);
// Use message_buffer...free(message_buffer);
Custom Error Codes
Applications can define their own error codes, typically within a specific range to avoid conflicts with system error codes. These are often defined in header files specific to the application or library.